gpt4 book ai didi

java - 在重启时加速 BroadcastReceiver 的启动

转载 作者:行者123 更新时间:2023-11-30 00:41:52 24 4
gpt4 key购买 nike

我有一个应用程序,用户可以在其中调用自定义锁定屏幕来锁定他/她的手机,这是一个名为 LockScreen.class 的 Activity 。一旦加载此屏幕,即设备被锁定,一个名为 IsLockedSharedPreferences 被指定为 true。一旦用户使用锁定屏幕执行他需要的操作,此 islocked 变为错误,手机恢复正常。

每个 hing 都按预期正常工作。

问题是,如果由于某种原因手机在 lock 屏幕处于 Activity 状态时重新启动,则它不会在重新启动时重新运行。

所以,我创建了一个 BootReciever,如下所示,它工作正常,但是在完成重启过程后,用户可以在加载之前做很多事情,我的问题是 如何我让它加载得更快? 以便在让用户有机会使用手机执行任何操作之前,手机屏幕再次被自定义 Activity 锁定?

public class BootReciever extends BroadcastReceiver
{

SharedPreferences mPrefs;
final String IsLockedPref = "IsLocked";

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

mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Boolean islocked = mPrefs.getBoolean(IsLockedPref, false);

Intent i;
if (islocked)
i = new Intent(context, LockScreen.class);
else
i = new Intent(context, MainActivity.class);

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}

list 文件的一部分是:

<receiver android:name=".BootReciever"
android:enabled="true"
android:exported="true">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

更新

我试图使用 this通过添加以下内容授予 Device Admin,但没有任何改进:

在主 Activity 中:

    private static final int ADMIN_INTENT = 15;
private static final String description = "Some Description About Your Admin";
private DevicePolicyManager mDevicePolicyManager;
private ComponentName mComponentName;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mDevicePolicyManager = (DevicePolicyManager)getSystemService(
this.DEVICE_POLICY_SERVICE);
mComponentName = new ComponentName(this, AdminReceiver.class);
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,description);
startActivityForResult(intent, ADMIN_INTENT);
.
.
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADMIN_INTENT) {
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "Registered As Admin", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Failed to register as Admin", Toast.LENGTH_SHORT).show();
}
}
}

并创建了空接收器以将 DeviceAdminReceiver 扩展为:

public class AdminReceiver extends DeviceAdminReceiver {

}

并将以下内容添加到 list 中:

<receiver
android:name="AdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/admin"/>

<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>

最佳答案

您可以做的一件事是为您的 intent-filter 设置优先级。来自 documentation

It controls the order in which broadcast receivers are executed to receive broadcast messages. Those with higher priority values are called before those with lower values.

<intent-filter 
android:priority="100">
...
</intent-filter>

The value must be an integer, such as "100". Higher numbers have a higher priority. The default value is 0. The value must be greater than -1000 and less than 1000.

关于java - 在重启时加速 BroadcastReceiver 的启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42447646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com