반응형
안녕하세요 돼지왕 왕돼지입니다.
AppWidget 을 빠르게 만들 수 있게, 그리고 쉽게 학습할 수 있도록 Sample Code 올립니다.
Test Widget 은 View 로 Button 하나를 가지는 위젯입니다.
Test Widget 은 View 로 Button 하나를 가지는 위젯입니다.
@manifest
<application android:icon="@drawable/icon" android:label="@string/app_name">
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".ButtonWidget" android:label="Button_widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/button_widget_provider" />
</receiver>
<activity android:name=".ClickOneActivity"/>
</application>
</manifest>
@AppWidgetProvider
public class ButtonWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
Intent active = new Intent(context, ButtonWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("msg", "Message for Button 1");
Intent configIntent = new Intent(context, ClickOneActivity.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.button_one, actionPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.button_two, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt( AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
}
else {
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
String msg = "null";
try {
msg = intent.getStringExtra("msg");
} catch (NullPointerException e) {
Log.e("Error", "msg = null");
}
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noty = new Notification(R.drawable.icon, "Button 1 clicked", System.currentTimeMillis());
noty.setLatestEventInfo(context, "Notice", msg, contentIntent);
notificationManager.notify(1, noty);
}
super.onReceive(context, intent);
}
}
}
@Activity
public class ClickOneActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configure);
Toast.makeText(getApplicationContext(), "We are in ClickOneActivity", Toast.LENGTH_SHORT).show();
}
}
로그인 없이 추천 가능합니다. 손가락 꾸욱~
반응형
'프로그래밍 놀이터 > 안드로이드, Java' 카테고리의 다른 글
[Android/안드로이드] Timer 를 이용한 App Widget Update Example (15) | 2012.02.16 |
---|---|
[Android/안드로이드] Service 를 이용한 App Widget Update (0) | 2012.02.16 |
[Android/안드로이드] Service 생명주기. (8) | 2012.02.15 |
[Android/안드로이드] Traceview 에 대해 알아보자. ( Profilinig ) (0) | 2012.02.15 |
[Android/안드로이드] Emulator의 /system/app 폴더에 apk 올리기. (0) | 2012.02.15 |
댓글