1
package example.com.locker.service; import android.app.KeyguardManager; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.IBinder; import android.util.Log; import android.widget.Toast; import example.com.locker.activity.LockActivity; public class LockService extends Service { private final String TAG="mylock"; public LockService() { } private Intent mIntent; @Override public IBinder onBind(Intent intent) { return null; } private BroadcastReceiver mScreenOnReceiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action=intent.getAction(); Log.d("mylock",action); if(action.equals(Intent.ACTION_SCREEN_ON) ||action.equals(Intent.ACTION_SCREEN_OFF)) { km=(KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE); kl=km.newKeyguardLock(""); kl.disableKeyguard(); startActivity(mIntent); } } }; KeyguardManager km; KeyguardManager.KeyguardLock kl; @Override public void onCreate() { super.onCreate(); Toast.makeText(this,"lock 服务启动",Toast.LENGTH_SHORT).show(); mIntent=new Intent(LockService.this, LockActivity.class); mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); IntentFilter mScreenOnFilter=new IntentFilter(); mScreenOnFilter.addAction("android.intent.action.SCREEN_ON"); mScreenOnFilter.addAction("android.intent.action.SCREEN_OFF"); registerReceiver(mScreenOnReceiver,mScreenOnFilter); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG,"服务终止"); this.unregisterReceiver(mScreenOnReceiver); //被销毁时启动自身,保持在后台存活 startService(new Intent(this,LockService.class)); } }
package example.com.locker.activity; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import example.com.locker.R; import example.com.locker.view.LockLayer; public class LockActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //全屏显示 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_lock); Button unlockButton=(Button)findViewById(R.id.unlock); unlockButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } //屏蔽音量键,返回键 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.KEYCODE_BACK: case KeyEvent.KEYCODE_VOLUME_DOWN: case KeyEvent.KEYCODE_VOLUME_UP: return true; } return super.onKeyDown(keyCode,event); } }
屏蔽系统本身锁屏,需要权限<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
监听开机广播,需要权限<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
监听开机广播
package example.com.locker.service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.Toast; import example.com.locker.activity.CardActivity; public class BootReceiver extends BroadcastReceiver { public BootReceiver() { } @Override public void onReceive(Context context, Intent intent) { Intent serviceIntent=new Intent(context,LockService.class); context.startService(serviceIntent); Toast.makeText(context,"开启启动",Toast.LENGTH_SHORT).show(); } }
<receiver android:name=".service.BootReceiver" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>