首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

当点击通知时,转到Mainactivity并恢复任务(Countdown timer应继续) Android Studio

当点击通知时,转到MainActivity并恢复任务(Countdown Timer应继续)需要进行以下步骤:

  1. 创建一个通知:
    • 使用NotificationCompat.Builder类创建一个通知构建器。
    • 设置通知的标题、内容、图标等属性。
    • 设置通知的点击行为,即点击通知时要启动的Activity。
  • 在MainActivity中处理通知点击事件:
    • 在MainActivity的onCreate方法中获取Intent对象。
    • 检查Intent中是否包含特定的标志,以确定是否是从通知启动的。
    • 如果是从通知启动的,则获取之前保存的任务状态,如倒计时的剩余时间。
    • 根据任务状态进行相应的处理,如恢复倒计时。
  • 保存任务状态:
    • 在MainActivity的onPause方法中保存任务状态,如倒计时的剩余时间。
    • 使用SharedPreferences或其他持久化方式将任务状态保存起来。

以下是一个示例代码,演示了如何实现上述功能:

代码语言:txt
复制
// 创建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("任务通知")
        .setContentText("点击通知返回任务")
        .setAutoCancel(true);

// 设置通知的点击行为
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);

// 发送通知
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());

// 在MainActivity中处理通知点击事件
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 检查是否是从通知启动的
    Intent intent = getIntent();
    if (intent != null && (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
        // 获取之前保存的任务状态
        SharedPreferences sharedPreferences = getSharedPreferences("TaskState", Context.MODE_PRIVATE);
        long remainingTime = sharedPreferences.getLong("RemainingTime", 0);

        // 根据任务状态进行处理,如恢复倒计时
        if (remainingTime > 0) {
            // 创建倒计时
            CountdownTimer countdownTimer = new CountdownTimer(remainingTime, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    // 更新倒计时显示
                }

                @Override
                public void onFinish() {
                    // 倒计时结束处理
                }
            };

            // 启动倒计时
            countdownTimer.start();
        }
    }
}

// 在MainActivity中保存任务状态
@Override
protected void onPause() {
    super.onPause();

    // 保存任务状态
    SharedPreferences sharedPreferences = getSharedPreferences("TaskState", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putLong("RemainingTime", countdownTimer.getRemainingTime());
    editor.apply();
}

在这个示例中,我们使用了NotificationCompat.Builder类创建了一个通知,并设置了通知的标题、内容、图标等属性。然后,我们创建了一个Intent对象,并设置了特定的标志,以便在MainActivity中判断是否是从通知启动的。接下来,我们使用PendingIntent将Intent与通知关联起来,当用户点击通知时,会启动MainActivity,并传递相应的Intent。最后,我们使用NotificationManager发送了通知。

在MainActivity中,我们在onCreate方法中获取了之前保存的任务状态,并根据任务状态进行相应的处理,如恢复倒计时。在onPause方法中,我们保存了任务状态,以便在下次启动时能够恢复任务。

请注意,这只是一个示例代码,实际应用中可能需要根据具体需求进行适当的修改和扩展。另外,为了简化示例,我们没有包含倒计时的具体实现,你需要根据自己的需求来实现倒计时的逻辑。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

Android开发笔记(五十)定时器AlarmManager

Java中的定时器机制有现成的方案,就是Timer+TimerTask。其中TimerTask用来描述时刻到达后的事务处理,而Timer用来调度定时任务,如何时启动、间隔多久再次运行等等。 Timer的调度方法是schedule,主要有三个参数。第一个参数表示用来调度的定时任务,第二个参数表示延迟多久首次启动任务,第三个参数表示间隔多久再次启动任务。 public void schedule(TimerTask task, long delay, long period) 定时任务得自己写个继承自TimerTask的新类,并重写run方法填入具体的事务处理代码。调用Timer的schedule方法,定时任务便会按照调度设置按时启动;TimerTask不能直接设置运行的次数上限,一旦启动就会持续定时运行,除非对象销毁或者调用了TimerTask的cancel方法。调用cancel方法停止定时任务后,若想重启该定时任务,只能重新声明TimerTask对象,并且重新调用schedule方法。 Timer+TimerTask的实质是利用开启Thread来触发定时任务,所以TimerTask实际上运行于非UI线程,也就无法直接操作UI。若想在TimerTask中修改UI控件,得通过Handler发送消息来间接实现。

01
领券