출처 : http://lilysoul.tistory.com/91
<!-- AndroidManifest.xml -->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.memowidget"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="MemoWidget">
<!-- 프로바이더 -->
<receiver android:name="MemoWidget" android:enabled="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.action.BUTTON_CLICK" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/memowidget" />
</receiver>
</application>
</manifest>
<!-- memowidget.xml -->
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="210px"
android:minHeight="110px"
android:updatePeriodMillis="5000"
android:initialLayout="@layout/main" >
</appwidget-provider>
<!-- main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="210px"
android:layout_height="110px"
android:background="@drawable/ss0001"
>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BUTTON"
/>
</LinearLayout>
<!-- MemoWidget.java -->
package com.memowidget;
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;
public class MemoWidget extends AppWidgetProvider{
public Context mContext;
private String ACTION_BTN = "android.action.BUTTON_CLICK";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
//버튼에 이벤트 주기
RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.main);
PendingIntent btn_click = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_BTN), PendingIntent.FLAG_UPDATE_CURRENT);
rViews.setOnClickPendingIntent(R.id.btn, btn_click);
//위젯 업데이트
final int N = appWidgetIds.length;
for(int i=0;i<N;i++){
int widgetID = appWidgetIds[i];
appWidgetManager.updateAppWidget(widgetID, rViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (action.equals(ACTION_BTN)) {
Log.e("XXXX","BUTTON_CLICK");
}
super.onReceive(context, intent);
}
}