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 に答える