0

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

当我打开 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());
4

3 回答 3

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);
于 2013-02-06T12:51:38.843 回答
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

于 2013-02-06T12:51:05.480 回答
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);

  }});
于 2013-02-06T12:59:20.233 回答