从DeviceAdminReceiver传递结果返回到调用活动结果、DeviceAdminReceiver

由网友(我的拽你学不起)分享简介:我的活动(MyActivity.class)执行设置屏幕锁定的方法如下:My activity (MyActivity.class) executes the method to set the screen lock as follow:startActivityForResult(Security.setLoc...

我的活动(MyActivity.class)执行设置屏幕锁定的方法如下:

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);
    }
}

然后我处理结果从 onActivityResult 方法 MyActivity 类。

就是上面的最好方式传递结果返回到活动?我注意到上面创建重用MyActivity类的现有实例的MyActivity类的另一个实例吧。

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?

推荐答案

我已经找到一种解决方案是使用共享preferences 从传递数据 DeviceAdminReceiver 类的活动类。

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.

阅读全文

相关推荐

最新文章