我正在使用BroadCastReceiver来收听GCM消息。即使应用程序被杀并且手机被锁定,我也希望我的通知到达。但是当应用程序被杀时,我的广播接收器的onReceive没有被触发,尽管它在应用程序处于后台时工作正常。 我已在AndroidManifest.xml中声明了我的BroadCastReceiver,并且不会在应用程序的任何位置手动取消注册广播接收器。
GcmBroadcastReceiver.java
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
PowerManager powermanager;
WakeLock wakeLock;
@Override
public void onReceive(Context context, Intent intent) {
powermanager= ((PowerManager)context.getSystemService(Context.POWER_SERVICE));
wakeLock=powermanager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
wakeLock.acquire();
Log.d("GCM broadcast receiver", intent+"");
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
System.out.println("MESSAGE IS---->>>"+msg);
ComponentName comp = new ComponentName(context.getPackageName(),
GCMNotificationIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tcs.hcp_android"
android:versionCode="3"
android:versionName="1.2" >
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:requiresSmallestWidthDp="120"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<permission
android:name="com.tcs.hcp_android"
android:label="my_permission"
android:protectionLevel="dangerous" >
</permission>
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="24" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_SECURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.tcs.hcp_android.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="com.google.android.c2dm.permission.SEND" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:windowSoftInputMode="adjustNothing" >
<!-- added by disha android:windowSoftInputMode="adjustNothing" > -->
<activity
android:name="com.tcs.hcp_android.SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.tcs.hcp_android.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:showOnLockScreen="true" >
</activity>
<receiver
android:name="com.tcs.hcp_android.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="com.tcs.hcp_android" />
</intent-filter>
</receiver>
<receiver
android:name="com.tcs.hcp_android.NetworkChangeReceiver"
android:label="NetworkChangeReceiver"
android:permission="com.tcs.hcp_android" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="com.tcs.hcp_android.Offline"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:permission="com.tcs.hcp_android"
android:screenOrientation="portrait" >
</activity>
<receiver
android:name="com.tcs.hcp_android.NotificationDismissedReceiver"
android:exported="false" >
</receiver>
<activity
android:name="com.tcs.hcp_android.NotificationSend"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:showOnLockScreen="true" >
</activity>
<service
android:name="com.tcs.hcp_android.GCMNotificationIntentService"
android:exported="false"
android:permission="com.tcs.hcp_android"
android:screenOrientation="portrait"
android:showOnLockScreen="true" />
</application>
答案 0 :(得分:0)
后者似乎相对非凡:为什么我们真的想停止我们想要保持生命的服务?因为如果我们不停止它,该服务将与我们的应用程序一起消失。相反,通过停止服务,我们将强制该服务调用我们自己的Destroy,这将迫使该应用在死机之后重建它。 这是服务:
@Override
public void onDestroy() {
super.onDestroy();
Log.i("EXIT", "ondestroy!");
Intent broadcastIntent = new Intent("ActivityRecognition.RestartSensor");
sendBroadcast(broadcastIntent);
stoptimertask();
}