1、AndroidManifest.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mars.appwidget02"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="mars.appwidget02.Activity01"
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="ExampleAppWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<!-- 加了新的 -->
<intent-filter>
<action android:name="mars.appwidget02.UPDATE_APP_WIDGET" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info"/>
</receiver>
</application>
</manifest>
2、布局文件:
(1、)main.xml代码:[===Activity01.java]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Activity01" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main" />
</RelativeLayout>
(2、)example_appwidget.xml代码:[===ExampleAppWidgetProvider.java]
<?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/apptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="appwidth"
android:textSize="20sp"
android:background="#000000"/>
<ImageView
android:id="@+id/imageid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/xiao"/>
<Button
android:id="@+id/appbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/appbutton"/>
</LinearLayout>
3、运行调用:
(1、)Activity01.java代码(无改变)
package mars.appwidget02;
import android.os.Bundle;
import android.app.Activity;
public class Activity01 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
2、)ExampleAppWidgetProvider.java代码:(实现AppWidgetProvider)
package mars.appwidget02;
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.widget.RemoteViews;
public class ExampleAppWidgetProvider extends AppWidgetProvider{
//常量字符串,用于命名Action
private String UPDATE_ACTION="mars.appwidget02.UPDATE_APP_WIDGET";
//1、onUpdate
//context 上下文对象
//appWidgetManager 专门管理appWidget对象
//appWidgetIds 为每次添加进来appWidget的绑定一个ID
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
//创建一个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_appwidget);
remoteviews.setOnClickPendingIntent(R.id.appbutton,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds,remoteviews);
}
//2、onDeleted
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
//3
public void onDisabled(Context context){
super.onDisabled(context);
}
//4
public void onEnabled(Context context){
super.onEnabled(context);
}
//5
public void onReceive(Context context,Intent intent){
String action=intent.getAction();
//如果是自己的,就更改状态
if(UPDATE_ACTION.equals(action)){
RemoteViews remoteViews=new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
remoteViews.setImageViewResource(R.id.imageid,R.drawable.xiao2);
remoteViews.setTextViewText(R.id.apptext, "text");
AppWidgetManager awm=AppWidgetManager.getInstance(context);
//RemoteViews是几个小控件,ComponentName是整个
//第二个参数:ExampleAppWidgetProvider这个类的class对象
ComponentName cn=new ComponentName(context,
ExampleAppWidgetProvider.class);
//更新
awm.updateAppWidget(cn,remoteViews);
}else{
//不是自己的,就调用父类的
super.onReceive(context,intent);
}
}
}
4、 运行效果:
自动运行的界面:
桌面显示部件:
点击”换文字和图片“后显示的桌面: