我ḿ解决此问题,消息到达了设备,但是当它出现通知时,应用程序因此错误而崩溃->
接收广播Intent时出错{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);
}
控制器无效显示消息
// 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)
最佳答案
它可以对控制器线进行注释。
@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);
}