Я использую BroadCastReceiver для прослушивания сообщений GCM. Я хочу, чтобы мое уведомление приходило, даже когда приложение убито, а телефон заблокирован. Но onReceive моего широковещательного приемника не срабатывает, когда приложение убито, хотя оно работает правильно, когда приложение находится в фоновом режиме. Я объявил свой BroadCastReceiver в AndroidManifest.xml и не отменяю регистрацию широковещательного приемника вручную в любом месте приложения.
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>