Can not start on boot when add more broadcast receive

Viewed 27

I found many topic but no case fit for my question. The first my app working smooth, it is always startup with device by code bellow:

In AndroidManifest:

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

and receiver:

 <!-- 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>

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

And now I added 2 broadcast to listen state of SD card and SIM card. But when I finish, My app can not start when my device restarted. And I don't know, where is problem, nothing clue.

 <!--        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>

And 2 classes receiver:

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

for sim state:

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

My device target is only Android 7.1, SDK lv 25. Some methods I tried but not work:

  • I removed SimCardReceiver
  • I added "android:priortity to StartBoot" like this:

<intent-filter android:priority="1000">

But still not working, Remember, my app always work without 2 broadcast SDCardListener and SimCardReceiver Extra question:

  • How many broadcast are available to use in one app. How many is good ? one or not limit
  • Could I merge all receiver to one broadcast
0 Answers
Related