阻止Android BootReceiver启动主要活动

时间:2013-03-14 09:45:11

标签: android android-intent push-notification boot

这是我的问题:

我的Android应用程序注册了一个启动接收器,它初始化了PushNotification-Manager(PushWoosh)。 这是因为即使重启设备,用户也应该能够接收推送通知而无需手动启动应用程序。

这样可行,但是当设备重新启动时,应用程序主要活动(MainMenuActivity)会启动并带到前台,这不应该发生。

以下是涉及的代码:

的AndroidManifest.xml:

<!-- Re-register PushManagers after reboot -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application 
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar">

<!-- PushWoosh -->
<activity android:name="com.arellomobile.android.push.PushWebview"/>
<activity android:name="com.arellomobile.android.push.MessageActivity"/>
<activity android:name="com.arellomobile.android.push.PushHandlerActivity"/>

<!-- PushWoosh -->
<receiver
    android:name="com.google.android.gcm.GCMBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND">

    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
        <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
        <category android:name="de.myapp.android"/>
    </intent-filter>
</receiver>

<!-- PushWoosh -->
<service android:name="com.arellomobile.android.push.PushGCMIntentService"/>

<!-- Boot-Receiever -->
<receiver android:name="de.myapp.android.startup.BootCompleteReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

<activity
    android:name="de.myapp.android.activity.MainMenuActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="@string/app_name"
    android:launchMode="singleTop"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="adjustPan">

    <intent-filter>
        <!-- Starten bei Klick auf Launcher Icon -->
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>

    <intent-filter>
        <!-- Starten bei Erhalt einer Push Notification -->
        <action android:name="de.myapp.android.MESSAGE"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

</activity>

BootCompleteReceiver.java:

public class BootCompleteReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent i) {
        PushWooshHelper.setupPushNotifications(context.getApplicationContext());
    }

}

PushWooshHelper.java:

public class PushWooshHelper {

    public static void setupPushNotifications(Context context) {
        PushManager pushManager = new PushManager(context, AppIDs.PUSHWOOSH_APP_ID, AppIDs.GCM_PROJECT_ID);
        pushManager.onStartup(context);
        pushManager.startTrackingGeoPushes();
    }

}

MainMenuActivity.java:

public class MainMenuActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }


    private void checkMessage(Intent intent) {

        if (intent != null) {

            String log = "PUSH NOTIFICATION RECEIVED.";

            if (intent.hasExtra(PushManager.PUSH_RECEIVE_EVENT)) {
                log += "message: " + intent.getExtras().getString(PushManager.PUSH_RECEIVE_EVENT);
            }
            else if (intent.hasExtra(PushManager.REGISTER_EVENT)) {
                log += "<register>";
            }
            else if (intent.hasExtra(PushManager.UNREGISTER_EVENT)) {
                log += "<unregister>";
            }
            else if (intent.hasExtra(PushManager.REGISTER_ERROR_EVENT)) {
                log += "<register error>";
            }
            else if (intent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT)) {
                log += "<unregister error>";
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {

        super.onNewIntent(intent);

        setIntent(intent);
        checkMessage(intent);

        setIntent(new Intent());
    }
}

请注意:我无法访问PushManager.onStartup(),因为它是由PushWoosh提供的。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

这很奇怪。 Pushwoosh使用GCM,它在开箱后重启设备后工作。您只需注册一次推送通知,然后在重新启动后GCM会自行处理。