android - 状态栏通知自动启动 Activity

标签 android android-activity notifications scheduled-tasks statusbar

我正在尝试进行预定通知。所有工作除外:当应用程序处于 Activity 状态并最小化时。通知自动启动 Activity,无需等待用户点击它。

收到时:

 public void onReceive(Context context, Intent paramIntent) {

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
        Notification notification = new Notification(R.drawable.logo_f, context.getResources().getString(R.string.notification_text), System.currentTimeMillis());

        Intent notificationIntent = new Intent(context, TimeLeftActivity.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);

        notification.setLatestEventInfo(context, context.getResources().getString(R.string.notification_text), "", intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.sound=alarmSound;
        // Fire the notification
        notificationManager.notify(1, notification);


    }

我的通知启动方式:

 private void createScheduledNotification(int sec)
    {
        // Get new calendar object and set the date to now
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        // Add defined amount of days to the date
        calendar.add(Calendar.SECOND, sec);

        // Retrieve alarm manager from the system
        AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);

        // Every scheduled intent needs a different ID, else it is just executed once
        int id = 1;

        // Prepare the intent which should be launched at the date
        Intent intent = new Intent(this, TimeAlarm.class);

        // Prepare the pending intent
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.cancel(pendingIntent);
        // Register the alert in the system. You have the option to define if the device has to wake up on the alert or not
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }

在基里尔回答后编辑。错误仍然存​​在。通知自动启动挂起的 Intent ,不等待点击。

  @Override
    public void onReceive(Context context, Intent paramIntent) {

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();


        Intent notificationIntent = new Intent(context, TimeLeftActivity.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(context)
                .setContentTitle(context.getResources().getString(R.string.notification_text))
                .setContentIntent(intent)
                .setSound(alarmSound)
                .build();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Fire the notification
        notificationManager.notify(1, notification);


    }

最佳答案

很难发现错误,因为您在代码中使用了已弃用的 API,您应该使用 Notication.Builder

 Notification noti = new Notification.Builder(mContext)
     .setContentTitle("New mail from " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_mail)
     .setLargeIcon(aBitmap)
     .build();

如果您需要支持旧版本,您可以使用 NotificationCompat

更新

这是我的应用程序示例,它抛出一个通知,通过点击打开 Activity ,我标记了添加 Intent 的方法。

    String message = context.getString(R.string.notif_message);
    Intent notificationIntent = new Intent(AddBpRecordActivity.ADD_ACTION);

    NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_notif_logo)
            .setContentTitle(message)
            .setContentText(billet.comment)
            .setDefaults(Notification.DEFAULT_ALL)
            .setAutoCancel(true)
      >>>   .setContentIntent(PendingIntent.getActivity(context, (int) billet.id, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT))
            .setWhen(System.currentTimeMillis());
    Notification notification = nb.build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify((int) billet.id, notification);

关于android - 状态栏通知自动启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28352171/

相关文章:

android - 处理具有更高分辨率的 Android 设备的屏幕,例如 Samsung Tab

java - 如何从服务器获得响应?

android - 如何确保我的 Android 应用程序始终在主要 Activity 上启动,即使它崩溃了?

iphone - 在后台模式下使用 App 接收通知

java - 在 Activity 中启动前台服务

android - Lint 和旧 API 级别警告

android - 在android中更改微调器上小三角形的颜色

Android:是否可以阻止特定 Activity 处理 Intent ?

android - 在 Activity 停止和恢复时保存 SurfaceView 的状态

android - 我可以在应用关闭时更改推送通知的声音吗?