gpt4 book ai didi

android - 阻止 Android BootReceiver 启动主要 Activity

转载 作者:太空狗 更新时间:2023-10-29 15:12:40 26 4
gpt4 key购买 nike

这是我的问题:

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

这是可行的,但是当设备重新启动时,应用程序主 Activity (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();
}

}

主菜单 Activity .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 提供的。

非常感谢任何帮助!

最佳答案

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

关于android - 阻止 Android BootReceiver 启动主要 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15405787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com