MainActivity崩溃与状态栏通知

时间:2013-02-06 12:37:18

标签: android

我研究了这个,这个代码就是我得到的

当我打开主要活动时,应用程序崩溃

 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());

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

试试这个,给出上下文名称getSystemmService(Context.NOTIFICATION_SERVICE);。        notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);         myNotification =新通知(R.drawable.ic_launcher,                 “通知!”,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);

答案 2 :(得分: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);

  }});