好吧,我快到了。我收到了通知,但想要打開該應用並在單擊通知時所用的web視圖中加載一個URL。我擁有的代碼如下。當我點擊通知時,它會打開webview活動,但它會崩潰並且不會加載頁面。從通知中打開newInent和loadUrl
這是我的通知
private void sendGCMIntent(final Context theContext, Intent theMsg)
{
int icon = R.drawable.noti_msg, count;
long when = Calendar.getInstance().getTimeInMillis();
String msg = theMsg.getStringExtra("message");
String url = theMsg.getStringExtra("photo");
count = Integer.parseInt(theMsg.getStringExtra("count"));
Bitmap photo = getBitmapFromURL(url);
String nameEncode = theMsg.getStringExtra("name");
String theMessage = Html.fromHtml(msg).toString().replace("\\", "");
String name = Html.fromHtml(nameEncode).toString();
String title = theContext.getString(R.string.app_name);
Uri soundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.msg);
long[] vibraPattern = { 0, 500, 250, 500 };
Intent notificationIntent = new Intent(theContext,MainActivity.class);
notificationIntent.setData(Uri.parse("content://"+when));
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.addCategory("android.intent.category.LAUNCHER");
PendingIntent contentIntent = PendingIntent.getActivity(theContext, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager notificationManager = (NotificationManager) theContext.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(theContext);
notification.setContentTitle(title);
notification.setContentText("New message from: "+ name);
notification.setSubText(theMessage);
notification.setContentIntent(contentIntent);
notification.setTicker("New message");
notification.setNumber(count);
notification.setSmallIcon(icon);
notification.setLargeIcon(photo);
notification.setAutoCancel(true);
notification.setSound(soundUri);
notification.setVibrate(vibraPattern);
notification.setWhen(when);
Notification notifies = notification.build();
notificationManager.notify((int)when, notifies);
}
而在我的MainActivity我有這個
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if(intent.getStringExtra("type").equals("message"))
{
String mUrl = intent.getStringExtra("mid");
webView.loadUrl(mUrl);
}
}
下面是錯誤日誌
04-15 07:33:41.061: E/AndroidRuntime(19851): FATAL EXCEPTION: main
04-15 07:33:41.061: E/AndroidRuntime(19851): java.lang.NullPointerException
04-15 07:33:41.061: E/AndroidRuntime(19851): at me.site.test.MainActivity.onNewIntent(MainActivity.java:230)
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1168)
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2181)
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.ActivityThread.performNewIntents(ActivityThread.java:2194)
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2203)
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.ActivityThread.access$1500(ActivityThread.java:139)
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1295)
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.os.Looper.loop(Looper.java:137)
04-15 07:33:41.061: E/AndroidRuntime(19851): at android.app.ActivityThread.main(ActivityThread.java:4918)
04-15 07:33:41.061: E/AndroidRuntime(19851): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 07:33:41.061: E/AndroidRuntime(19851): at java.lang.reflect.Method.invoke(Method.java:511)
04-15 07:33:41.061: E/AndroidRuntime(19851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
04-15 07:33:41.061: E/AndroidRuntime(19851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
04-15 07:33:41.061: E/AndroidRuntime(19851): at dalvik.system.NativeStart.main(Native Method)
請包括您的logcat輸出。你說它崩潰了,但是你不會向我們展示它在崩潰時拋出的異常。 –