Android久坐提醒功能实现:完整代码及详细教程
Android久坐提醒功能实现:完整代码及详细教程
如今,人们长时间使用手机或电脑办公学习,久坐不动已成为普遍现象,这对健康造成了一定的危害。为了提醒用户注意起身活动,很多应用都加入了久坐提醒功能。
本文将介绍如何使用Android实现一个简单的久坐提醒功能,并提供完整可运行的代码示例。
功能介绍
本示例程序的功能如下:
- 点击'Start Sit Reminder'按钮,启动久坐提醒功能。2. 程序会在后台计时,例如设定为60秒。3. 计时结束后,会在屏幕上弹出一个通知,提醒用户休息。4. 点击'Stop Sit Reminder'按钮,可以停止提醒功能。
实现步骤
1. 创建Android项目
首先,在Android Studio中创建一个新的Android项目。
2. 添加权限
在AndroidManifest.xml文件中添加震动权限:xml
3. 设计布局文件
创建布局文件activity_main.xml,添加两个按钮,分别用于启动和停止久坐提醒:xml
<Button android:id='@+id/start_button' android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='Start Sit Reminder' />
<Button android:id='@+id/stop_button' android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='Stop Sit Reminder' />
4. 创建通知图标
在res/drawable目录下创建一个名为ic_notification.xml的矢量图文件,用于通知栏图标:xml
5. 编写Java代码
最后,编写MainActivity.java文件,实现久坐提醒的逻辑:javaimport android.app.NotificationChannel;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.os.Build;import android.os.Bundle;import android.os.CountDownTimer;import android.view.View;import android.widget.Button;import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;import androidx.core.app.NotificationCompat;import androidx.core.app.NotificationManagerCompat;
public class MainActivity extends AppCompatActivity {
private static final String CHANNEL_ID = 'sit_reminder_channel'; private static final int NOTIFICATION_ID = 1;
private Button startButton; private Button stopButton; private CountDownTimer countDownTimer;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
createNotificationChannel();
startButton = findViewById(R.id.start_button); stopButton = findViewById(R.id.stop_button);
startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startSitReminder(); } });
stopButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopSitReminder(); } }); }
private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = getString(R.string.channel_name); String description = getString(R.string.channel_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); } }
private void startSitReminder() { countDownTimer = new CountDownTimer(60000, 1000) { @Override public void onTick(long millisUntilFinished) { // Nothing to do on tick }
@Override public void onFinish() { showNotification(); } }.start();
Toast.makeText(this, 'Sit reminder started', Toast.LENGTH_SHORT).show(); }
private void stopSitReminder() { if (countDownTimer != null) { countDownTimer.cancel(); }
cancelNotification();
Toast.makeText(this, 'Sit reminder stopped', Toast.LENGTH_SHORT).show(); }
private void showNotification() { Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle('Sit Reminder') .setContentText('You have been sitting for a long time. Take a break and stretch!') .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) .setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(NOTIFICATION_ID, builder.build()); }
private void cancelNotification() { NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.cancel(NOTIFICATION_ID);
原文地址: https://www.cveoy.top/t/topic/QqT 著作权归作者所有。请勿转载和采集!