BroadcastReceiver 未在启动时触发 - 以编程方式

BroadcastReceiver not triggered on boot - programmatically

如果用户希望在启动时启动我的应用程序,我将尝试启动它。我有 un/register 广播接收器的复选框。如果我在清单中声明接收器,我的应用程序总是在启动时启动,这是不需要的行为。我也试过 getApplicationcontext().registerReceiver 没有运气。我错过了什么吗?

    final BroadcastReceiver startMyActivityAtBootReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            Log.i("LockState", "broadcast receiver called started");
            if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
                Intent activityIntent = new Intent(context, MainActivity.class);
                activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(activityIntent);
            }
        }
    };

    if (switch4.isChecked()){
        LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(startMyActivityAtBootReceiver, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
        Log.i("LockState", "broadcastreceiver registered");
    }
    else {
        LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(startMyActivityAtBootReceiver);
        Log.i("LockState", "broadcastreceiver unregistered");
    }

    switch4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            SharedPref.write(Constants.LAUNCH_AT_START, isChecked);
            Log.i("LockState", "launch app at start: " + isChecked);

            if (isChecked) {
                LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(startMyActivityAtBootReceiver, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
                Log.i("LockState", "broadcastreceiver registered");
            } else {
                LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(startMyActivityAtBootReceiver);
                Log.i("LockState", "broadcastreceiver unregistered");
            }
        }
    });

LocalBroadcastManager 将 Intent 广播发送到进程中的本地对象,Boot 接收器将处理发送全局广播。

所以我们应该在 AndroidManifest.xml 中注册广播,当您收到接收方的回调时,您可以检查逻辑并针对它执行操作。

示例:

switch4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            SharedPref.write(Constants.LAUNCH_AT_START, isChecked);
            Log.i("LockState", "launch app at start: " + isChecked);
        }
    });

并在单独的 class 中创建广播接收器并在 AndroidManifest.xml 中注册。

AndroidManifest.xml:

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

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

MyBootReceiver.java:

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

        if (Check your prefernce) {
                Intent activityIntent = new Intent(context, MainActivity.class);
                activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(activityIntent);
          }
    }
}

必须在清单中声明。你正在以错误的方式解决这个问题。 您不应允许用户 enable/disable 广播接收器进行引导,而应在接收器本身内处理该选项。根据复选框中保存的用户首选项值,启动或不启动 activity。

    @Override
    public void onReceive(Context context, Intent intent) {
        // Handle user preference
        if (!isUserPreferenceEnabled()) return;

        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent activityIntent = new Intent(context, MainActivity.class);
            activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(activityIntent);
        }
    }