0

ScheduleAlert というアクティビティ クラスを呼び出すアラーム マネージャがあります。

public class ScheduleAlert extends ActivityGroup {

   private String notificationAlart, editEventid;

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                ...........
                ..........
                // ************* Notification ************//
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        final Notification notifyDetails = new Notification(R.drawable.icon, "Myapp", nextAlarmTime);

        Context context = getApplicationContext();
        CharSequence contentTitle = "Myapp";
        CharSequence contentText = notificationAlart;

        Intent notifyIntent = new Intent(context, MyApp.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(ScheduleAlert.this, 0, notifyIntent,android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
        notifyDetails.setLatestEventInfo(context, contentTitle, contentText,pendingIntent);

            notifyDetails.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_AUTO_CANCEL;
    notifyDetails.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
            mNotificationManager.notify((int) editEventid, notifyDetails);

        // ************* Notification ************//
        this.finish();
      }

      public void onDestroy(){
        super.onDestroy();

      }

}

通知メッセージをタップしたときに、MyApp アクティビティのインデントが発生するようにします。通知時は音とバイブだけでいい。しかし、今では音と振動が発生しており、実際には望んでいない MyApp アクティビティも発生しています。私のコードの問題は何ですか?

4

1 に答える 1

1

このコードには、奇妙なことがたくさんあります。

  • ActivityGroupこのコードを拡張している理由がわかりません
  • getApplicationContext()このようなほとんどの状況では使用しないでください
  • これはActivityGroup(何らかの理由で) でありService、 ではないため、OS とユーザーに誤解を与える可能性があります。FLAG_FOREGROUND_SERVICE
  • FLAG_FOREGROUND_SERVICE組み合わせてFLAG_AUTO_CANCELもほとんど意味がない

MyAppただし、これにより自動的に開始されるとは思いません。実際、知る限り、ユーザーがタップしなくてもNotification自動的に呼び出される状況はありません。PendingIntentあなたの本当の問題は別のところにあると思います。

于 2011-03-18T13:22:55.783 に答える