Android Widget的写法

Xml   example_appwidget_info.xml




 

界面:process_widget.xml




    

        

        

        
    

    

        

            

            
        

        


 

Service  自己 在清单文件中配置信息

package cn.itcast.mobilesafe.receiver.service;

import java.util.Timer;
import java.util.TimerTask;


import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.os.IBinder;
import android.widget.RemoteViews;
import cn.itcast.mobilesafe.R;
import cn.itcast.mobilesafe.receiver.LockScreenReceiver;
import cn.itcast.mobilesafe.util.TaskUtil;

public class UpdateWidgetService extends Service {
	private Timer timer;
	private TimerTask task;
	private AppWidgetManager widgetmanager;
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void onCreate() {
		timer = new Timer();
		widgetmanager = AppWidgetManager.getInstance(getApplicationContext());
		task = new TimerTask() {
			
			@Override
			public void run() {
				//更新widget界面
				ComponentName componentname = new ComponentName("cn.itcast.mobilesafe","cn.itcast.mobilesafe.provider.ProcessWidget");
				RemoteViews views =  new RemoteViews("cn.itcast.mobilesafe",R.layout.process_widget);
				views.setTextViewText(R.id.process_count,"进程数目"+TaskUtil.getProcessCount(getApplicationContext()));
				views.setTextViewText(R.id.process_memory,"可用内存"+TaskUtil.getMemeorySize(getApplicationContext()));
				views.setTextColor(R.id.process_count,Color.RED);
				views.setTextColor(R.id.process_memory,Color.BLUE);
				Intent intent = new Intent(UpdateWidgetService.this,LockScreenReceiver.class);
				PendingIntent pendingintent = PendingIntent.getBroadcast(getApplicationContext(),0, intent,0);
						
				views.setOnClickPendingIntent(R.id.btn_clear, pendingintent);
				widgetmanager.updateAppWidget(componentname, views);
			}
		};
		//延迟1秒钟执行 每隔2000刷新一次
		timer.scheduleAtFixedRate(task, 1000,2000);
		super.onCreate();
	}
	@Override
	public void onDestroy() {
		timer.cancel();
		timer = null;
		task = null;
		super.onDestroy();
	}
}


provider   ProcessWidget

package cn.itcast.mobilesafe.provider;

import cn.itcast.mobilesafe.receiver.service.UpdateWidgetService;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;

public class ProcessWidget extends AppWidgetProvider{
	Intent intent;
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onDeleted(context, appWidgetIds);
		context.stopService(intent);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
	}
	
	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		super.onEnabled(context);
		intent = new Intent(context,UpdateWidgetService.class);
		context.startService(intent);//开启服务
	}
	
}


 

你可能感兴趣的:(Adroid,Widget,桌面小空间)