在Android中创建按键点击开始特定时间间隔的重复通知和点击停止通知可以通过以下步骤完成:
下面是一个示例代码,用于实现上述功能:
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button startButton;
private Button stopButton;
private NotificationManager notificationManager;
private static final int NOTIFICATION_ID = 1;
private static final String CHANNEL_ID = "channel_id";
private static final String CHANNEL_NAME = "channel_name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = findViewById(R.id.startButton);
stopButton = findViewById(R.id.stopButton);
// 创建通知渠道
createNotificationChannel();
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 设置通知点击事件的Intent
Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);
// 创建通知
Notification notification = new Notification.Builder(MainActivity.this, CHANNEL_ID)
.setContentTitle("重复通知")
.setContentText("点击了开始按钮")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.build();
// 设置定时任务
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
long intervalMillis = 1000 * 60 * 60; // 1小时
long triggerAtMillis = SystemClock.elapsedRealtime() + intervalMillis;
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, intervalMillis, pendingIntent);
// 发送通知
notificationManager.notify(NOTIFICATION_ID, notification);
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 取消定时任务
Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
// 取消通知
notificationManager.cancel(NOTIFICATION_ID);
}
});
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
}
public static class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 接收到通知的广播消息
}
}
}
这样,当用户点击开始按钮时,将会创建一个重复通知,并每隔1小时触发一次。点击停止按钮时,将会取消定时任务并取消通知。
推荐的腾讯云相关产品是腾讯云移动推送服务(TPNS),它可以帮助开发者实现Android设备上的消息推送功能。了解更多关于腾讯云移动推送服务的信息,请参考腾讯云官网的介绍页面:腾讯云移动推送服务。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云