Android 久坐提醒实现:弹窗通知与完整代码示例
以下是一个使用Android实现久坐提醒的示例代码,该代码会通过弹窗和通知提醒用户起身活动。
MainActivity.java
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
public class MainActivity extends AppCompatActivity {
private static final int REMINDER_INTERVAL = 60 * 60 * 1000; // 1小时提醒一次
private static final int NOTIFICATION_ID = 1;
private static final String CHANNEL_ID = "Sedentary Reminder";
private Handler handler;
private Runnable reminderRunnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建Notification Channel(适用于Android 8.0以上版本)
createNotificationChannel();
// 初始化Handler和Runnable
handler = new Handler();
reminderRunnable = new Runnable() {
@Override
public void run() {
showReminderDialog();
}
};
// 启动定时提醒
startReminder();
}
@Override
protected void onDestroy() {
super.onDestroy();
// 停止定时提醒
stopReminder();
}
private void startReminder() {
handler.postDelayed(reminderRunnable, REMINDER_INTERVAL);
}
private void stopReminder() {
handler.removeCallbacks(reminderRunnable);
}
private void showReminderDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle('久坐提醒');
builder.setMessage('您已经连续坐了很长时间了,请起身活动一下。');
builder.setPositiveButton('确定', new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
startReminder(); // 继续下一次提醒
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
startReminder(); // 继续下一次提醒
}
});
builder.show();
// 显示Notification
showNotification();
}
private void showNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle('久坐提醒')
.setContentText('您已经连续坐了很长时间了,请起身活动一下。')
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = '久坐提醒';
String description = '久坐提醒通知';
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:padding="16dp"
tools:context=".MainActivity">
<!-- 添加其他界面元素 -->
</RelativeLayout>
ic_notification.xml (放在res/drawable目录下)
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#000000"
android:pathData="M12,2L10.59,3.41 5.17,8.83 4.41,8.07 3,9.46V12c0,6.63 5.37,12 12,12s12,-5.37 12,-12S18.63,0 12,0L12,2zM11,17h2v2h-2v-2zM11,7h2v6h-2V7z"/>
</vector>
请确保将上述代码中的R.drawable.ic_notification
替换为您自己的通知图标。此外,还需要在AndroidManifest.xml中添加必要的权限和声明MainActivity。
注意:
- 代码中使用了
REMINDER_INTERVAL
来设置提醒的间隔时间,默认值为1小时。您可以根据需要修改该值。 - 在AndroidManifest.xml中添加
android.permission.VIBRATE
权限,以允许震动提醒。 - 在AndroidManifest.xml中添加
android.permission.RECEIVE_BOOT_COMPLETED
权限,以允许在设备启动时启动定时提醒服务。 - 该代码仅供参考,您可能需要根据您的实际需求进行调整。

原文地址: http://www.cveoy.top/t/topic/Qq5 著作权归作者所有。请勿转载和采集!