Android 闹钟,实现demo

可变时间格式:"yyyy-MM-dd HH:mm:ss"

保存闹钟时间:

  • SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式

  • String thistime = df.format(new Date());

  • SharedPreferences sharedPreferences = getSharedPreferences("industryInfo", Context.MODE_PRIVATE); //私有数据

  • SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器

  • editor.putString(“retime”, thistime);//名称 id

  • editor.commit();//提交

监听时间变化:

监听service

/**
 * 作者:created by meixi
 * 邮箱:[email protected]
 * 日期:2018/9/27 09
 */
public class Serview extends Service {

    /**
     * 广播接受者
     */
    private BroadcastReceiver mBatInfoReceiver;
    private String TAG = "lgq--------------------";
    @Override
    public void onCreate() {
        super.onCreate();
        initBroadcastReceiver();
    }


    /**
     * 注册广播
     */
    private void initBroadcastReceiver() {
        final IntentFilter filter = new IntentFilter();
        // 屏幕灭屏广播
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        //关机广播
        filter.addAction(Intent.ACTION_SHUTDOWN);
        // 屏幕亮屏广播
        filter.addAction(Intent.ACTION_SCREEN_ON);
        // 屏幕解锁广播
//        filter.addAction(Intent.ACTION_USER_PRESENT);
        // 当长按电源键弹出“关机”对话或者锁屏时系统会发出这个广播
        // example:有时候会用到系统对话框,权限可能很高,会覆盖在锁屏界面或者“关机”对话框之上,
        // 所以监听这个广播,当收到时就隐藏自己的对话,如点击pad右下角部分弹出的对话框
        filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        //监听日期变化
        filter.addAction(Intent.ACTION_DATE_CHANGED);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIME_TICK);

        mBatInfoReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(final Context context, final Intent intent) {
                String action = intent.getAction();
                if (Intent.ACTION_SCREEN_ON.equals(action)) {
                    Log.i(TAG, "screen on");
                } else if (Intent.ACTION_TIME_TICK.equals(action)) {//日期变化步数重置为0
                    Log.i("lgq0000000000000000","日期变化步数重置为0===="+action);
                    SharedPreferences share = getSharedPreferences("industryInfo", Activity.MODE_PRIVATE);
                    String industryOne = share.getString("retime", "");//名称 获取id

                    if (industryOne.equals(new SimpleDateFormat("HH:mm").format(new Date()))){
                        //todo   闹钟到了
                    }

                }
            }
        };
        registerReceiver(mBatInfoReceiver, filter);
    }




    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
<service android:name=".testt.Serview">

    <intent-filter>
        <!-- 系统启动完成后会调用-->
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.DATE_CHANGED" />
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <action android:name="android.intent.action.USER_PRESENT" />
        <action android:name="android.intent.action.ACTION_TIME_TICK" />
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
    </intent-filter>
</service>
    /**
     * 开启计步服务
     */
    private void setupService() {
        Intent intent = new Intent(this, Serview.class);
//        isBind = bindService(intent, conn, Context.BIND_AUTO_CREATE);
        startService(intent);
    }

猜你喜欢

转载自blog.csdn.net/meixi_android/article/details/82866405
今日推荐