You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加SD/SIM卡广播接收器后Android7.1开机自启失效求助

开机自启功能失效排查

原本应用可正常实现开机自启,添加SD卡和SIM卡状态监听的广播接收器后,该功能失效。设备目标版本为Android7.1(SDK level 25),尝试以下操作均无效:

  • 移除SimCardReceiver
  • 为StartOnBoot的intent-filter添加android:priority="1000"

原有正常实现代码

AndroidManifest配置

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- start when module start broadcast -->
<receiver android:name=".StartOnBoot"
    android:exported="true">
    <intent-filter >
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
<!-- listen messenger received -->
<receiver
    android:name=".Phone.SmsListener"
    android:exported="true">
    <intent-filter android:priority="1000">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

StartOnBoot类代码

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

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

新增的广播接收器代码

AndroidManifest配置

<!-- broad cast SDCard receive-->
<receiver android:name=".Peripheral.SDCardListener">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_EJECT" />
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <data android:scheme="file" />
    </intent-filter>
</receiver>

<receiver android:name=".Phone.SimCardReceiver">
    <intent-filter>
        <action android:name="android.intent.action.SIM_STATE_CHANGED"/>
    </intent-filter>
</receiver>

SDCardListener类代码

public class SDCardListener extends BroadcastReceiver {

//restart app when SD card mounted
@Override
public void onReceive(Context context, Intent intent) {
    Log.d("TAG","SDcard state : " + intent.getAction());
    if (intent.getAction().equals(Intent.ACTION_MEDIA_EJECT)){
       //sdcard eject
    }else if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){
        SDCard.getInstance().SDCardMounted();
     }
   }
  }

SimCardReceiver类代码

public class SimCardReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.d("TAG", "Sim state :" + intent.getAction());
   }
  }

额外问题

  1. 一个应用中可以使用多少个广播接收器?多少个比较合适?是否无限制?
  2. 是否可以将所有接收器合并为一个BroadcastReceiver?

内容的提问来源于stack exchange,提问作者Jamebes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 23:25:22