Android--手机卫士涉及的知识点总结(五)

简介: 主活动中点击切换 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

主活动中点击切换

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
     package com.cca.mobilephone.activity;
 
import java.util.ArrayList;
import java.util.List;
 
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import com.cca.mobilephone.R;
import com.cca.mobilephone.db.dao.AppClockDao;
import com.cca.mobilephone.domain.AppInfo;
import com.cca.mobilephone.engine.AppManagerInfos;
/**
  * 设置程序加锁的Activity
  * @author Administrator
  *
  */
public class AppLockedActivity extends Activity implements OnClickListener {
 
private TextView app_unlock;
private TextView app_locked;
/**
  * 两种线性布局
  */
private LinearLayout ll_applocked;
private LinearLayout ll_appunlock;
/**
  * 两种listview
  */
private ListView lv_locked;
private ListView lv_unlock;
/**
  * 正在加载的进度条
  */
private LinearLayout ll_loading;
/**
  * 所有程序的集合
  */
private List infos;
/**
  * 显示未加锁程序的个数
  */
private TextView tv_unlock_count;
/**
  * 显示加锁程序的个数
  */
private TextView tv_locked_count;
/**
  * 加锁的数据库
  */
private AppClockDao dao;
/**
  * 加锁集合
  */
private List lockedInfo;
/**
  * 未加锁集合
  */
private List unlockInfo;
/**
  * 未加锁
  */
private MylockAdapter unlockadapter;
/**
  * 加锁适配器
  */
private MylockAdapter lockedadapter;
 
private Handler handler= new Handler(){
     public void handleMessage(android.os.Message msg) {
         // 加载未加锁的适配器
         lv_unlock.setAdapter(unlockadapter);
         // 加载加锁的适配器
         lv_locked.setAdapter(lockedadapter);
         ll_loading.setVisibility(View.INVISIBLE);
     };
};
 
@Override
protected void onCreate(Bundle savedInstanceState) {
     super .onCreate(savedInstanceState);
 
     setContentView(R.layout.activity_applocked);
     tv_unlock_count = (TextView) findViewById(R.id.tv_unlock_count);
     tv_locked_count = (TextView) findViewById(R.id.tv_locked_count);
 
     ll_loading=(LinearLayout) findViewById(R.id.ll_loading);
     dao = new AppClockDao( this );
 
     app_unlock = (TextView) findViewById(R.id.app_unlock);
     app_locked = (TextView) findViewById(R.id.app_locked);
     // 两种listview
     lv_locked = (ListView) findViewById(R.id.lv_locked);
     lv_unlock = (ListView) findViewById(R.id.lv_unlock);
     // 两个不同点击事件
     app_locked.setOnClickListener( this );
     app_unlock.setOnClickListener( this );
     // 两个线性布局
     ll_applocked = (LinearLayout) findViewById(R.id.ll_applocked);
     ll_appunlock = (LinearLayout) findViewById(R.id.ll_appunlock);
 
     lockedInfo = new ArrayList();
     unlockInfo = new ArrayList();
     unlockadapter = new MylockAdapter( true );
     lockedadapter = new MylockAdapter( false );
 
     new Thread(){
         public void run() {
     // 获得全部软件程序
     infos = AppManagerInfos.getAppManagerInfos(getApplicationContext());
 
     /**
      * 遍历集合,区分加锁和未加锁
      */
     for (AppInfo info : infos) {
         if (dao.find(info.getPakageName())) {
             lockedInfo.add(info);
         } else {
             unlockInfo.add(info);
         }
     }
             handler.sendEmptyMessage( 0 );
         };
     }.start();
 
}
 
@Override
public void onClick(View v) {
     switch (v.getId()) {
     case R.id.app_unlock: // 点击未加锁
         ll_appunlock.setVisibility(View.VISIBLE);
         ll_applocked.setVisibility(View.GONE);
         app_unlock.setBackgroundResource(R.drawable.tab_left_pressed);
         app_locked.setBackgroundResource(R.drawable.tab_left_default);
 
         break ;
     case R.id.app_locked: // 点击已加锁
         ll_appunlock.setVisibility(View.GONE);
         ll_applocked.setVisibility(View.VISIBLE);
         app_unlock.setBackgroundResource(R.drawable.tab_left_default);
         app_locked.setBackgroundResource(R.drawable.tab_left_pressed);
 
         break ;
     }
}
private class MylockAdapter extends BaseAdapter {
     /**
      * isunlock是否加锁的标识符 true 为未加锁 、 false 已加锁
      */
     boolean isunlock;
 
     public MylockAdapter( boolean isunlock) {
         this .isunlock = isunlock;
     }
     @Override
     public int getCount() {
         int count = 0 ;
         if (isunlock) {
             count = unlockInfo.size();
             tv_unlock_count.setText( "未加锁软件:" + count);
         } else {
             count = lockedInfo.size();
             tv_locked_count.setText( "加锁软件:" + count);
         }
 
         return count;
     }
 
     @Override
     public View getView( int position, View convertView, ViewGroup parent) {
         final View view;
         ViewHolder holder;
         if (convertView != null && convertView instanceof RelativeLayout) {
             view = convertView;
             holder = (ViewHolder) view.getTag();
         } else {
             view = View.inflate(getApplicationContext(),
                     R.layout.item_unlock_app, null );
             holder = new ViewHolder();
             holder.img_appicom = (ImageView) view
                     .findViewById(R.id.img_appicom);
             holder.tv_appname = (TextView) view
                     .findViewById(R.id.tv_appname);
             holder.img_locked = (ImageView) view
                     .findViewById(R.id.img_locked);
             view.setTag(holder);
         }
 
         final AppInfo info;
         if (isunlock) {
             info = unlockInfo.get(position);
             holder.img_locked
                     .setImageResource(R.drawable.list_button_lock_pressed);
         } else {
             info = lockedInfo.get(position);
             holder.img_locked
                     .setImageResource(R.drawable.list_button_unlock_pressed);
 
         }
 
         holder.img_appicom.setImageDrawable(info.getIcon());
         holder.tv_appname.setText(info.getAppname());
         /**
          * 点击加锁按钮移出条目,把数据加到数据库
          */
         holder.img_locked.setOnClickListener( new OnClickListener() {
             @Override
             public void onClick(View v) {
 
                 if (isunlock) {
                     TranslateAnimation am = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0 ,
                             Animation.RELATIVE_TO_SELF, 1 .0f,
                             Animation.RELATIVE_TO_SELF, 0 ,
                                             Animation.RELATIVE_TO_SELF, 0 );    
                     am.setDuration( 500 );
                     view.startAnimation(am);
                     am.setAnimationListener( new AnimationListener() {
                         @Override
                         public void onAnimationStart(Animation animation) {
                         }
                         @Override
                         public void onAnimationRepeat(Animation animation) {
                         }
                         @Override
                         public void onAnimationEnd(Animation animation) {
                             // 未加锁
                             unlockInfo.remove(info);
                             lockedInfo.add(info);
                             dao.insert(info.getPakageName());
                             // 通知界面更新
                             // notifyDataSetChanged();
                             unlockadapter.notifyDataSetChanged();
                             lockedadapter.notifyDataSetChanged();
                         }
                     });
 
                 } else {
                     TranslateAnimation am = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0 ,
                             Animation.RELATIVE_TO_SELF, - 1 .0f,
                             Animation.RELATIVE_TO_SELF, 0 ,
                                             Animation.RELATIVE_TO_SELF, 0 );    
                     am.setDuration( 500 );
                     view.startAnimation(am);
                     am.setAnimationListener( new AnimationListener() {
                         @Override
                         public void onAnimationStart(Animation animation) {
                         }
                         @Override
                         public void onAnimationRepeat(Animation animation) {
                         }
                         @Override
                         public void onAnimationEnd(Animation animation) {
                             // 已经加锁
                             lockedInfo.remove(info);
                             unlockInfo.add(info);
                             dao.delete(info.getPakageName());
                             // 通知界面更新
                             // notifyDataSetChanged();
                             unlockadapter.notifyDataSetChanged();
                             lockedadapter.notifyDataSetChanged();
                         }
                     });
 
                 }
 
             }
         });
 
         return view;
     }
 
     @Override
     public Object getItem( int position) {
         return null ;
     }
 
     @Override
     public long getItemId( int position) {
         return 0 ;
     }
}
 
static class ViewHolder {
     TextView tv_appname;
     ImageView img_appicom;
     ImageView img_locked;
}
</appinfo></appinfo></appinfo></appinfo></appinfo>

}

在Service中开启一个活动Activity 在Activity往Service中发送信息采用自定义广播,只有服务才能接收:

活动中:

?
1
2
3
4
Intent intent= new Intent();
intent.setAction( "com.cca.mobilesafe.watchdog" );
intent.putExtra( "packageNmae" ,packageName);
sendBroadcast(intent);

在服务中定义一个内部类广播接收者

?
1
2
3
4
5
6
7
8
private class InnerWatchDogReceiver extends BroadcastReceiver{
     @Override
     public void onReceive(Context context, Intent intent) {
 
         temppackageName = intent.getStringExtra( "packageName" );
 
     }
}

在onCreate方法中注册广播接收者:

?
1
2
3
4
5
//注册一个广播接收者
receiver= new InnerWatchDogReceiver();
IntentFilter filter= new IntentFilter();
filter.addAction( "com.cca.mobilephone.watchdog" );
registerReceiver(receiver, filter);

程序锁的主逻辑

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.cca.mobilephone.service;
import java.util.List;
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;
 
import com.cca.mobilephone.activity.EnterPasswordActivity;
import com.cca.mobilephone.db.dao.AppClockDao;
/**
  * 看门狗服务,监视运行的软件
  * @author Administrator
  *
  */
public class WatchDogLockService extends Service {
 
     private ActivityManager am;
     private boolean flags;
     private AppClockDao dao;
     private InnerWatchDogReceiver receiver;
/**
  * 临时不需要保护的包名
  */
     private String temppackageName;
     @Override
     public IBinder onBind(Intent intent) {
         return null ;
 
     }
 
     @Override
     public void onCreate() {
         super .onCreate();
         //注册一个广播接收者
         receiver= new InnerWatchDogReceiver();
         IntentFilter filter= new IntentFilter();
         filter.addAction( "com.cca.mobilephone.watchdog" );
         registerReceiver(receiver, filter);
 
         //获取活动管理器
         am=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
         flags= true ;
         dao= new AppClockDao( this );
         new Thread(){
                 public void run() {
                 while (flags){
                 //获取任务站里面的情况,对于任务栈里面的信息进行排序,最近使用的排在最前面
                  List<runningtaskinfo> infos = am.getRunningTasks( 100 );
                 String packageName=infos.get( 0 ).topActivity.getPackageName();
                     if (dao.find(packageName)){
                         //程序需要被保护,弹出一个输入密码的对话框
 
                         //再次判断是否需要保护
                         if (packageName.equals(temppackageName)){
                             //暂时不需要保护
                         } else {
                             //需要保护
                             Intent intent = new Intent(getApplicationContext(),EnterPasswordActivity. class );
                             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                             intent.putExtra( "packageName" , packageName);
                             startActivity(intent);
                         }
                     } else {
                         //程序不需要被保护
                     }
                     try {
                         Thread.sleep( 200 );
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                 }
             };
         }.start();
     }
     @Override
     public void onDestroy() {
         super .onDestroy();
         flags= false ;
         unregisterReceiver(receiver);
         receiver= null ;
     }
     /**
      * 定义内部类广播接收者,接收不需要保护的程序包名
      * @author Administrator
      *
      */
     private class InnerWatchDogReceiver extends BroadcastReceiver{
         @Override
         public void onReceive(Context context, Intent intent) {
             temppackageName = intent.getStringExtra( "packageName" );
         }
     }
}
</runningtaskinfo>

程序的优化:考虑每个细节执行的时间,可以优化就优化

当有手机卫士在后台运行时,界面的跳转就不正常了,需要为打开密码保护的程序Activity设置一个模式:打开运用时会在一个新的任务栈中运行,这样就不会跳转回手机卫士了

?
1
android:launchMode= "singleInstance"

在低版本时运行还没弹出密码界面就已经进入程序界面了,可以看到一些信息了,需要优化代码,执行效率更快

?
1
2
List<runningtaskinfo> infos = am.getRunningTasks( 100 );
</runningtaskinfo>

这句代码我们只需要获取第一个运用程序的包名

?
1
2
List<runningtaskinfo> infos = am.getRunningTasks( 1 )定义为;
</runningtaskinfo>

并定义为成员变量

优化:查询数据库所消耗的时间比内存中的多,把数据库查询的数据放在内存中,需要使用时到内存中查找即可

优化程序后发现新增加的要保护的程序保护不了,想到数据库查询时已经把数据存储在内存中固定了,所以要重新查询即更新数据,可以使用广播接收者、也可以使用内容观察者来观察数据库的变化,增加和删除一旦发现,重新查询数据存进内存中。

优化后的代码:电量的优化 屏幕锁屏后和锁屏的广播接收者

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
     /**
  * 看门狗服务,监视运行的软件
  * @author Administrator
  *
  */
public class WatchDogLockService extends Service {
 
     private ActivityManager am;
     private boolean flags;
     private AppClockDao dao;
     private InnerWatchDogReceiver receiver;
      private List<runningtaskinfo> infos ;
     private String packageName;
/**
  * 临时不需要保护的包名
  */
     private String temppackageName;
     /**
      * 内容观察者
      */
     private AppClockDaoObserver observer;
     private Intent intent;
     private List<string> packname;
     @Override
     public IBinder onBind(Intent intent) {
         return null ;
 
     }
 
     @Override
     public void onCreate() {
         super .onCreate();
         //注册一个广播接收者
         receiver= new InnerWatchDogReceiver();
         IntentFilter filter= new IntentFilter();
         filter.addAction( "com.cca.mobilephone.watchdog" );
         filter.addAction(Intent.ACTION_SCREEN_OFF);
         filter.addAction(Intent.ACTION_SCREEN_ON);
         registerReceiver(receiver, filter);
 
         intent = new Intent(getApplicationContext(),EnterPasswordActivity. class );
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 
         //获取活动管理器
         am=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
 
         dao= new AppClockDao( this );
         packname = dao.findAll();
 
         //注册一个内容观察者
         Uri uri=Uri.parse( "content://com.cca.mobilephone.appclockdb" );
         observer= new AppClockDaoObserver( new Handler());
         getContentResolver().registerContentObserver(uri, true , observer);
 
         showWhatchDogStart();
     }
     /*
     *对电量的优化,锁屏后停止监控
     *
     */
     public void showWhatchDogStart() {
         if (flags== false ){
             return ;
         }
         flags= true ;
         new Thread(){
                 public void run() {
                 while (flags){
                 //获取任务站里面的情况,对于任务栈里面的信息进行排序,最近使用的排在最前面
                     infos= am.getRunningTasks( 1 );
                     packageName=infos.get( 0 ).topActivity.getPackageName();
                     if (packname.contains(packageName)){
                         //程序需要被保护,弹出一个输入密码的对话框
                         //再次判断是否需要保护
                         if (packageName.equals(temppackageName)){
                             //暂时不需要保护
                         } else {
                             //需要保护
 
                             intent.putExtra( "packageName" , packageName);
                             startActivity(intent);
                         }
                     } else {
                         //程序不需要被保护
                     }
                     try {
                         Thread.sleep( 30 );
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                 }
             };
         }.start();
     }
     @Override
     public void onDestroy() {
         super .onDestroy();
         flags= false ;
         unregisterReceiver(receiver);
         receiver= null ;
     }
     /**
      * 定义内部类广播接收者
      * @author Administrator
      *
      */
     private class InnerWatchDogReceiver extends BroadcastReceiver{
         @Override
         public void onReceive(Context context, Intent intent) {
             if ( "com.cca.mobilephone.watchdog" .equals(intent.getAction())){
                 temppackageName = intent.getStringExtra( "packageName" );
             } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())){
                 //屏幕锁屏
                 temppackageName= null ;
                 flags= false ;
             } else if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())){
                 //屏幕解锁
                 showWhatchDogStart();
             }
         }
     }
     /**
      * 定义内容观察者内部类
      * @author Administrator
      *
      */
     private class AppClockDaoObserver extends ContentObserver{
 
         public AppClockDaoObserver(Handler handler) {
             super (handler);
         }
         //观察到数据库内容发生变化
         @Override
         public void onChange( boolean selfChange) {
             super .onChange(selfChange);
             System.out.println( "内容观察者观察到数据库发生变化了" );
             packname = dao.findAll();
         }
     }
}
</string></runningtaskinfo>

目录
相关文章
|
5天前
|
Android开发 数据安全/隐私保护 虚拟化
安卓手机远程连接登录Windows服务器教程
安卓手机远程连接登录Windows服务器教程
26 4
|
25天前
|
安全 搜索推荐 Android开发
Android vs. iOS:解锁智能手机操作系统的奥秘####
【10月更文挑战第21天】 在当今这个数字化时代,智能手机已成为我们生活中不可或缺的伙伴。本文旨在深入浅出地探讨两大主流操作系统——Android与iOS的核心差异、优势及未来趋势,帮助读者更好地理解这两个平台背后的技术哲学和用户体验设计。通过对比分析,揭示它们如何塑造了我们的数字生活方式,并展望未来可能的发展路径。无论您是技术爱好者还是普通用户,这篇文章都将带您走进一个充满创新与可能性的移动世界。 ####
44 3
|
1月前
|
Ubuntu Linux Android开发
termux+anlinux+Rvnc viewer来使安卓手机(平板)变成linux服务器
本文介绍了如何在Android设备上安装Termux和AnLinux,并通过这些工具运行Ubuntu系统和桌面环境。
131 2
termux+anlinux+Rvnc viewer来使安卓手机(平板)变成linux服务器
|
27天前
|
Android开发
Android面试高频知识点(1) 图解Android事件分发机制
Android面试高频知识点(1) 图解Android事件分发机制
|
27天前
|
消息中间件 存储 Java
Android面试高频知识点(2) 详解Android消息处理机制(Handler)
Android面试高频知识点(2) 详解Android消息处理机制(Handler)
|
27天前
|
XML 前端开发 Android开发
Android面试高频知识点(3) 详解Android View的绘制流程
Android面试高频知识点(3) 详解Android View的绘制流程
Android面试高频知识点(3) 详解Android View的绘制流程
|
30天前
|
消息中间件 Android开发 索引
Android面试高频知识点(4) 详解Activity的启动流程
Android面试高频知识点(4) 详解Activity的启动流程
26 3
|
1月前
|
XML 前端开发 Android开发
Android面试高频知识点(3) 详解Android View的绘制流程
Android面试高频知识点(3) 详解Android View的绘制流程
24 2
|
1月前
|
消息中间件 存储 Java
Android面试高频知识点(2) 详解Android消息处理机制(Handler)
Android面试高频知识点(2) 详解Android消息处理机制(Handler)
48 1
|
1月前
|
Android开发
Android面试高频知识点(1) 图解 Android 事件分发机制
Android面试高频知识点(1) 图解 Android 事件分发机制
39 1
下一篇
无影云桌面