2013-02-06 119 views
0

我reasearched這個,還有這個代碼是我得到MainActivity崩潰隨着狀態欄通知

應用崩潰,當我打開在MainActivity

NotificationCompat.Builder builder = 
    new NotificationCompat.Builder(MainActivity.this) 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setContentTitle("Get Other APW Co. Apps on Play!") 
    .setContentText("Want more? All our apps are free!"); 
    int mNotificationId = 001; 
    NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemmService(NOTIFICATION_SERVICE); 
    mNotifyMgr.notify(mNotificationId, builder.build()); 
+1

郵政logcat的崩潰報告。在當前代碼中更多的上下文會很好。 –

回答

0

您必須指定contentIntent,即PendingIntent那將在項目被點擊時執行。 這是強制性的,,你沒有指定它導致錯誤。

您可以在構建器或上做到這一點Notification

  • 在建設者:

    builder.setContentIntent(contentIntent); 
    Notification n = builder.build(); 
    
  • 的通知:

    Notification n = builder.build(); 
    n.contentIntent = contentIntent; 
    

只能後你把它發送到NotificationManager

mNotifyMgr.notify(mNotificationId, n); 

確切contentIntent值取決於你想要做什麼。看到這裏引用: http://developer.android.com/reference/android/app/Notification.html#contentIntent

看到這裏的工作例如:https://github.com/nheid/unitedcoders-android/blob/master/src/com/unitedcoders/android/examples/download/DownloadProgress.java

0

試試這個,你給上下文名稱getSystemmService(Context.NOTIFICATION_SERVICE);。 notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); myNotification = new Notification(R.drawable.ic_launcher, 「Notification!」,System.currentTimeMillis());

Context context = getApplicationContext(); 

    String notificationTitle = "App Name"; 
    String notificationText = Msg; 
    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog), 
      context, MainActivity.class); 
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION 
      | Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 
      0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

    myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
    myNotification.defaults |= Notification.DEFAULT_SOUND; 
    myNotification.flags |= Notification.FLAG_AUTO_CANCEL; 
    myNotification.setLatestEventInfo(context, notificationTitle, 
      notificationText, pendingIntent); 
    notificationManager.notify(MY_NOTIFICATION_ID, myNotification); 
0

這將工作:

notificationManager = 
    (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
    myNotification = new Notification(R.drawable.ic_launcher, 
    "Notification!", 
    System.currentTimeMillis()); 
    Context context = getApplicationContext(); 
    String notificationTitle = "Get Other APW Co. Apps on Play!"; 
    String notificationText = ""Want more? All our apps are free!""; 
    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(SOME_URL)); 
    PendingIntent pendingIntent 
    = PendingIntent.getActivity(AndroidNotification.this, 
     0, myIntent, 
     Intent.FLAG_ACTIVITY_NEW_TASK); 
    myNotification.defaults |= Notification.DEFAULT_SOUND; 
    myNotification.flags |= Notification.FLAG_AUTO_CANCEL; 
    myNotification.setLatestEventInfo(context, 
     notificationTitle, 
     notificationText, 
     pendingIntent); 
    notificationManager.notify(1, myNotification); 

    }}); 
+0

應用程序仍然崩潰 – user2039764