2013-03-14 83 views
3

Here's我的问题:阻止Android的BootReceiver从开始主要活动

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

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

Here's所涉及的代码:

的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提供。

任何帮助,非常感谢!

+0

什么是'de.myapp.android.MESSAGE'? – StarPinkER 2013-03-15 08:13:37

回答

0

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

+0

你说得对。我实现了启动接收器,因为推送通知在重新启动后不起作用。但是这是由于另一个错误,所以在正确设置时,实际上不需要手动在每次启动时实例化PushManager。 – 2013-03-15 08:29:25