1看门狗服务
package com.example.mobilesafe.service;
import java.util.List;
import com.example.mobilesafe.lockActivity;
import com.example.mobilesafe.bean.donotChangeValue;
import com.example.mobilesafe.db.dao.LockAppDao;
import com.example.mobilesafe.utils.SPUtil;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
/**
* @author Administrator 不停的查看当前打开应用的包名,如果包含在数据库中,就打开拦截界面
*/
public class WatchDagService extends Service {
private boolean isWatch;
private String skipPackage;
private SkipReceiver skipReceiver;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
isWatch = true;
watch();
skipReceiver = new SkipReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(SPUtil.getSpString(getApplicationContext(),
donotChangeValue.SKIPLOCKACTIVITYACTION));
registerReceiver(skipReceiver, intentFilter);
super.onCreate();
}
/**
* 观察是否有被锁的应用被开启
*/
private void watch() {
new Thread() {
public void run() {
LockAppDao dao = LockAppDao
.getInstence(getApplicationContext());
List<String> allPackageName = dao.getAllPackageName();
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
while (isWatch) {
// 得到当前正在运行的APP
List<RunningTaskInfo> runningTasks = am.getRunningTasks(1);
RunningTaskInfo runningTaskInfo = runningTasks.get(0);
// 如果这个应用是被锁应用
if (allPackageName.contains(runningTaskInfo.topActivity
.getPackageName())) {
if (runningTaskInfo.topActivity.getPackageName()
.equals(skipPackage)) {
continue;
}
// 就弹出拦截页面
Intent intent = new Intent(getApplicationContext(),
lockActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("packagename",
runningTaskInfo.topActivity.getPackageName());
startActivity(intent);
}
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
}
private class SkipReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
skipPackage = intent.getStringExtra("packagename");
}
}
@Override
public void onDestroy() {
isWatch = false;
if (skipReceiver != null) {
unregisterReceiver(skipReceiver);
}
super.onDestroy();
}
}
2.拦截页面
package com.example.mobilesafe;
import com.example.mobilesafe.bean.donotChangeValue;
import com.example.mobilesafe.engine.getAppInfoEngine;
import com.example.mobilesafe.utils.SPUtil;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class lockActivity extends Activity {
private Drawable icon;
private String name;
private TextView tv_name;
private ImageView iv_icon;
private EditText et_psd;
private Button bt_submit;
private String packagename;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock);
initUI();
initData();
initClick();
}
/**
* 初始化点击事件
*/
private void initClick() {
bt_submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String psd = et_psd.getText().toString();
//检测密码
if (TextUtils.isEmpty(psd)) {
Toast.makeText(getApplicationContext(), "请输入密码", 0).show();
return;
} else {
if (psd.equals("123")) {
//如果输入的是“123”,就发送广播,携带包名,告诉这个APP不需要被锁住
Intent intent = new Intent(SPUtil.getSpString(
getApplicationContext(),
donotChangeValue.SKIPLOCKACTIVITYACTION));
intent.putExtra("packagename", packagename);
sendBroadcast(intent);
finish();
} else {
Toast.makeText(getApplicationContext(), "密码错误", 0)
.show();
}
}
}
});
}
private void initUI() {
tv_name = (TextView) findViewById(R.id.tv_name);
iv_icon = (ImageView) findViewById(R.id.iv_icon);
et_psd = (EditText) findViewById(R.id.et_psd);
bt_submit = (Button) findViewById(R.id.bt_submit);
}
@SuppressLint("NewApi")
private void initData() {
Intent intent = getIntent();
//得到打开的应用的包名(打开本页面携带的信息)
packagename = intent.getStringExtra("packagename");
PackageManager pm = getPackageManager();
try {
//用PackageManager与包名获得此应用信息
ApplicationInfo info = pm.getApplicationInfo(packagename, 0);
icon = info.loadIcon(pm);
name = info.loadLabel(pm).toString();
//设置控件信息
tv_name.setText(name);
iv_icon.setBackground(icon);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onBackPressed() {
//打开桌面
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
super.onBackPressed();
}
}
3。拦截页面在清单文件配置的方式
<activity
android:name="com.example.mobilesafe.lockActivity"
android:excludeFromRecents="true" //关闭页面后,最近开启里找不到该应用
android:launchMode="singleInstance" //保证单例模式开启应用,任务栈里只有一个应用 >
</activity>