挖穿Android第五十六天

挖穿Android第五十六天系统进程显示和隐藏1.创建进程管理设置页面:ProcessManagerSettingActivity2.编写设置页面布局文件3.监听Check

大家好,欢迎来到IT知识分享网。

挖穿Android第五十六天

系统进程显示和隐藏

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() {
 @Override
public 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的显示个数

@Override
public 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);
}
@Override
protected 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();
 }
 }
 }

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/51210.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信