我正在开发一个应用程序,它将锁定屏幕,它包含一个主要活动,有两个按钮来启动和停止应用程序。 之后它进入广播接收器,检查屏幕是打开还是关闭,然后重定向到服务。对于前3或4次摇动锁定并在其无限期执行后完全解锁其工作。
我正在使用的广播接收器是..
public void onReceive(Context context, Intent intent) {
System.out.println("Entered Broadcaste Reciever");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// DO WHATEVER YOU NEED TO DO HERE
wasScreenOn = "1";
System.out.println("Check"+"Screen went OFF");
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screenstate", wasScreenOn);
context.startService(i);
System.out.println("jrkejhr keh");
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// AND DO WHATEVER YOU NEED TO DO HERE
wasScreenOn = "2";
Log.i("Check","Screen went ONN");
System.out.println("Entering to the service.................");
Intent ScreenOnIntent = new Intent(context,UpdateService.class);
ScreenOnIntent.putExtra("screenstate", wasScreenOn);
context.startService(ScreenOnIntent);
}
}
,服务是.....
public void onStart(Intent intent, int startId) {
System.out.println("Entered On Start");
data=intent.getStringExtra("screenstate");
System.out.println("Data from Broadcast Receiver is"+data);
mShaker = new ShakeListener(this);
mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () {
public void onShake()
{
if(data.equals("1"))
{
System.out.println(".............................................");
PowerManager TempPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock TempWakeLock = TempPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE, "TempWakeLock");
TempWakeLock.acquire();
final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibe.vibrate(100);
}
else
{
final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
System.out.println("The screen is off and is shaking.........");
vibe.vibrate(100);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
ComponentName mAdminName = new ComponentName(UpdateService.this,LockActivity.class);
System.out.println("The Device device admin enabled");
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"onEnabled");
mDPM.lockNow();
mDPM.setMaximumTimeToLock(mAdminName,0);
intent.putExtra("force-locked", DeviceAdminInfo.USES_POLICY_FORCE_LOCK);
// startActivityForResult(intent, 1);
}
}
});
}
我只想执行一次..如果屏幕执行一次并关闭屏幕,反之亦然..