簡単なwidget(marsから)を作成する

3754 ワード

1、AppWidgetProviderを作成する 
 
 
public class ExampleAppWidgetProvider extends AppWidgetProvider {
	//         ,       Action
	private static final String UPDATE_ACTION = "mars.appwidget03.UPDATE_APP_WIDGET";
	//         ,    app
	private static final String KILL_APP = "com.rui.app.KILL_APP";

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onDisabled(Context context) {
		// TODO Auto-generated method stub
		super.onDisabled(context);
	}

	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		super.onEnabled(context);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		String action = intent.getAction();
		Log.i("TAG", action);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Log.i("TAG", "===================================");
		//    Intent  
		Intent intent = new Intent();
		// Intent    Action
		intent.setAction(KILL_APP);
		//  getBroadcast  ,    PendingIntent  ,       ,       
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
				intent, 0);
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.example_appwidget);
		remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
	}

 
2、layoutの下にコンテンツを表示するためのレイアウトを作成する example_appwidget.xml
 
     
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
	android:id="@+id/widgetButtonId"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="     "
	/>
</LinearLayout>

 
3、resの下のxmlの下に新しいxmlを作成する (example_appwidget_info.xml)ファイルを使用して、このwidgetの情報を指定します.
 
      xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="72dp" android:updatePeriodMillis="5000" android:initialLayout="@layout/example_appwidget" > </appwidget-provider>  
4、最後にAndroidManifest.xmlでこのwidgetを構成する必要がある情報:
 
      
			<intent-filter>
				<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
			</intent-filter>
			<intent-filter>
				<action android:name="mars.appwidget03.UPDATE_APP_WIDGET"/>
			</intent-filter>
			<intent-filter>
				<action android:name="com.rui.app.KILL_APP"/>
			</intent-filter>
			<meta-data android:name="android.appwidget.provider"
				android:resource="@xml/example_appwidget_info" />
		</receiver>