如何以编程方式locking/解锁屏幕?

我正在做一个应用程序,在摇动locking屏幕。 现在它正在locking,从那里进入一个广播接收机,如果屏幕没有进入一个必须开启屏幕的服务。

以下是广播接收机:

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); } }); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } 

它正在进入服务。 但主屏幕不显示。 当服务被调用时,屏幕被locking。

Solutions Collecting From Web of "如何以编程方式locking/解锁屏幕?"

编辑:

由于有些人在编程locking后需要解锁设备的帮助,我通过编程方式来发布Android的屏幕locking/解锁 ,请看看,可以帮助你。

原来的答案是:

你需要获得pipe理员权限,你可以locking手机屏幕

请检查下面的简单教程来实现这一个

locking手机屏幕编程

这里也是代码示例..

LockScreenActivity.java

 public class LockScreenActivity extends Activity implements OnClickListener { private Button lock; private Button disable; private Button enable; static final int RESULT_ENABLE = 1; DevicePolicyManager deviceManger; ActivityManager activityManager; ComponentName compName; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); deviceManger = (DevicePolicyManager)getSystemService( Context.DEVICE_POLICY_SERVICE); activityManager = (ActivityManager)getSystemService( Context.ACTIVITY_SERVICE); compName = new ComponentName(this, MyAdmin.class); setContentView(R.layout.main); lock =(Button)findViewById(R.id.lock); lock.setOnClickListener(this); disable = (Button)findViewById(R.id.btnDisable); enable =(Button)findViewById(R.id.btnEnable); disable.setOnClickListener(this); enable.setOnClickListener(this); } @Override public void onClick(View v) { if(v == lock){ boolean active = deviceManger.isAdminActive(compName); if (active) { deviceManger.lockNow(); } } if(v == enable){ Intent intent = new Intent(DevicePolicyManager .ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, compName); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Additional text explaining why this needs to be added."); startActivityForResult(intent, RESULT_ENABLE); } if(v == disable){ deviceManger.removeActiveAdmin(compName); updateButtonStates(); } } private void updateButtonStates() { boolean active = deviceManger.isAdminActive(compName); if (active) { enable.setEnabled(false); disable.setEnabled(true); } else { enable.setEnabled(true); disable.setEnabled(false); } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case RESULT_ENABLE: if (resultCode == Activity.RESULT_OK) { Log.i("DeviceAdminSample", "Admin enabled!"); } else { Log.i("DeviceAdminSample", "Admin enable FAILED!"); } return; } super.onActivityResult(requestCode, resultCode, data); } } 

MyAdmin.java

 public class MyAdmin extends DeviceAdminReceiver{ static SharedPreferences getSamplePreferences(Context context) { return context.getSharedPreferences( DeviceAdminReceiver.class.getName(), 0); } static String PREF_PASSWORD_QUALITY = "password_quality"; static String PREF_PASSWORD_LENGTH = "password_length"; static String PREF_MAX_FAILED_PW = "max_failed_pw"; void showToast(Context context, CharSequence msg) { Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } @Override public void onEnabled(Context context, Intent intent) { showToast(context, "Sample Device Admin: enabled"); } @Override public CharSequence onDisableRequested(Context context, Intent intent) { return "This is an optional message to warn the user about disabling."; } @Override public void onDisabled(Context context, Intent intent) { showToast(context, "Sample Device Admin: disabled"); } @Override public void onPasswordChanged(Context context, Intent intent) { showToast(context, "Sample Device Admin: pw changed"); } @Override public void onPasswordFailed(Context context, Intent intent) { showToast(context, "Sample Device Admin: pw failed"); } @Override public void onPasswordSucceeded(Context context, Intent intent) { showToast(context, "Sample Device Admin: pw succeeded"); } } 

使用Activity.getWindow()来获取你的活动的窗口; 使用Window.addFlags()在你想要的WindowManager.LayoutParams中添加以下任何一个标志:

  • FLAG_DISMISS_KEYGUARD

  • FLAG_SHOW_WHEN_LOCKED

  • FLAG_TURN_SCREEN_ON

示例页面上的androidmanifest.xml和policies.xml文件在浏览器中不可见,因为它试图将XML文件格式化为HTML。 我只是发布这个为了方便别人参考,这是来自示例页面。

感谢所有这个有用的问题!

AndroidManifest.xml中:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.kns" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".LockScreenActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyAdmin" android:permission="android.permission.BIND_DEVICE_ADMIN"> <meta-data android:name="android.app.device_admin" android:resource="@xml/policies" /> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver> </application> </manifest> 

policies.xml

 <?xml version="1.0" encoding="utf-8"?> <device-admin xmlns:android="http://schemas.android.com/apk/res/android"> <uses-policies> <limit-password /> <watch-login /> <reset-password /> <force-lock /> <wipe-data /> </uses-policies> </device-admin>