Android AppWidgetProvider使用简例

AppWidgetProvider是一个BroadcastReceiver,必须在AndroidManifest.xml中声明该Receiver,并接收“Android.appwidget.action.APPWIDGET_UPDATE”。AppWidgetProvider使开发者能够自定义的桌面小工具。本文介绍一个桌面时钟的小工具。用户安装程序后需要在手机窗口小工具中添加,方可查看到。

继承AppWidgetProvider类,实现update方法。

package com.example.desktopdemo;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;

public class EX04_28 extends AppWidgetProvider {

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Intent intent = new Intent(context, UpdateService.class);
		context.startService(intent);
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

	public static class UpdateService extends Service {

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

		@SuppressLint("SimpleDateFormat") @Override
		public void onStart(Intent intent,int startId){
		RemoteViews updateViews = new RemoteViews(this.getPackageName(),R.layout.main);
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
		updateViews.setTextViewText(R.id.textView01, ""+sdf.format((new Date())));
		ComponentName thisWidget = new ComponentName(this,EX04_28.class);
		AppWidgetManager manager = AppWidgetManager.getInstance(this);
		manager.updateAppWidget(thisWidget, updateViews);
		}
	}

}

AndroidManifest.xml的定义

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.desktopdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
            android:name=".EX04_28"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data
                android:resource="@xml/my_widget_provider"
                android:name="android.appwidget.provider">
            </meta-data>
        </receiver>
        <service android:name=".EX04_28$UpdateService"></service>
    </application>
</manifest>

my_widget_provider.xml定义(注:android:updatePeriodMillis 这种方式已经在SKD1.5以后被废了,可自定义线程进行刷新

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

main.xml定义:

<?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="vertical" >

    <TextView
        android:id="@+id/textView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="123"
        android:textColor="#F29CB1"
        android:textSize="20sp" />

</LinearLayout>


Demo下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值