0

I want to protect certain features behind device password. So if users try to use those feature, they have to confirm password again.

This is code in activity class.

Context appContext = activity.getApplicationContext();
RestrictionsManager rManager = (RestrictionsManager) appContext.getSystemService(appContext.RESTRICTIONS_SERVICE);
rManager.requestPermission(RestrictionsManager.ACTION_REQUEST_PERMISSION, "someID", new PersistableBundle());

This is code to handle response from the password-confirmation dialog in PermissionResponseReceiver class.

public class PermissionResponseReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
       ......
    }
}

Is there a simple way to reference to the original activity in PermissionResponseReceiver.onReceive(...). Currently I have to keep track of activity instance and provide a public static method to get it, but that seems very hacky and I'm looking for an alternative. Do you have any suggestions? Thanks.

hixhix
  • 771
  • 7
  • 23
  • Why do you want to do this? Holding any activity reference is dangerous, but especially a static one. If you explain what you are trying to achieve, someone will tell you the right way to do it. – Simon Oct 31 '15 at 00:24
  • If password is confirmed then I want the activity to do something. Hence, I need to be able to access back to activity in onReceive() – hixhix Oct 31 '15 at 00:50
  • Try moving the PermissionResponseReceiver as an inner class of your activity. – kzz Oct 31 '15 at 01:12

1 Answers1

1

Use a local broadcast to communicate between your PermissionResponseReceiver and your Activity.

In your PermissionResponseReceiver:

public class PermissionResponseReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
       ......

    // Create the intent that will be broadcast
    Intent intent = new Intent("my-permission-response-action");
    // Add any extra data you want to put in the intent here..

    // Broadcast to any registered receivers
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
    }
}

In your Activity:

@Override
public void onResume() {
    super.onResume();

    final LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);

    // IntentFilter to define which actions mLocalBroadcastReceiver will respond to
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("my-permission-response-action");

    // Register for desired broadcasts
    localBroadcastManager.registerReceiver(mLocalBroadcastReceiver, intentFilter);
}

@Override
public void onPause() {

    // Unregister our mLocalBroadcastReceiver
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocalBroadcastReceiver);
    super.onPause();
}

/**
 * My Local Broadcast Receiver
 */
private BroadcastReceiver mLocalBroadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(my-permission-response-action)) {
            // Our Activity has received the broadcast from PermissionResponseReceiver.
            // If we added any extras to the intent we can extract them here
            // for use in the Activity
        }
    }
};
Sound Conception
  • 5,263
  • 5
  • 30
  • 47