以下是一个使用 Android 实现久坐提醒的示例代码,每隔 30 分钟弹出一个弹窗提醒用户起身活动。

  1. 创建一个名为 'MainActivity' 的 Activity 类:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;

public class MainActivity extends Activity {

    private Handler mHandler;
    private Runnable mRunnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 创建一个 Handler 来处理定时任务
        mHandler = new Handler();

        // 创建一个 Runnable,用于显示提醒对话框
        mRunnable = new Runnable() {
            @Override
            public void run() {
                showReminderDialog();
            }
        };

        // 启动定时任务,每隔 30 分钟执行一次
        startReminderTask();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 停止定时任务
        stopReminderTask();
    }

    private void startReminderTask() {
        mHandler.postDelayed(mRunnable, 30 * 60 * 1000); // 30分钟
    }

    private void stopReminderTask() {
        mHandler.removeCallbacks(mRunnable);
    }

    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();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        dialog.show();
    }
}
  1. 在 'res' 目录下创建一个名为 'layout' 的文件夹,并在该文件夹下创建一个名为 '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:paddingBottom='@dimen/activity_vertical_margin'
    android:paddingLeft='@dimen/activity_horizontal_margin'
    android:paddingRight='@dimen/activity_horizontal_margin'
    android:paddingTop='@dimen/activity_vertical_margin'
    tools:context='.MainActivity'>

    <!-- 此处添加其他布局元素 -->

</RelativeLayout>
  1. 在 'AndroidManifest.xml' 文件中添加以下权限和声明:
<uses-permission android:name='android.permission.SYSTEM_ALERT_WINDOW' />

<application
    <!-- 此处添加其他声明 -->
    android:theme='@android:style/Theme.Translucent.NoTitleBar'>
    <activity android:name='.MainActivity'>
        <intent-filter>
            <action android:name='android.intent.action.MAIN' />

            <category android:name='android.intent.category.LAUNCHER' />
        </intent-filter>
    </activity>
</application>

以上代码会在每隔 30 分钟弹出一个久坐提醒的对话框,并在屏幕上显示。请注意,由于 Android 6.0 及以上版本的权限限制,需要在应用的设置中手动开启 "悬浮窗" 权限,否则无法显示提醒对话框。

希望以上代码对您有所帮助!

Android 久坐提醒实现:弹窗提醒代码示例

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

免费AI点我,无需注册和登录