调用服务里的方法:绑定服务
1. 在看门狗的服务中创建添加app包名到临时不需要受密码保护的集合的方法 tempStopProtect()
2. 创建 watchdogInference接口,在接口中定义一个方法
3. 创建代理人类继承服务中的
Binder ,实现 watchdogInference接口 ; 在代理人 类中实现接口中未实现的方法, 在方法中调用tempStopProtect()
(给代理人一个方法)
4. 在看门狗服务的public IBinder onBind(Intent intent){} 中 return: 代理人
5.在输入密码的activity中的Oncreate()方法中绑定服务
6.定义一个类实现ServiceConnection接口, 实现未实现的方法, 在未实现的方法中初始化代理人,
watchDogInference
=(WatchDogInference) service;
7. 在密码输入正确的时候, 调用代理人接口内的方法,即:调用服务内的方法
8. 在onDestroy()方法中, 取消绑定服务
----------------------------------------------------------------------------------------------------------
在WatchDogService 服务中:
public
class
WatchDogService
extends
Service {
protected
static
final
String
TAG
=
"WatchDogService"
;
private
ActivityManager
am
;
private
boolean
flags
;
private
AppLockDao
dao
;
private
pauseProtectBroad
ppbreceiver
;
private
ScreenLockReceiver
lockReceiver
;
private
List<String>
tempStopProtectPacknames
;
@Override
public
IBinder onBind(Intent intent) {
return
new
MyBind();
}
/**
* 3. 创建代理人类继承服务中的Binder ,实现 watchdogInference接口 ; 在代理人 类中实现接口中未实现的方法
*
@author
Administrator
*
*/
public
class
MyBind
extends
Binder
implements
WatchDogInference{
@Override
public
void
getMethod(String pakagename) {
temStopProtect(pakagename);
//给代理人一个方法
}
}
/**
* 1.服务内部的方法,把临时停止保护的应用程序的包名加入到集合里面
*
@param
pakage_name
*/
public
void
temStopProtect(String pakage_name){
tempStopProtectPacknames
.add(pakage_name);
}
/**
* 注册自定义广播事件, 拿到watchdogActivity返回过来的包名,并告诉看门狗该应用暂时不用保护了
*/
public
class
pauseProtectBroad
extends
BroadcastReceiver{
@Override
public
void
onReceive(Context context, Intent intent) {
String pkgname = intent.getStringExtra(
"pkgname"
);
//得到暂停保护的包名
tempStopProtectPacknames
.add(pkgname);
//
将包名装进暂停保护的集合中
}
}
/**
* 注册当屏幕锁屏时的广播事件: 从暂时不受密码保护的集合中去掉该包名
*
@author
Administrator
*
*/
private
class
ScreenLockReceiver
extends
BroadcastReceiver{
@Override
public
void
onReceive(Context context, Intent intent) {
//屏幕锁屏
tempStopProtectPacknames
.clear();
}
}
/**
* 服务开启时
*/
@Override
public
void
onCreate() {
Log. i(
TAG
,
"开启服务"
);
tempStopProtectPacknames
=
new
ArrayList<String>();
//当服务开启时开启初始化pass集合的广播
ppbreceiver
=
new
pauseProtectBroad();
IntentFilter filter=
new
IntentFilter();
filter.addAction(
"this.activity.not.protected"
);
registerReceiver(
ppbreceiver
, filter);
//当服务开启时开启清空pass集合的广播
lockReceiver
=
new
ScreenLockReceiver();
IntentFilter lockFilter =
new
IntentFilter();
lockFilter.addAction(Intent.
ACTION_SCREEN_OFF
);
registerReceiver(
lockReceiver
, lockFilter);
dao
=
new
AppLockDao(getApplicationContext());
//1.创建activity管理员
am
=(ActivityManager) getSystemService(
ACTIVITY_SERVICE
);
//2.看门狗在服务开启时不停的监视着后台正在运行的程序
new
Thread(){
public
void
run() {
flags
=
true
;
while
(
flags
){
//获取正在运行的任务栈 3:可以任意定义,表示近期开启的3个任务栈
List<RunningTaskInfo> runningTasks =
am
.getRunningTasks(3);
//最上层的任务栈: 刚打开的应用程序的任务栈
RunningTaskInfo runningTaskInfo = runningTasks.get(0);
//得到刚刚开启的应用程序的activity的名称
String packageName = runningTaskInfo.
topActivity
.getPackageName();
//判断得到的包名的应用是否在已加锁的数据库中
if
(
dao
.find(packageName)){
if
(!
tempStopProtectPacknames
.contains(packageName)){
//如果暂停保护集合中存在该包名
Log. i(
TAG
,
"弹出提示输入密码的activity"
);
Intent intent=
new
Intent(getApplicationContext(),WatchDogActivity.
class
);
intent.setFlags(Intent.
FLAG_ACTIVITY_NEW_TASK
);
//开启新任务栈
intent.putExtra(
"packageName"
, packageName);
startActivity(intent);
}
try
{
Thread. sleep(50);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
};
}.start();
super
.onCreate();
}
/**
* 服务销毁时
*/
public
void
onDestroy() {
Log. i(
TAG
,
"关闭服务..."
);
flags
=
false
;
//服务关闭时关闭关门狗
//服务关闭时终止自定义广播
ppbreceiver
.abortBroadcast();
lockReceiver
.abortBroadcast();
};
}
-------------------------------------------------------------------------------------------------------------------------
接口类:
/**
* 2. 创建 watchdogInference接口,在接口中定义一个方法
*
@author
Administrator
*
*/
public
interface
WatchDogInference {
public
void
getMethod(String pakagename);
}
--------------------------------------------------------------------------------------------------------------
在activity类中:
public
class
WatchDogActivity
extends
Activity {
private
ImageView
iv_icon
;
private
TextView
tv_name
;
private
EditText
et_password
;
String
packageName
;
private
WatchDogInference
watchDogInference
;
private
MyCoon
conn
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_watchdog
);
iv_icon
=(ImageView) findViewById(R.id.
iv_icon
);
tv_name
=(TextView) findViewById(R.id.
tv_name
);
et_password
=(EditText) findViewById(R.id.
et_password
);
//得到服发来的包名
Intent intent = getIntent();
packageName
= intent.getStringExtra(
"packageName"
);
//绑定服务
Intent intent_bind=
new
Intent(
this
,WatchDogService.
class
);
conn
=
new
MyCoon();
bindService(intent_bind,
conn
, 0);
//得到包管理者
PackageManager pm = getPackageManager();
//得到app 的信息
try
{
ApplicationInfo applicationInfo = pm.getApplicationInfo(
packageName
, 0);
String appName = applicationInfo.loadLabel(pm).toString();
Drawable appIcon = applicationInfo.loadIcon(pm);
//给关心的控件赋值
iv_icon
.setImageDrawable(appIcon);
tv_name
.setText(appName);
}
catch
(NameNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取代理人中的方法
*
@author
Administrator
*
*/
public
class
MyCoon
implements
ServiceConnection{
@Override
public
void
onServiceConnected(ComponentName name, IBinder service) {
watchDogInference
=(WatchDogInference) service;
}
@Override
public
void
onServiceDisconnected(ComponentName name) {
}
}
/**
* 屏蔽返回键
*/
@Override
public
boolean
onKeyDown(
int
keyCode, KeyEvent event) {
if
(keyCode == KeyEvent.
KEYCODE_BACK
) {
// 后退键盘被点击了
// 回桌面的操作
// <action android:name="android.intent.action.MAIN" />
// <category android:name="android.intent.category.HOME" />
// <category android:name="android.intent.category.DEFAULT" />
// <category android:name="android.intent.category.MONKEY"/>
Intent intent =
new
Intent();
intent.setAction(
"android.intent.action.MAIN"
);
intent.addCategory(
"android.intent.category.HOME"
);
intent.addCategory(
"android.intent.category.DEFAULT"
);
intent.addCategory(
"android.intent.category.MONKEY"
);
startActivity(intent);
finish();
return
true
;
}
return
super
.onKeyDown(keyCode, event);
}
/**
* 确定按钮的点击事件
*
@param
view
*/
public
void
login(View view){
String pwd=
et_password
.getText().toString().trim();
if
(
"123"
.equals(pwd)){
//发送自定义广播给看门狗, 当前activity不要保护了
/* Intent intent_pass=new Intent();
intent_pass.setAction("this.activity.not.protected");
intent_pass.putExtra(" pkgname",packageName);
sendBroadcast(intent_pass);*/
//在密码输入失败的时候,调用接口内的
watchDogInference
.getMethod(
packageName
);
finish();
//关闭当前activity, 进入加锁的程序页面
}
else
{
Toast. makeText(
this
,
"密码输入不正确"
, 0).show();
}
}
/**
* 当 activity销毁时,接触绑定
*/
@Override
protected
void
onDestroy() {
super
.onDestroy();
if
(
conn
!=
null
){
unbindService(
conn
);
}
}
}