java GCM 安卓接收广播意图时出错{act=PATH.DISPLAY_消息flg=0x10(有附加)
我ḿ 解决此问题时遇到问题,消息会到达设备,但当出现通知时,应用程序会因此错误而崩溃->
接收广播意图时出错{act=PATH.DISPLAY_消息flg=0x10(具有额外功能)
我已经发布了部分代码,如果有必要,我可以发布更多
多谢大家
大卫
通知生成
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("安卓.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
留言
@Override
protected void onMessage(Context context, Intent intent) {
if(aController == null)
aController = (Controller) getApplicationContext();
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
aController.displayMessageOnScreen(context, message);
// notifies user
generateNotification(context, message);
}
控制器无效显示消息
// Notifies UI to display a message.
void displayMessageOnScreen(Context context, String message) {
Intent intent = new Intent(Config.DISPLAY_MESSAGE_ACTION);
intent.putExtra(Config.EXTRA_MESSAGE, message);
// Send Broadcast to Broadcast receiver with message
context.sendBroadcast(intent);
}
显示
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="PATH"
安卓:versionCode="1"
安卓:versionName="1.0" >
<uses-sdk
安卓:minSdkVersion="8"
安卓:targetSdkVersion="17" />
<application
安卓:name="PATH.Controller"
安卓:allowBackup="true"
安卓:icon="@drawable/ic_launcher"
安卓:label="@string/app_name"
安卓:theme="@安卓:style/Theme.NoTitleBar.Fullscreen" >
<activity
安卓:name="PATH.MainActivity"
安卓:configChanges="orientation|keyboardHidden"
安卓:label="@string/app_name"
安卓:screenOrientation="portrait" >
</activity>
<receiver
安卓:name="com.google.安卓.gcm.GCMBroadcastReceiver"
安卓:permission="com.google.安卓.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action 安卓:name="com.google.安卓.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action 安卓:name="com.google.安卓.c2dm.intent.REGISTRATION" />
<category 安卓:name="PATH" />
</intent-filter>
</receiver>
<service 安卓:name="PATH.GCMIntentService" />
<activity
安卓:name="PATH.SplashScreen"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
安卓:name="com.google.ads.AdActivity"
安卓:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
<uses-permission 安卓:name="安卓.permission.ACCESS_NETWORK_STATE" />
<!-- GCM connects to Internet Services. -->
<uses-permission 安卓:name="安卓.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission 安卓:name="安卓.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission 安卓:name="安卓.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
安卓:name="PATH.permission.C2D_MESSAGE"
安卓:protectionLevel="signature" />
<uses-permission 安卓:name="PATH.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission 安卓:name="com.google.安卓.c2dm.permission.RECEIVE" />
</manifest>
01-09 22:03:21.315: E/AndroidRuntime(13801): java.lang.RuntimeException: Error receiving broadcast Intent { act=PATH.DISPLAY_MESSAGE flg=0x10 (has extras) } in PATH.MainActivity$1@41a10b28
01-09 22:03:21.315: E/AndroidRuntime(13801): at 安卓.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
01-09 22:03:21.315: E/AndroidRuntime(13801): at 安卓.os.Handler.handleCallback(Handler.java:605)
01-09 22:03:21.315: E/AndroidRuntime(13801): at 安卓.os.Handler.dispatchMessage(Handler.java:92)
01-09 22:03:21.315: E/AndroidRuntime(13801): at 安卓.os.Looper.loop(Looper.java:137)
01-09 22:03:21.315: E/AndroidRuntime(13801): at 安卓.app.ActivityThread.main(ActivityThread.java:4517)
01-09 22:03:21.315: E/AndroidRuntime(13801): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 22:03:21.315: E/AndroidRuntime(13801): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 22:03:21.315: E/AndroidRuntime(13801): at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
01-09 22:03:21.315: E/AndroidRuntime(13801): at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:760)
01-09 22:03:21.315: E/AndroidRuntime(13801): at dalvik.system.NativeStart.main(Native Method)
01-09 22:03:21.315: E/AndroidRuntime(13801): Caused by: java.lang.NullPointerException
01-09 22:03:21.315: E/AndroidRuntime(13801): at PATH.MainActivity$1.onReceive(MainActivity.java:140)
01-09 22:03:21.315: E/AndroidRuntime(13801): at 安卓.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
# 1 楼答案
它适用于此控制器线路