@ZeroGeek
2015-10-23T09:10:59.000000Z
字数 1984
阅读 893
每周主题
Android弱化了线程和进程等概念,主要表现为组件。这个桌面小部件与App是分离的,可以通过服务或广播来通信。
<intent-filter><action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/></intent-filter>
public class MyAppWidget extends AppWidgetProvider {public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {final int N = appWidgetIds.length;// Perform this loop procedure for each App Widget that belongs to this providerfor (int i=0; i<N; i++) {int appWidgetId = appWidgetIds[i];// 创建Intent来启动AcitivityIntent intent = new Intent(context, MainActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);// Get the layout for the App Widget and attach an on-click listener// to the button// 初始化部件布局,设置监听事件RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);views.setOnClickPendingIntent(R.id.button, pendingIntent);// Tell the AppWidgetManager to perform an update on the current app widgetappWidgetManager.updateAppWidget(appWidgetId, views);}}}
我只加了个按钮
<?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"><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="match_parent"android:text="OK" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"android:minWidth="200dip"android:minHeight="50dip"android:initialLayout="@layout/appwidget_provider_layout" ></appwidget-provider>
<receiver android:name="MyAppWidget" android:label="测试插件" ><intent-filter><action android:name="android.appwidget.action.APPWIDGET_UPDATE" /></intent-filter><meta-data android:name="android.appwidget.provider"android:resource="@xml/my_provider" /></receiver>
http://androiddoc.qiniudn.com/guide/topics/appwidgets/index.html
