android中如何在窗口小插件(widget),中使用自定义进度条

各位大大,最近在开发一个任务管理器,widget部分要做一个进度条显示当前内存使用情况,点击能清理内存,app运行后往桌面添加widget提示小插件加载故障,我的widgetProvider类如下:

package com.example.android_dev.taskkiller.activity;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import android.util.Log;

import android.widget.RemoteViews;
import android.widget.Toast;

import com.example.android_dev.taskkiller.R;

import com.example.android_dev.taskkiller.Service.MyWidgetService;
import com.example.android_dev.taskkiller.biz.Process_biz;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


/**
 * Created by Android-DEV on 2015/10/22.
 */
public class MyWidgetProvider extends AppWidgetProvider{
    private  int progress ;
    String availMemStr;
    String info;

    private static final String TAG = "MyWidgetProvider";

    /**  启动AppWidgetService服务对应的action  */
    private final Intent my_widget_service_intent=new Intent("android.action.MY_WIDGET_SERVICE");


    /** 更新 widget 的广播对应的action */
    private final String ACTION_WIDGET_RECEIVE="com.example.task_killer.widget.click";


    /** 保存 widget 的id的HashSet,每新建一个 widget 都会为该 widget 分配一个 id*/
    private static Set idsSet = new HashSet();

    /** 图片信息 */
    private static final int Layout_Show = 1;

    /** 内存使用情况 */
   private static double totality;   //总内存
   private static double available;   //可用内存

/*    @ViewInject(R.id.widget_progress_bar)
    private ArcProgressBar  arcProgressBar;*/

    private boolean DEBUG = false;



    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {

        super.onDeleted(context, appWidgetIds);
        /** 当 widget 被删除时,对应的删除set中保存的widget的id */
        for (int appWidgetId : appWidgetIds) {
            idsSet.remove(Integer.valueOf(appWidgetId));
        }
        prtSet();

    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);

        Intent eintent = new Intent(createExplicitFromImplicitIntent(context,my_widget_service_intent));
        context.startService(eintent);

        Log.i("TAG", "onEnabled");
        /** 当前内存使用情况 */
        Process_biz biz = Process_biz.getInstance(context);
        String am = biz.getSystemAvaialbeMemorySize();
        String bm = biz.getTotalMemory();
        totality =  Double.valueOf(bm);
        available = Double.valueOf(am);





    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);

        Log.i("TAG", "onDisabled");
        Intent eintent = new Intent(createExplicitFromImplicitIntent(context,my_widget_service_intent));
        context.stopService(eintent);
    }

    public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
        // Retrieve all services that can match the given intent
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);

        // Make sure only one match was found
        if (resolveInfo == null || resolveInfo.size() != 1) {
            return null;
        }

        // Get component info and create ComponentName
        ResolveInfo serviceInfo = resolveInfo.get(0);
        String packageName = serviceInfo.serviceInfo.packageName;
        String className = serviceInfo.serviceInfo.name;
        ComponentName component = new ComponentName(packageName, className);

        // Create a new intent. Use the old one for extras and such reuse
        Intent explicitIntent = new Intent(implicitIntent);

        // Set the component to be explicit
        explicitIntent.setComponent(component);

        return explicitIntent;
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.i("TAG", "onUpdate");

        /** 每次 widget 被创建时,对应的将widget的id添加到set中  */
        for(int appWidgetId:appWidgetIds){
            idsSet.add(Integer.valueOf(appWidgetId));
        }
        prtSet();



    }


    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.i("TAG","intent = " + intent);
        if(intent.getAction().equals("com.example.task_killer.widget.click")){
            Log.i("TAG", "onReceive");

            updatepWidgets(context, AppWidgetManager.getInstance(context), idsSet);
        }


    }


    private void updatepWidgets(Context context, AppWidgetManager appWidgetManager, Set set){

        /** widget 的id */
        int appID;
        /** 迭代器,用于遍历所有保存的widget的id */
        Iterator it = set.iterator();

        while(it.hasNext()){
            appID = ((Integer)it.next()).intValue();

            /** 获取对应的布局 */
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
            int a =(int) ((totality-available)/totality*100);
            String b = String.valueOf(a);
            remoteViews.setTextViewText(R.id.widget_tv,b+"%");
           // remoteViews.setProgressBar(R.id.widget_progress_bar,(int)totality,(int)(totality-available),true);
            /*double b = /totality;
            arcProgressBar.addProgress(236);*/
            /** 设置点击按钮对应的PendingIntent:即点击按钮时,发送广播。 */
            remoteViews.setOnClickPendingIntent(R.id.widget_layout, getPendingIntent(context, Layout_Show));

            /** 更新widget */
            appWidgetManager.updateAppWidget(appID,remoteViews);
        }

    }

    private PendingIntent getPendingIntent(Context context, int layout_Id) {
        Intent intent = new Intent();
        intent.setClass(context,MyWidgetProvider.class);
        intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
        intent.setData(Uri.parse("custom:" + layout_Id));
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0 );
        return pi;

    }

    /** 调试用:遍历set */
    private void prtSet() {
        if (DEBUG) {
            int index = 0;
            int size = idsSet.size();
            Iterator it = idsSet.iterator();
            Log.d(TAG, "total:"+size);
            while (it.hasNext()) {
                Log.d(TAG, index + " -- " + ((Integer)it.next()).intValue());
            }
        }
    }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值