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,因此会误导操作系统和用户拥有FLAG_FOREGROUND_SERVICE
  • FLAG_FOREGROUND_SERVICEFLAG_AUTO_CANCEL组合起来毫无意义

但是,我不希望这会导致MyApp自动启动。事实上,AFAIK,在没有用户点击它的情况下Notification不会自动调用它。PendingIntent我怀疑您的真正问题出在其他地方。

于 2011-03-18T13:22:55.783 回答