简体   繁体   中英

Android how to initiate a BroadcastReceiver when phone restarted

How can I initiate my BroadcastReceiver whenever a user phone has been restarted. and if user kill the app, Can the BroadcastReceiver keep running in the back ground ?

I created a ParsePushBroadcastReceiver that receive push notification from parse.com. but my app will only receive the notification if the user opens the app.

public class MyParseReceiver extends ParsePushBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.i(TAG, "onReceive Called");

        if (intent == null) {
            Log.e(TAG, "Receiver intent null");
        } 
        else {
            // Parse push message and handle accordingly
            Log.d(TAG, "Receiver intent data => " + intent.toString());
        }

    }//end onReceive



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

My manifest:

<activity
        android:name=".SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

<service android:name="com.parse.PushService" />

<receiver android:name="com.parse.ParseBroadcastReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED" />
        <action android:name="android.intent.action.USER_PRESENT" />
    </intent-filter>
</receiver>

<receiver
    android:name="com.my.app.core.MyParseReceiver"
    android:exported="false" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.USER_PRESENT" />
        <action android:name="com.parse.push.intent.RECEIVE" />
        <action android:name="com.parse.push.intent.DELETE" />
        <action android:name="com.parse.push.intent.OPEN" />
    </intent-filter>
</receiver>
<receiver
    android:name="com.parse.GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED" />
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

        <category android:name="com.my.app" />
    </intent-filter>
</receiver>

<meta-data
    android:name="com.parse.push.notification_icon"
    android:resource="@drawable/ic_launcher" />

Q: How can I initiate my BroadcastReceiver whenever a user phone has been restarted. and if user kill the app, Can the BroadcastReceiver keep running in the back ground?
A: Yes, It can.
If you want to implement BroadcastReceiver (push notifications) into your app, you should use Google Cloud Message with the latest GCM instead of the older C2DM.
This service use Google Play Service, so you haven't to run the app because it base on Google Play Service, which is a service on your android.
Please refer: https://developer.android.com/google/gcm/index.html
Example: https://github.com/google/gcm

您能否在清单中为接收者标签设置android:exported="true"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM