Search code examples
androidandroid-intentandroid-activitybroadcastreceiver

Will the app receive Broadcast when it's not launched?


In my project, I have two modules: "app" and "outerscheme". In app, there is a MainBroadcastReceiver (extends Broadcast). In outerscheme, there is a MainActivity (extends Activity). The code is as follows:

outerscheme MainActivity core code :

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.activity: // start SecondActivity in app module by implicit
            Intent activityIntent = new Intent("SecondActivity");
            activityIntent.setData(Uri.parse("SteveqiSecondActivity://"));
            startActivity(activityIntent);
            break;
        case R.id.broadcast: // start MainBroadcastReceiver in app module by implicit
            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("Broadcast");
            // add this the app module may be wakeup.
            broadcastIntent.setData(Uri.parse("SteveqiBroadcast://"));
            sendBroadcast(broadcastIntent);
        default:
            break;
    }
}

app Manifest core code:

     <activity
        android:name="com.twlkyao.myscheme.SecondActivity"
        android:label="@string/title_activity_second"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="SecondActivity" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="SteveqiSecondActivity" />
        </intent-filter>
    </activity>   
    <receiver android:name="com.twlkyao.myscheme.MainBroadcastReceiver">
        <intent-filter>
            <action android:name="Broadcast" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="SteveqiBroadcast" />
        </intent-filter>
    </receiver>

Part I:

When click the R.id.activity button, the SecondActivity in app module is up, so as the app module, at this time click the R.id.broadcast button, the MainBroadcastReceiver can receive the broadcast, then kill the process of app module, click the R.id.broadcast button, the MainBroadcastReceiver can receive the broadcast again, and kill app module again, and click the R.id.broadcast button the MainBroadcastReceiver can never receive the broadcast.

Part II:

When I remove the line <data android:scheme="SteveqiBroadcast" /> of the MainBroadcastReceiver, and repeat the Part I steps, the MainBroadcastReceiver can never receive the broadcast.

My questions are:

1.Is that the app module must be alive so that BroadcastReceiver can receive broadcast?

2.Why the BroadcastReceiver may receive broadcast when <data android:scheme="SteveqiBroadcast" /> exist, and can't receive broadcast when the line not exit?


Solution

  • I have solved the problem, it's because of the ROM, I think MIUI restricts the Broadcast, I have successfully run the demo on SamSung phone.