Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android widget使用

android widget使用

編輯:關於Android編程

在 \res\xml下新建example_appwidget_info.xml



在layout下新建process_widget.xml,用來顯示widget的布局




    

        

        

        
    

    

        

            

            
        

        


新建一個類ProcessWidget繼承AppWidgetProvider

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

public class ProcessWidget extends AppWidgetProvider {
	
	Intent intent;
	
	
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
		intent = new Intent(context,UpdateWidgetService.class);
		context.stopService(intent);
	}

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

}

新建類UpdateWidgetService實現widget更新

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;

public class UpdateWidgetService extends Service {
	private Timer timer ;
	private TimerTask task;
	private AppWidgetManager  widgetmanager;
	@Override
	public IBinder onBind(Intent intent) {
		// 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 name = 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, "進程數目"+TaskUtil.getProcessCount(getApplicationContext()));
				views.setTextColor(R.id.process_count, Color.RED);
				views.setTextViewText(R.id.process_memory, "可用內存"+TaskUtil.getMemeorySize(getApplicationContext()));
				views.setTextColor(R.id.process_memory, Color.RED);
				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(name, views);
				
			}
		};
		timer.scheduleAtFixedRate(task, 1000, 2000);
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		timer.cancel();
		timer = null;
		task =null;
		super.onDestroy();
	}

}
TaskUtil的代碼如下:

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.text.TextUtils;

public class TaskUtil {

	public static void killAllProcess(Context context) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List runningapps = am.getRunningAppProcesses();
		for (RunningAppProcessInfo info : runningapps) {
			String packname = info.processName;
			am.killBackgroundProcesses(packname);
		}

	}
	public static int getProcessCount(Context context) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List runningapps = am.getRunningAppProcesses();
		return runningapps.size();

	}
	public static String getMemeorySize(Context context) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		MemoryInfo outInfo = new ActivityManager.MemoryInfo();
		am.getMemoryInfo(outInfo);
		return TextFormater.getDataSize( outInfo.availMem);

	}
}

在AndroidManifest.xml的配置

 
    
        
    
    
  



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved