android进程管理器桌面小控件的实现

参照android API实现

1.创建ProcessWidget类,继承AppWidgetProvider类(AppWidgetProvider其实就是BroadcastReceiver的子类)

public class ProcessWidget extends AppWidgetProvider {
	private Intent intent;
	@Override
	public void onEnabled(Context context) {
		intent = new Intent(context,UpdateWidgetService.class);
		context.startService(intent);
		
		
		
		super.onEnabled(context);
	}

	@Override
	public void onDisabled(Context context) {
		
		intent = new Intent(context,UpdateWidgetService.class);
		context.stopService(intent);
		
		
		super.onDisabled(context);
	}
	
}

2.在清单文件中声明ProcessWidget类

<receiver android:name=".receiver.ProcessWidget" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/process_appwidget" />
        </receiver>

3.创建UpdateWidgetService类,实现后台定时更新管理器相关信息

public class UpdateWidgetService extends Service {
	
	private Timer timer;
	private TimerTask task;
	private AppWidgetManager awm;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		
		timer = new Timer();
		awm = AppWidgetManager.getInstance(this);
		
		
		task = new TimerTask(){
			@Override
			public void run() {
				
				ComponentName provider = new ComponentName("cn.itcast.mobilesafe", "cn.itcast.mobilesafe.receiver.ProcessWidget");
				RemoteViews views = new RemoteViews("cn.itcast.mobilesafe", R.layout.process_widget);
				views.setTextViewText(R.id.process_count, "进程数目:"+TaskUtils.getProcessCount(getApplicationContext()));
				views.setTextViewText(R.id.process_memory, "剩余内存:"+TaskUtils.getAvaiMemoryInfo(getApplicationContext()));
				views.setTextColor(R.id.process_count, Color.RED);
				views.setTextColor(R.id.process_memory, Color.RED);
				Intent intent = new Intent(getApplicationContext(),LockScreenReceiver.class);
				PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
				views.setOnClickPendingIntent(R.id.btn_clear, pendingIntent);
				
				
				awm.updateAppWidget(provider, views);
			}
		};
		
		
		timer.schedule(task, 1000, 2000);
		
		super.onCreate();
	}
	
	@Override
	public void onDestroy() {
		timer.cancel();
		timer = null;
		task = null;
		super.onDestroy();
	}

}

4.定义桌面小控件的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/widget_bg_portrait"
    android:gravity="center_vertical" >

    <LinearLayout
        android:layout_width="0.0dip"
        android:layout_height="fill_parent"
        android:layout_marginLeft="5.0dip"
        android:layout_weight="1.0"
        android:background="@drawable/widget_bg_portrait_child"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:paddingBottom="3.0dip"
        android:paddingTop="3.0dip" >

        <TextView
            android:id="@+id/process_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10.0dip"
            />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1.0dip"
            android:layout_marginTop="1.0dip"
            android:background="@drawable/widget_bg_portrait_child_divider" />

        <TextView
            android:id="@+id/process_memory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10.0dip"
          />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" >

            <ImageView
                android:layout_width="20.0dip"
                android:layout_height="20.0dip"
                android:background="@drawable/notification" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                />
        </LinearLayout>

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="90.0dip"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginTop="5.0dip"
            android:background="@drawable/button_background_selected"
            android:text="一键清理"
            />
    </LinearLayout>

</LinearLayout>
5.定义清理进程的广播事件

private SharedPreferences sp;

	private static final String TAG = "LockScreenReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		//屏幕锁屏的时候调用的方法
		Log.i(TAG, "锁屏调用的事件");
		Log.i(TAG, "锁屏调用的事件");
		sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		boolean lockScreenKill = sp.getBoolean("lockScreenKill", false);
		if(lockScreenKill){
			TaskUtils.killAllProcess(context);
			Log.i(TAG, "杀死所有进程");
		}
		
		//发送自定义的广播事件
	}







你可能感兴趣的:(android进程管理器桌面小控件的实现)