android 闹钟后台实现,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

* 邮箱:13164716840@163.com

* 日期: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;

}

}

/**

* 开启计步服务

*/

private void setupService() {

Intent intent = new Intent(this, Serview.class);

// isBind = bindService(intent, conn, Context.BIND_AUTO_CREATE);

startService(intent);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值