我正在开发一个应用程序,它将在摇动时锁定屏幕,它由一个主要活动组成,该活动有两个按钮来启动和停止应用程序。之后,它进入广播接收器,该接收器正在检查屏幕是打开还是关闭,然后重定向到服务。对于前 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);
}
}
});
}
我只想执行一次..如果屏幕打开执行一次并关闭屏幕,反之亦然..