Pass result from DeviceAdminReceiver back to calling activity

孤街浪徒 提交于 2019-12-23 04:33:30

问题


My activity (MyActivity.class) executes the method to set the screen lock as follow:

startActivityForResult(Security.setLockscreen(getBaseContext()), 1001);

Then my receiver class logs the change to the screen lock as follow:

public class MyDeviceAdminReceiver extends DeviceAdminReceiver {
   @Override
   public void onPasswordChanged(Context context, Intent intent) {
        super.onPasswordChanged(context, intent);
        // pass result back to calling activity
        intent = new Intent(context, MyActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("pwdChange", true);
        context.startActivity(intent);
    }
}

And then I process the result in the onActivityResult method from MyActivity class.

Is the above the best way to pass result back to the activity? I noticed the above creates another instance of MyActivity class instead of reusing an existing instance of MyActivity class.

Is there another more efficient way of passing data back to the calling activity?


回答1:


One solution I've found is to use SharedPreferences to pass data from a DeviceAdminReceiver class to an Activity class.

I'm sure there are other working solutions that other experienced developers can post in this thread to share with everyone.



来源:https://stackoverflow.com/questions/13965380/pass-result-from-deviceadminreceiver-back-to-calling-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!