创建
package d.m;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
/**
* @version 2012-8-7 上午09:50:18
**/
public class wg extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// 刷新界面
Log.e("wg", "onUpdate");
RemoteViews rv = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Intent intent = new Intent("d.m.click");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, 0);
rv.setOnClickPendingIntent(R.id.ll1, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[0], rv);
// 启动Activity
Intent intent2 = new Intent(context, mainActivity.class);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0,
intent2, 0);
rv.setOnClickPendingIntent(R.id.btn1, pendingIntent2);
appWidgetManager.updateAppWidget(appWidgetIds[0], rv);
// 启动Service1
Intent intent3 = new Intent("Service名");
PendingIntent pendingIntent3 = PendingIntent.getService(context, 0,
intent3, 0);
rv.setOnClickPendingIntent(R.id.btn1, pendingIntent3);
appWidgetManager.updateAppWidget(appWidgetIds[0], rv);
// 启动Service2
Intent intent4 = new Intent("Service名");
context.startService(intent4);
appWidgetManager.updateAppWidget(appWidgetIds[0], rv);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// 销毁
Log.e("wg", "onDeleted");
}
@Override
public void onReceive(Context context, Intent intent) {
// 每次的动作都会有反应
Log.e("wg", "onReceive");
super.onReceive(context, intent);
if(intent.getAction().equals("d.m.click")) {
}
}
@Override
public void onEnabled(Context context) {
// 创建第一个
super.onEnabled(context);
Log.e("wg", "onEnabled");
}
@Override
public void onDisabled(Context context) {
// 销毁最后一个
super.onDisabled(context);
Log.e("wg", "onDisabled");
}
}
wedget 的配置文件
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minWidth="146dip"
android:minHeight="146dip"
android:updatePeriodMillis="1800">
<!-- android:initialLayout="@layout/widget_layout"加载布局文件 -->
<!-- android:minWidth="146dip"宽度 -->
<!-- android:minHeight="146dip"高度 -->
<!-- 高度宽度的计算公式为 (块数*74)-2 -->
<!-- android:configure="d.m.mainActivity" -->
<!-- 是一个在Widget启动前先启动的Activity,方便用户对Widget的属性进行设置。 -->
<!-- android:updatePeriodMillis="360000"更新(1.5后需要设置为30分钟以上的时间)单位毫秒 -->
</appwidget-provider>
wedget在Manifest.xml中的配置
<receiver
android:label="@string/app_name"
android:icon="@drawable/icon1"
android:name=".wg">
<!-- android:label="@string/hello"显示在小部件窗口名称 -->
<!-- android:icon="@drawable/icon1"显示在小部件窗口图标 -->
<intent-filter>
<!-- 这个是必须有的 -->
<action
android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<!-- 对weiget的各种操作 -->
<action
android:name="d.m.click" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget" />
</receiver>
如果在wedget的配置文件中使用了android:configure="d.m.mainActivity"在wedget启动前调用Activity
package d.m;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
public class mainActivity extends Activity {
int mAppWidgetId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 如果返回RESULT_CANCELED,系统会认为配置失败,终止widget的创建过程。
setResult(RESULT_CANCELED);
// 获得widget的id
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras != null) {
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
// 返回的是无效的id
if(mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
// return OK
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
}
Activity在Manifest.xml中的配置
<activity
android:name=".mainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
<!-- 如果在widget的属性中使用了android:configure,那么就必须添加intent-filter 值为AppWidgetManager.ACTION_APPWIDGET_DELETED -->
</intent-filter>
</activity>