网站首页 > 资讯 >隐藏进程(隐藏进程的软件)

隐藏进程(隐藏进程的软件)

跟我敲代码-56

系统进程显示和隐藏

1.创建进程管理设置页面:ProcessManagerSettingActivity

2.编写设置页面布局文件

3.监听Checkbox的勾选事件,更新本地SharePreference

根据本地记录,更新checkbox状态

 boolean showSystem = mPrefs.getBoolean("show_system_process", true); if (showSystem) { cbShowSystem.setChecked(true); cbShowSystem.setText("显示系统进程"); } else { cbShowSystem.setChecked(false); cbShowSystem.setText("不显示系统进程"); }

设置状态勾选监听

cbShowSystem.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { cbShowSystem.setText("显示系统进程");mPrefs.edit().putBoolean("show_system_process", true).commit(); } else { cbShowSystem.setText("不显示系统进程"); mPrefs.edit().putBoolean("show_system_process", false) .commit(); } } });

4.根据sp记录的是否显示系统进程,更新listview的显示个数

@Overridepublic int getCount() { // 通过判断是否显示系统进程,更新list的数量 boolean showSystem = mPrefs.getBoolean("show_system_process", true); if (showSystem) { return 1 + mUserProcessList.size() + 1 + mSystemProcessList.size(); } else { return 1 + mUserProcessList.size(); }}

5.保证勾选框改变后,listview可以立即刷新

public void setting(View view) { startActivityForResult(new Intent(this, ProcessManagerSettingActivity.class), 0);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { // 当从设置页面回跳回来之后,刷新listview mAdapter.notifyDataSetChanged();}

锁屏清理

1.判断锁屏清理的广播是否正在运行

boolean serviceRunning = ServiceStatusUtils.isServiceRunning( "com.ziyang.mobilesafeteach.service.AutoKillService", this); if (serviceRunning) { cbLockClear.setChecked(true); cbLockClear.setText("当前状态:锁屏清理已经开启"); } else { cbLockClear.setChecked(false); cbLockClear.setText("当前状态:锁屏清理已经关闭"); } cbLockClear.setOnCheckedChangeListener(new OnCheckedChangeListener() {  @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Intent intent = new Intent(ProcessManagerSettingActivity.this, AutoKillService.class); if (isChecked) { // 启动锁屏清理的服务 startService(intent); cbLockClear.setText("当前状态:锁屏清理已经开启"); } else { // 关闭锁屏清理的服务 stopService(intent); cbLockClear.setText("当前状态:锁屏清理已经关闭"); } } });

2.锁屏清理进程的服务

  public class AutoKillService extends Service { private InnerScreenOffReceiver mReceiver; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); //监听屏幕关闭的广播, 该广播只能在代码中注册,不能在清单文件中注册 mReceiver = new InnerScreenOffReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_OFF); registerReceiver(mReceiver, filter); } @Override public void onDestroy() { super.onDestroy(); unregisterReceiver(mReceiver); mReceiver = null; }

3.锁屏关闭的广播接收者

 class InnerScreenOffReceiver extends BroadcastReceiver {   @Override public void onReceive(Context context, Intent intent) { System.out.println("屏幕关闭..."); // 杀死后台所有运行的进程 ActivityManager am =(ActivityManager)getSystemService(ACTIVITY_SERVICE); List<RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses(); for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) { // 跳过手机卫士的服务 if(runningAppProcessInfo.processName.equals(ctx.getPackageName())) { return; } am.killBackgroundProcesses(runningAppProcessInfo.processName); } } }}

4.定时器清理

 // 在AutoKillService的onCreate中启动定时器,定时清理任务 mTimer = new Timer(); mTimer.schedule(new TimerTask() { @Override public void run() { System.out.println("5秒运行一次!"); } }, 0, 5000); @Override protected void onDestroy() { super.onDestroy(); mTimer.cancel(); mTimer = null; }

桌面Widget(窗口小部件)

1. 在com.ziyang.mobilesafe.receiver目录下创建MyWidget并继承AppWidgetProvider

2. 在功能清单文件注册,参照文档

<receiver android:name=".receiver.MyWidget" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_info" /> </receiver>

3. 在res/xml/创建文件example_appwidget_info.xml拷贝文档内容

<appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="72dp"//能被调整的最小宽高,大于minWidth minHeight忽略 android:updatePeriodMillis="86400000"//更新周期,毫秒,最短默认半小时 android:previewImage="@drawable/preview"//默认是ic_launcher android:initialLayout="@layout/example_appwidget"//布局文件android:configure="com.example.android.ExampleAppWidgetConfigure"android:resizeMode="horizontal|vertical"//widget可以被拉伸的方向android:widgetCategory="home_screen|keyguard"//在屏幕主页和锁屏状态也能显示android:initialKeyguardLayout="@layout/example_keyguard"//锁屏状态显示样式></appwidget-provider>

4. 精简example_appwidget_info.xml文件,最终结果:

<appwidget-provider-xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp"  android:minHeight="72dp" android:updatePeriodMillis="1800000" android:initialLayout="@layout/appwidget"></appwidget-provider>

5. widget布局文件:appwidget.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" > <TextView  android:id="@+id/textView1"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:background="#f00"  android:text="我是widget,哈哈哈"  android:textSize="30sp" /> </LinearLayout>

widget生命周期

public class MyWidget extends AppWidgetProvider { /** * widget的每次变化都会调用onReceive */ @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); System.out.println("MyWidget: onReceive"); } /** * 当widget第一次被添加时,调用onEnable */ @Override public void onEnabled(Context context) { super.onEnabled(context); System.out.println("MyWidget: onEnabled"); } /** * 当widget完全从桌面移除时,调用onDisabled */ @Override public void onDisabled(Context context) { super.onDisabled(context); System.out.println("MyWidget: onDisabled"); } /** * 新增widget时,或者widget更新时,调用onUpdate * 更新时间取决于xml中配置的时间,最短为半小时 */ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); System.out.println("MyWidget: onUpdate");  }  /** * 删除widget时,调onDeleted */ @Override public void onDeleted(Context context, int[] appWidgetIds) { super.onDeleted(context, appWidgetIds); System.out.println("MyWidget: onDeleted"); } /** * 当widget大小发生变化时,调用此方法 */ @Override public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) { System.out.println("MyWidget: onAppWidgetOptionsChanged"); } } 

更新widget方法

 /** * 定时更新widget的service *  * @author Kevin *  */ public class UpdateWidgetService extends Service {  private Timer mTimer; private AppWidgetManager mAWM;  @Override public IBinder onBind(Intent intent) { return null; }  @Override public void onCreate() { super.onCreate(); mAWM = AppWidgetManager.getInstance(this); // 启动定时器,每个5秒一更新 mTimer = new Timer(); mTimer.schedule(new TimerTask() {  @Override public void run() { System.out.println("更新widget啦!"); updateWidget(); } }, 0, 5000); }  /** * 更新widget */ private void updateWidget() { // 初始化远程的view对象 RemoteViews views = new RemoteViews(getPackageName(), R.layout.process_widget);  views.setTextViewText(R.id.tv_running_processes, "正在运行的软件:" + ProcessInfoProvider.getRunningProcessNum(this)); views.setTextViewText( R.id.tv_memory_left, "可用内存:" + Formatter.formatFileSize(this, ProcessInfoProvider.getAvailMemory(this)));  // 初始化组件 ComponentName provider = new ComponentName(this, MyWidget.class);  // 更新widget mAWM.updateAppWidget(provider, views); }  @Override public void onDestroy() { super.onDestroy(); mTimer.cancel(); mTimer = null; } }

点击事件处理

 // 初始化延迟意图,pending是等待的意思 Intent intent = new Intent(this, HomeActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  // 当点击widget布局时,跳转到主页面 views.setOnClickPendingIntent(R.id.ll_root, pendingIntent);  //当一键清理被点击是,发送广播,清理内存 Intent btnIntent = new Intent(); btnIntent.setAction("com.ziyang.mobilesafeteach.KILL_ALL"); PendingIntent btnPendingIntent = PendingIntent.getBroadcast(this, 0, btnIntent, PendingIntent.FLAG_UPDATE_CURRENT); views.setOnClickPendingIntent(R.id.btn_clear, btnPendingIntent); ---------------------------  /** * 杀死后台进程的广播接受者 * 清单文件中配置action="com.ziyang.mobilesafeteach.KILL_ALL" *  * @author Kevin *  */ public class KillAllReceiver extends BroadcastReceiver {  @Override public void onReceive(Context context, Intent intent) { System.out.println("kill all..."); // 杀死后台所有运行的进程 ProcessInfoProvider.killAll(context); } } --------------------------- <receiver android:name=".receiver.KillAllReceiver" > <intent-filter> <action android:name="com.ziyang.mobilesafeteach.KILL_ALL" /> </intent-filter> </receiver>

做一个有情怀的程序员, 拒绝耗电!

 当锁屏关闭时,停止widget定时器的更新 UpdateWidgetService: // 注册屏幕开启和关闭的广播接受者 mReceiver = new InnerScreenReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); registerReceiver(mReceiver, filter); /** * 屏幕关闭和开启的广播接收者 *  * @author Kevin *  */ class InnerScreenReceiver extends BroadcastReceiver {  @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (Intent.ACTION_SCREEN_OFF.equals(action)) {// 屏幕关闭 if (mTimer != null) { // 停止定时器 mTimer.cancel(); mTimer = null; } } else {// 屏幕开启 startTimer(); } } }

相关内容

隐藏进程(隐藏进程的软件)文档下载.: PDF DOC TXT
您可能感兴趣的文章
24h快讯
违法生育包括哪些情形,什么叫违法生育
一、教师如果违法计划生育会有什么处罚如果是教师,违反计划生育规定,应由计生机......
2024-04-05 法规
自残违法吗?自残算不算犯罪
一、教唆他人自残构成犯罪吗教唆他人自残有故意杀人的嫌疑,所以规劝大家不要以身......
2024-04-05 法规
新昌机动车违法?机动车违章处理时间限制
一、新昌货车通行证怎么办理1.办理新昌货车通行证是可行的。2.因为新昌货车通......
2024-04-05 法规
违法举报网站,公安干警违法举报平台
一、国家有奖举报平台以举报违章车辆为例,有奖举报平台如下:为加大监管力度,营......
2024-04-05 法规
查看更多