如何以编程方式锁定/解锁屏幕?
我正在做一个锁定屏幕的应用程序。现在它是锁定的,如果屏幕没有进入必须打开屏幕的服务,它就从那里进入广播接收器。
以下是广播接收器:
public class ScreenReceiver extends BroadcastReceiver { public static boolean wasScreenOn = true; @Override 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 System.out.println("SCREEN_OFF"+wasScreenOn); wasScreenOn = false; Intent i = new Intent(context, UpdateService.class); i.putExtra("screen_state", 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 = true; System.out.println("SCREEN_ON"+wasScreenOn); } }
它进入我写过回家的意图行动的服务是......
ShakeListener mShaker; int amountOfTime = 0; Context context1; @Override public void onCreate() { super.onCreate(); // REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC System.out.println("Enterd Service"); final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); mShaker = new ShakeListener(this); mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () { public void onShake() { vibe.vibrate(100); Intent goHome = new Intent(); goHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); goHome.setAction("android.intent.action.MAIN"); goHome.addCategory("android.intent.category.HOME"); startActivity(goHome); } }); }
它正在进入该服务。但是主屏幕没有显示。调用服务时,屏幕被锁定。
相关分类