转载请标明出处http://blog.csdn.net/mingyue_1128
一、首先要创建一个Service保证Service一直开启,同时不被一键清理掉。
二、屏蔽掉系统的锁屏
三、创建锁屏的Activity,在Activity中屏蔽掉back,menu,home键
四、保证自己的进程不在最近任务中出现
一、创建一个Service名字为LockService
public class LockService extends Service{ private KeyguardManager km; private KeyguardLock kk; private BroadcastReceiver broadcastReceiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent arg1) { Log.e("-----可以跳到锁屏界面--------", "---------"); Intent intent=new Intent(context,LockActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }; @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); //服务里面收到关闭屏幕的动作就发送广播 IntentFilter iFilter=new IntentFilter(Intent.ACTION_SCREEN_OFF); this.registerReceiver(broadcastReceiver, iFilter); } @Override public void onCreate() { super.onCreate(); //屏蔽掉系统的锁屏 km=(KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); kk=km.newKeyguardLock(""); kk.disableKeyguard(); } @Override public void onDestroy() { super.onDestroy(); } }
在Service中与他合作的还有一个广播,当监听到屏幕关的时候,service就会发送广播,使屏幕开的时候为自己锁屏界面
三、锁屏界面中屏蔽掉menu,home,back
public class LockActivity extends Activity{ private static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000; private Button btn_open; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);//关键代码 setContentView(R.layout.activity_main); //解锁的按钮 btn_open=(Button) findViewById(R.id.open); btn_open.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { finish(); } }); } @SuppressWarnings("static-access") @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==event.KEYCODE_BACK){ return true; } if(keyCode==event. KEYCODE_HOME){ return true; } if(keyCode == KeyEvent.KEYCODE_MENU) {//MENU键 //监控/拦截菜单键 return true; } return super.onKeyDown(keyCode, event); } }保证自己的锁屏程序不在最近进程中出现,只需要在注册锁屏界面的Activity时,添加一个属性android:excludeFromRecents="true",如下
<activity android:name="com.mm.wallpaper.LockActivity" android:launchMode="singleInstance" android:excludeFromRecents="true"> <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
四、我们需要看一下主界面怎么处理
public class MainActivity extends Activity { MyBroadcast receiveBroadCast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册广播 receiveBroadCast = new MyBroadcast(); IntentFilter filter = new IntentFilter(); filter.addAction("action"); // 只有持有相同的action的接受者才能接收此广播 registerReceiver(receiveBroadCast, filter); //发送广播 Intent intent=new Intent(); intent.setAction("action"); this.sendBroadcast(intent); } public class MyBroadcast extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { KeyguardManager km = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE); KeyguardLock kk = km.newKeyguardLock(""); kk.disableKeyguard(); //开始服务 Intent service=new Intent(); service.setClass(context,LockService.class);//稍后再定义 context.startService(service); } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); unregisterReceiver(receiveBroadCast); } }
在这里我们需要注册一个广播保证服务的开启
在配置清单中MainActivity需要多一个属性来保证程序始终在
<category android:name="android.intent.category.HOME"/>
整个的流程大概如此,希望对想做解锁软件的朋友有所帮助。