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(); // Log.d(TAG, "onCreate()"); // initNotification(); // initTodayData(); initBroadcastReceiver(); // new Thread(new Runnable() { // public void run() { // startStepDetector(); // } // }).start(); // startTimeCount(); } /** * 注册广播 */ 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_SCREEN_OFF.equals(action)) { Log.i(TAG, "screen off"); //改为60秒一存储 // duration = 60000; } else if (Intent.ACTION_USER_PRESENT.equals(action)) { Log.i(TAG, "screen unlock"); // save(); //改为30秒一存储 // duration = 30000; } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) { Log.i(TAG, " receive Intent.ACTION_CLOSE_SYSTEM_DIALOGS"); //保存一次 // save(); } else if (Intent.ACTION_SHUTDOWN.equals(intent.getAction())) { Log.i(TAG, " receive ACTION_SHUTDOWN"); // save(); } else if (Intent.ACTION_DATE_CHANGED.equals(action)) {//日期变化步数重置为0 // Logger.d("重置步数" + StepDcretor.CURRENT_STEP); // save(); // isNewDay(); } else if (Intent.ACTION_TIME_CHANGED.equals(action)) { //时间变化步数重置为0 Log.i("lgq000000000000","sssss时间变化步数重置为0===="+action); // isCall(); // save(); // isNewDay(); } else if (Intent.ACTION_TIME_TICK.equals(action)) {//日期变化步数重置为0 Log.i("lgq0000000000000000","日期变化步数重置为0===="+action); // isCall(); // Logger.d("重置步数" + StepDcretor.CURRENT_STEP); // save(); // isNewDay(); } } }; 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); }