android典型代码系列(二十六)------App widget的使用

App widget的使用:
第一步、创建widget类

package cn.beita.mobilesafe.receiver;
import cn.beita.mobilesafe.service.AppWightService;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
public class AppWightReceiver extends AppWidgetProvider {
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {        
        super.onDeleted(context, appWidgetIds);
        Intent intent=new Intent(context,AppWightService.class);
        context.stopService(intent);
    }
    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Intent intent=new Intent(context,AppWightService.class);
        context.startService(intent);
        System.out.println("Intent intent=new Intent(context,AppWightService.class);");
    }   
}

第二步、在清单文件中进行配置

      <receiver android:name=".receiver.AppWightReceiver" >
            <intent-filter >
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data  android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info" />
        </receiver>

第三步、xml— example_appwidget_info

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="72dp" android:updatePeriodMillis="0" android:initialLayout="@layout/example_appwidget">
</appwidget-provider>

第四步、创建布局文件:layout---example_appwidget

<?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="horizontal" android:background="@drawable/widget_bg_portrait" android:gravity="center_vertical|center_horizontal">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/widget_bg_portrait_child" android:orientation="vertical" android:gravity="center_vertical" >
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginTop="3dip" android:layout_marginBottom="3dip" android:id="@+id/tv_widget_count" android:text="进程数目"/>
        <ImageView android:layout_width="fill_parent" android:layout_height="1dip" android:background="@drawable/widget_bg_portrait_child_divider"/>
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tv_widget_memeory" android:layout_marginLeft="10dip" android:layout_marginTop="3dip" android:layout_marginBottom="3dip" android:text="可用内存"/>

    </LinearLayout>
    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">
        <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
            <ImageView android:layout_width="20dip" android:layout_height="20dip" android:src="@drawable/notification" android:id="@+id/iv_appname" android:layout_margin="4dip"/>
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_appname" android:layout_toRightOf="@id/iv_appname" android:layout_alignBaseline="@id/iv_appname" android:layout_marginTop="5dip" android:text="手机卫士"/>
        </RelativeLayout>
        <Button android:layout_width="wrap_content" android:layout_marginLeft="10dip" android:paddingLeft="5dip" android:paddingRight="10dip" android:id="@+id/bt_example_appwidget" android:layout_height="wrap_content" android:background="@drawable/button_shape" android:text="一键清理"/>
    </LinearLayout>
</LinearLayout>

第五步、创建service时时地更新上面的appwidget

package cn.beita.mobilesafe.service;
public class AppWightService extends Service {
    private AppWidgetManager widgetManager;
    private ScheduledExecutorService pool;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        widgetManager=AppWidgetManager.getInstance(getApplicationContext());
        System.out.println("widgetManager=AppWidgetManager.ge");
        pool = Executors.newScheduledThreadPool(1);
        pool.scheduleAtFixedRate(new Runnable(){
                @Override
                public void run() {
                    ComponentName componentName=new ComponentName("cn.beita.mobilesafe", "cn.beita.mobilesafe.receiver.AppWightReceiver");
                    RemoteViews view = new RemoteViews("cn.beita.mobilesafe", R.layout.example_appwidget);
                    view.setTextViewText(R.id.tv_widget_count, "当前进程数为:"+TaskUtils.getAllProcess(AppWightService.this));
                    view.setTextColor(R.id.tv_widget_count, Color.RED);
                    view.setTextViewText(R.id.tv_widget_memeory, "当前可用内存为:"+TextFormat.getDataSize(TaskUtils.getAvailMemeoty(AppWightService.this)));
                    view.setTextColor(R.id.tv_widget_memeory, Color.RED);
                    Intent intent=new Intent(AppWightService.this,LockClearReceiver.class);
                    PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
                    view.setOnClickPendingIntent(R.id.bt_example_appwidget, pendingIntent);
                    widgetManager.updateAppWidget(componentName, view);
                }
        }, 1, 2, TimeUnit.SECONDS);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        pool.shutdown();
    }
}

你可能感兴趣的:(android)