1.什么是AppWidget?
所谓的AppWidget就是指android手机桌面上的一个小控件
2.在学习AppWidget前,先了解与之相关的两个概念:
App WigdetProviderInfo对象:
为App Wigdet提供元数据(元数据表示:描述数据的数据,例如XML标签,关系型数据库的表结构),包括布局更新频率等等数据。这个对象被定义在XML文件当中(这个对象是android系统根据我们所定义XML文件自动生成的)
App WigdetProvider
定义了App Wigdet的基本生命周期函数
3.创建AppWidget步骤:
A.定义App WigdetProviderInfo:在res/xml文件夹当中定义一个名为example_appwigdet_info.xml的文件:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp" android:minHeight="80dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/example_appwigdet" >
</appwidget-provider>
B.为AppWidget制定样式和布局,定义一个新的布局文件example_appwigdet.Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:text="第一个文本框"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#eeefff" />
<ImageView android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ku" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一个AppWidget"
android:textColor="#eeeeee"
android:textSize="18px" />
</LinearLayout>
C.实现App WigdetProvider
public class AppWidgetPro extends AppWidgetProvider {
private static final String UPDATE_ACTION = "delia.appwidget.action.UPDATE_APP_WIDGET";
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
//System.out.println("onDeleted");
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
//System.out.println("onDisabled");
super.onDisabled(context);
}
@Override
public void onEnabled(Context context) {
System.out.println("onEnabled");
super.onEnabled(context);
}
@Override
public void onReceive(Context context, Intent intent) {
//System.out.println("onReceive");
super.onReceive(context, intent);
String action = intent.getAction();
if(UPDATE_ACTION.equals(action)){
//System.out.println("onReceive-->" + UPDATE_ACTION);
RemoteViews remote = new RemoteViews(context.getPackageName(), R.layout.example_appwigdet);
remote.setTextViewText(R.id.tv, "转变后的textview");
remote.setTextColor(R.id.tv, Color.MAGENTA);
remote.setImageViewResource(R.id.img, R.drawable.xiao);
AppWidgetManager appwidgetmanager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, AppWidgetPro.class);
appwidgetmanager.updateAppWidget(componentName, remote);
}else{
super.onReceive(context, intent);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//System.out.println("onUpdate");
//创建一个Intent对象
Intent intent = new Intent();
//为Intent对象设置Action
intent.setAction(UPDATE_ACTION);
//使用getBroadcast方法,得到一个PendingIntent对象,当对象执行时,会发送一个广播
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews remoteviews = new RemoteViews(context.getPackageName(), R.layout.example_appwigdet); remoteviews.setOnClickPendingIntent(R.id.button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteviews);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
D.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="delia.app"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Appwigdet02"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="AppWidgetPro">
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter> <intent-filter >
<action android:name="delia.appwidget.action.UPDATE_APP_WIDGET"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/example_appwigdet_info"/>
</receiver>
</application>
</manifest>