练手小项目(5)安全卫士_程序锁
最近想做的小新工具箱,一直想做一个程序锁,其实原理,很简单,先注册一个服务,检测手机所有进程,如果发现被加锁的app启动,马上弹出一个输入程序锁界面,但是这样子bug很多。我先做一个基本后面慢慢把bug修复。
①基本软件管理器做一个程序锁
懒得写界面了,如果你没看软件管理器,建议你看一下、
练手小项目之软件管理器
②UI界面的完成
其实也就是在软件管理器中 后面加了一个 图片 lock
③业务逻辑的完成
其实我在一直做的软件管理器,因为要修复bug,但是又不能保准哪里出错,所以在eclipse里面有个很好的标记
/** TODO 数据库查询*/
eclipse 会自动在在旁边打个蓝色的标记
OK, 最后我会把我的测试项目上传上来,哎 里面有好多干货的.............
1.初始化组件
<span style="white-space:pre"> </span>/** TODO 修改部分*/ImageView iv_status;
holder.tv_name = (TextView) view.findViewById(R.id.tv_app_name);
2.数据库存储状态
一,建立数据库帮助类
package com.example.Darkbutton.Appmanager;import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;public class ApplockDBOpenHelper extends SQLiteOpenHelper {public ApplockDBOpenHelper(Context context) {super(context, "applcok.db", null, 1);}@Overridepublic void onCreate(SQLiteDatabase db) {
// db.execSQL("create table applock (_id integer primary key autoincrement,packname varchar(20))");db.execSQL("create table applock (_id integer primary key autoincrement,packname varchar(20))");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}
二,建立数据库查询类
package com.example.Darkbutton.Appmanager;import java.util.ArrayList;
import java.util.List;import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;/*** @author xiaoxin007 2014-12-7下午11:57:27 TODO*/
public class ApplockDao {private Context context;private ApplockDBOpenHelper helper;public ApplockDao(Context context) {super();this.context = context;helper = new ApplockDBOpenHelper(context);}/*** 添加一个要锁定应用程序的包名* @param packname*/public void add(String packname){SQLiteDatabase db = helper.getWritableDatabase();ContentValues values=new ContentValues();values.put("packname", packname);db.insert("applock", null, values);db.close();}/*** 删除一个要锁定应用程序的包名* @param packname*/public void delete(String packname){SQLiteDatabase db = helper.getWritableDatabase();db.delete("applock", "packname=?", new String[]{packname});db.close();}/*** 查询一条程序锁包名记录是否存在* @param packname* @return*/public boolean find(String packname){boolean result = false;SQLiteDatabase db = helper.getReadableDatabase();Cursor cursor=db.query("applock", null, "packname=?", new String[]{packname}, null, null, null);if(cursor.moveToNext()){result = true;}cursor.close();db.close();return result;}/*** 查询全部的包名* @param packname* @return*/public List<String> findAll(){List<String> protectPacknames = new ArrayList<String>();SQLiteDatabase db = helper.getReadableDatabase();Cursor cursor = db.query("applock", new String[]{"packname"}, null,null, null, null, null);while(cursor.moveToNext()){protectPacknames.add(cursor.getString(0));}cursor.close();db.close();return protectPacknames;}}
三,使用数据库
/** TODO 数据库查询*/private ApplockDao dao;
<span style="white-space:pre"> </span>/** TODO 实例化*/dao = new ApplockDao(this);
<span style="white-space:pre"> </span>/** TODO* */if(dao.find(appInfo.getPackname())){holder.iv_status.setImageResource(R.drawable.lock);}else{holder.iv_status.setImageResource(R.drawable.unlock);}
④ 加锁应用
/** TODO 修改部分*/@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {// 设置点击第一个条目的时候不显示,因为不是软件if (position == 0) {return false;// 以此类推} else if (position == (userAppInfos.size() + 1)) {return false;} else if (position <= userAppInfos.size()) {// 用户程序int newposition = position - 1;appInfo = userAppInfos.get(newposition);} else {// 系统程序int newposition = position - 1 - userAppInfos.size() - 1;appInfo = systemAppInfos.get(newposition);}ViewHolder holder=(ViewHolder) view.getTag();if (dao.find(appInfo.getPackname())) {dao.delete(appInfo.getPackname());holder.iv_status.setImageResource(R.drawable.unlock);} else {dao.add(appInfo.getPackname());holder.iv_status.setImageResource(R.drawable.lock);}return false;}
因为在listview 中 有两个条目不是软件 所以进行判断
⑤ 监控软件
package com.example.Darkbutton.Appmanager;import java.util.List;import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;public class WatchDogService extends Service {private ActivityManager am;private boolean flag;//2.第二部 已经检测软件先了,现在用数据库去查询是否需要保护的private ApplockDao dao;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {am=(ActivityManager)getSystemService(ACTIVITY_SERVICE);dao=new ApplockDao(this);flag=true;new Thread(){public void run() {while (flag) {//得到正在运行的运行task ,因为只要APP一运行就会建立一个人物栈List<RunningTaskInfo> infos = am.getRunningTasks(1);String packageName = infos.get(0).topActivity.getPackageName();if (dao.find(packageName)) {//③当需要保护的时候 蹦出来一个输入界面Intent intent=new Intent(getApplicationContext(),EnterPwdActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(intent);}try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}}};}.start();super.onCreate();}@Overridepublic void onDestroy() {super.onDestroy();}
}
然后再设置界面中 打开他 他就会去监控了
package com.example.Darkbutton.Appmanager;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;import com.example.Darkbutton.R;
import com.example.Darkbutton.SetingItem;
import com.example.Darkbutton.Utils.ServiceUtils;public class SetingActivity extends Activity {private SetingItem siv_watchdog;private Intent watchIntent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_setting);//程序锁设置siv_watchdog = (SetingItem) findViewById(R.id.siv_watchdog);watchIntent = new Intent(this, WatchDogService.class);siv_watchdog.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (siv_watchdog.isChecked()) {// 变为非选中状态siv_watchdog.setChecked(false);stopService(watchIntent);} else {// 选择状态siv_watchdog.setChecked(true);startService(watchIntent);}}});}@Overrideprotected void onResume() {super.onResume();boolean iswatchdogServiceRunning = ServiceUtils.isServiceRunning(SetingActivity.this,"com.example.Darkbutton.Appmanager.WatchDogService");siv_watchdog.setChecked(iswatchdogServiceRunning);}
}
注意 服务要在配置文件中注册 好需要一个权限 get_Task
package com.example.Darkbutton.Appmanager;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.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;import com.example.Darkbutton.R;public class EnterPwdActivity extends Activity {private EditText et_password;private String packname;private TextView tv_appname;private ImageView iv_appicon;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_enter_pwd);et_password = (EditText) findViewById(R.id.et_password);tv_appname = (TextView) findViewById(R.id.tv_appname);iv_appicon = (ImageView) findViewById(R.id.iv_appicon);Intent intent = getIntent();//当前要保护的应用程序包名packname = intent.getStringExtra("packname");PackageManager pm = getPackageManager();try {ApplicationInfo info = pm.getApplicationInfo(packname, 0);tv_appname.setText(info.loadLabel(pm));iv_appicon.setImageDrawable(info.loadIcon(pm));} catch (NameNotFoundException e) {e.printStackTrace();}}@Overridepublic void onBackPressed() {//回桌面。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);//所有的activity最小化 不会执行ondestory 只执行 onstop方法。}@Overrideprotected void onStop() {super.onStop();finish();}public void click(View view){String pwd = et_password.getText().toString().trim();if(TextUtils.isEmpty(pwd)){Toast.makeText(this, "密码不能为空", 0).show();return;}//假设正确的密码是123if("123".equals(pwd)){//告诉看门狗这个程序密码输入正确了。 可以临时的停止保护。//自定义的广播,临时停止保护。
// Intent intent = new Intent();
// intent.setAction("com.xiaoxin.mobilesafe.tempstop");
// intent.putExtra("packname", packname);
// sendBroadcast(intent);finish();}else{Toast.makeText(this, "密码错误。。", 0).show();}}
}
练手小项目(5)安全卫士_程序锁相关推荐
- c语言模拟器怎么打程序,C语言初学者练手小项目——万花模拟器
原标题:C语言初学者练手小项目--万花模拟器 还记得小时候玩的万花尺么?好好玩,各种不同的点距能画出各种各样形状图形. C语言程序万花尺模拟 函数功能:每隔5秒随机生成万花图形 并自动保存作图参数以及 ...
- C# Socket网络编程(附赠练手小项目)
在公司花了两天的时间来研究Socket编程,感觉也还是学到了一点点皮毛,不过也还是有一点点的小成就. 配合Winform窗体界面,实现了简单的窗体间互相通信的小项目(可以互相发消息,服务器可以给客户端 ...
- ssm练手小项目_20 个 JavaScript+Html+CSS 练手的小项目
前言: 最近在 GitHub 上发现了一个 vanillawebprojects[1] 开源仓库,里面收集了 20 个 JavaScript+Html+CSS的练手项目,没有使用任何框架,可以让你从基 ...
- 台式小风扇(HTML+CSS+JS练手小项目)
台式小风扇(HTML+CSS+JS练手小项目) 功能介绍 外观展示 HTML代码 CSS代码 JS代码 总结 功能介绍 前段时间看到这样的风扇特效,感觉还挺好玩,就自己也写一个练练手. 风扇有四个档位 ...
- 爬虫练手小项目:豆瓣高分图书TOP100
爬虫练手小项目:豆瓣高分图书TOP100 import requests import re from requests.exceptions import RequestException impo ...
- 数据结构练手小项目(AVL树、哈希表、循环链表、MySQL数据库)
文章目录 前言 正文(无删减) 我的想法(删减修改版) 数据导入与数据存储 功能实现 数据结构 用户结构 SIM卡结构 AVL树数据结构 哈希表结构 数据表 用户表 SIM卡表 时间安排 前言 本月主 ...
- html+css+js之20个练手小项目(一)
html+css+js之20个练手小项目(一)--Hangman 前言 一.HTML 二.CSS 三.JS 前言 前端新手练习,记录不迷失. 主要练习html和CSS布局以及JS. 来源github, ...
- 练手小项目,爬取3DM图片
博客原文:https://weweweha.com 1. 概述 爬取3DM指定网页的游戏壁纸,并且通过多线程来加速爬取图片的速度. 2.使用库 request库用来1解析指定网页,re库用来搜 ...
- Golang练手小项目系列
Golang练手小项目系列 本系列整理了10个工作量和难度适中的Golang小项目,适合已经掌握Go语法的工程师进一步熟练语法和常用库的用法. golang练手小项目系列(1)-位向量 golang练 ...
最新文章
- 将不确定变成确定~frameset页面不能正确加载
- Asp.Net 将HTML中通过dom-to-image.js标签div内的内容转化为图片保存到本地
- php get_extension_funcs,get_extension_funcs()
- User Mode Driver Management介绍(一)
- 【angularjs】使用angular搭建项目,pc端实现网页中的内容不可复制
- 安全机构未清理 PDF 文件,暴露敏感信息
- Maven异常:Missing artifact org.slf4j:slf4j-api:jar:1.6.4
- jmeter java性能_使用JMeter进行性能测试(Java请求)
- Atitit httpclient 概述 rest接口
- python pandas Excel 生成图片
- 详解程序化交易、量化交易、高频交易、算法交易、各是什么意思?
- android lt;webview,Android WebView使用基础 – 圣骑士wind – 博客园
- pe系统 服务器维护,FirPE 维护系统
- 浅谈Go 语言之 go-app
- Keras_examples
- STM32调试出现Cannot access Memory的情况
- MIPI 系列之 D-PHY
- origin8.0 绘图学习笔记
- DSS部署-2、环境准备
- 对你最好的男人,永远是还没上床的那个