Widget窗口小部件
阅读原文时间:2021年04月21日阅读:1

查看文档中app components--->App Widget

1.编写一个类继承AppWidgetProvider

这里开启了服务,如果服务没写,就会在创建窗口小部件时报错,可以把开启,关闭服务的功能屏蔽(这里没有屏蔽),要不会报错

package com.example.mobilesafe.provider;

import java.security.Provider;
import java.util.Timer;
import java.util.TimerTask;

import com.example.mobilesafe.service.updateWeightViewService;

import android.annotation.SuppressLint;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;

public class MyAppWidgetProvider extends AppWidgetProvider {
    @Override
    public void onEnabled(Context context) {
        context.startService(new Intent(context, updateWeightViewService.class));
        super.onEnabled(context);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, updateWeightViewService.class));
        super.onReceive(context, intent);
    }

    @SuppressLint("NewApi")
    @Override
    public void onAppWidgetOptionsChanged(Context context,
            AppWidgetManager appWidgetManager, int appWidgetId,
            Bundle newOptions) {
        // TODO Auto-generated method stub
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,
                newOptions);
    }

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

2.在清单文件里配置这个类

 <receiver android:name="com.example.mobilesafe.provider.MyAppWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

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

 是类名

 android:resource="@xml/my_appwidget_info" />是一个配置文件

3.配置文件

    在xml下创建一个配置文件,名字叫my_appwidget_info

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/process_widget"
    android:minHeight="72.0dip"
    android:minWidth="294.0dip"
    android:updatePeriodMillis="0" />

    android:initialLayout="@layout/process_widget"是布局文件

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:id="@+id/ll_Weight"
    android:background="@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/tv_processCount"
            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/tv_processSpace"
            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:src="@drawable/ic_launcher" />

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

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

</LinearLayout>

5到这里显示工作就完成了一大部分,但是这个窗口小部件不能显示数据,点击也没有响应(因为第一步那个服务还没写),甚至会报错,下面就是通过第一步那个没有实现的服务实现所有功能

6.服务

package com.example.mobilesafe.service;

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

import com.example.mobilesafe.R;
import com.example.mobilesafe.engine.AppProcessEngine;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.RemoteViews;

public class updateWeightViewService extends Service {

    private myReceiver myReceiver;
    private TimerTask timerTask;
    private Timer timer;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        myReceiver = new myReceiver();
        registerReceiver(myReceiver, intentFilter);
        startUpdate();
        super.onCreate();
    }

    /**
     * 开启任务
     */
    private void startUpdate() {
        timer = new Timer();
        timerTask = new TimerTask() {

            @Override
            public void run() {
                updateWeight();
            }

        };
        timer.schedule(timerTask, 0, 5000);
    }

    /**
     * 更新控件
     */
    private void updateWeight() {
        AppWidgetManager aWM = AppWidgetManager
                .getInstance(getApplicationContext());
        RemoteViews views = new RemoteViews(getPackageName(),
                R.layout.process_widget);
        // 设置文本更新1
        views.setTextViewText(
                R.id.tv_processCount,
                "进程数:"
                        + AppProcessEngine
                                .getProcessCount(getApplicationContext()));
        // 设置文本更新2
        views.setTextViewText(
                R.id.tv_processSpace,
                "内存:"
                        + Formatter
                                .formatFileSize(
                                        getApplicationContext(),
                                        AppProcessEngine
                                                .getAvlibeProcessSpace(getApplicationContext())));

        // 点击打开应用
        Intent intent = new Intent("android.intent.action.HOME");
        intent.addCategory("android.intent.category.DEFAULT");
        PendingIntent pendingIntent = PendingIntent.getActivity(
                getApplicationContext(), 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.ll_Weight, pendingIntent);
        // 点击清理内存
        Intent intent2 = new Intent(
                "android.intent.action.KILL_BACKGROUND_PROCESS");

        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 0,
                intent2, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.bt_clear, pendingIntent2);
        // 得到componentName
        ComponentName componentName = new ComponentName(
                getApplicationContext(),
                "com.example.mobilesafe.provider.MyAppWidgetProvider");
        aWM.updateAppWidget(componentName, views);
    }

    /**
     * 停止任务
     */
    private void stopUpdate() {
        if (timer != null) {
            timer.cancel();
        }
    }

    private class myReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
                startUpdate();
            } else {
                stopUpdate();
            }
        }
    }

    @Override
    public void onDestroy() {
        if (myReceiver != null) {
            unregisterReceiver(myReceiver);
        }
        stopUpdate();
        super.onDestroy();
    }
}

这里还有那个点击按钮,开启广播功能的那个广播没写,这里就不写了