我有麻烦来解决这个问题,邮件会到达设备,但是当它出现通知时,应用程序会因此错误而崩溃 - >
接收广播意图错误{act = PATH.DISPLAY_MESSAGE 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("android.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);
}
CONTROLLER VOID显示消息
// 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:android="http://schemas.android.com/apk/res/android"
package="PATH"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:name="PATH.Controller"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<activity
android:name="PATH.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="PATH" />
</intent-filter>
</receiver>
<service android:name="PATH.GCMIntentService" />
<activity
android:name="PATH.SplashScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="PATH.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="PATH.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.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 android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
01-09 22:03:21.315: E/AndroidRuntime(13801): at android.os.Handler.handleCallback(Handler.java:605)
01-09 22:03:21.315: E/AndroidRuntime(13801): at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 22:03:21.315: E/AndroidRuntime(13801): at android.os.Looper.loop(Looper.java:137)
01-09 22:03:21.315: E/AndroidRuntime(13801): at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
01-09 22:03:21.315: E/AndroidRuntime(13801): at com.android.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 android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
答案 0 :(得分:0)
它可以对这些控制器线进行操作。
@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);
}