1. AppWidgetProviderInfo object๋ฅผ xml๋ก ์์ฑํ๋ค.
ย – ๋ ์ด์์, ์ ๋ฐ์ดํธ ์ฃผ๊ธฐ, Provider, Configuration Activity
ย – res/xml/mywidget_provider.xml
<appwidget-provider xmlns:android=”http://schemas.android.com/apk/res/android”
android:minWidth=”146dp”
android:minHeight=”144dp”
android:initialLayout=”@layout/main”
android:updatePeriodMillis=”0” /> <!– ์ ๋ฐ์ดํธ ์ฃผ๊ธฐ(Service๋ก ์ ์ดํ๊ธฐ์ํด 0) –>
2. AppWidgetProvider๋ฅผ ์์๋ฐ๋ ํด๋์ค๋ฅผ ๋ง๋ ๋ค.
ย – Update, Enable, Disable, Delete ๋ฑ ์ฒ๋ฆฌ๋ฅผ ํ๋ค.
ย – AppWidgetProvider๊ฐ Broadcast๋ฅผ ์์๋ฐ๊ธฐ ๋๋ฌธ์ Broadcast ๋ฉ์์ง๋ฅผ ์์ ํ๋ค.
ย – Widget์ ๋ธ๋ก๋์บ์คํธ๋ฅผ ์ง์ ์ฒ๋ฆฌํ ๋ ค๋ฉด onReceive()๋ฅผ ๋ฑ๋กํ๋ค. (์๋๋ก์ด๋1.6 ์ด์๋ถํฐ 30๋ถ ์ด์ ๋จ์๋ก Update ๋๋ค. ํ
์คํธ ๋ฑ์ ๋ชฉ์ ์ผ๋ก ์งง์ ์๊ฐ ๋จ์๋ก ๊ฐฑ์ ์ ํ ๊ฒฝ์ฐ Service๋ฅผ ์์๋ฐ๋ ๋ด๋ถ ์
๋ฐ์ดํธ ํด๋์ค๋ฅผ ๋ง๋ค์ด ์ฒ๋ฆฌํ๋ค.)
public class MyWidget extends AppWidgetProvider {
…
public void onReceive(Context context, Intent intent) {
…
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
Int[] appWidgetIds) {
…
}
}
3. AppWidgetProviderInfo์์ ์ง์ ํด์ค Layout์ ๋ง๋ ๋ค.
ย – FrameLayout, LinearLayout, RelativeLayout๊ณผ AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, TextView ๋ง ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
<!–[if !supportEmptyParas]–>ย <!–[endif]–>
4. AndroidManifest์ Widget ์ ์
ย – receiver๋ฅผ ๋ฑ๋กํ๊ณ intent filter์ android.appWidget.action.APPWIDGET_UPDATE๋ฅผ ์ ์ํ๋ค.
ย – ์ ์๋ AppWidgetProviderInfo๋ฅผ mata-data์ ๊ธฐ์ ํด์ค๋ค.
<receiver android:name=”.MyWidget” android:label=”@string/app_name”>
<intent-filter>
<action android:name=”android.appwidget.action.APPWIDGET_UPDATE” />
</intent-filter>
<meta-data android:name=”android.appwidget.provider”
ย ย ย ย ย android:resource=”@xml/mywidget_provider” />
</receiver>
5. Update์ RemoteViews์ AppWidgetManager๋ฅผ ์ด์ฉํ์ฌ ๋ทฐ ์ ๋ฐ์ดํธ๋ฅผ ํ๋ค.
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
remoteViews.setTextViewText(R.id.textview01, ”update”);
<!–[if !supportEmptyParas]–>ย <!–[endif]–>
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
/* or onUpdate ๋ฐ์ ๋ฉ์๋๋ฅผ ๋ง๋ค์ด์ ์ฌ์ฉํ ๊ฒฝ์ฐ
ComponentName thisWidget = new ComponentName(this, MyWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, remoteViews);
*/
6. 30๋ถ ์ดํ์ ๊ฐ๊ฒฉ์ผ๋ก ํ๋ฉด์ ๊ฐฑ์ ํ๊ธฐ ์ํด Service ํด๋์ค๋ฅผ ์์๋ฐ๋ ํด๋์ค ์์ฑ์ onStart()์์ ์ฒ๋ฆฌ
ย
public static class UpdateService extends Service {
…
public void onStart(Intent intent, int startId) {
…// ์์ ฏ ์ ๋ฐ์ดํธ
Context context = getBaseContext();
// ํ์ฌ ์๊ฐ + ์ ๋ฐ์ดํธ ๊ฐ๊ฒฉ์ผ๋ก ๋ค์ ์ ๋ฐ์ดํธ ์๊ฐ ์ง์
long nextAlarm = System.currentTimeMillis() + updateInterval
// onUpdate()๋ก๋ถํฐ ์ ๋ฌ๋ฐ์ WidgetId๋ฅผ ๋ค์ ์คํํ boradcastํ intent์
// ์ฎ๊ฒจ๋ด๊ณ Update action์ ์ธํ ํธ ์์ฑ
int appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
Intent alarmIntent = new Intent(context, NaverRank.class);
alarmIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
alarmIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// ์ ์์ฑ์ ๊ฐ์ง๋ ๋ธ๋ก๋์บ์คํธ ๋ฉ์์ง๋ฅผ ๋ณด๋ด๋ pendingIntent ๊ฐ์ฒด ์์ฑ
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
<!–[if !supportEmptyParas]–>ย <!–[endif]–>
// ์์ ๋ง๋ค์ด์ง pendingintent ๊ฐ์ฒด์ ๋ค์ ์ ๋ฐ์ดํธ ์๊ฐ์ผ๋ก ์๋๋งค๋์ ์คํ
AlarmManager alarmManager = (AlarmManager) context.getSystemService(android.app.Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, nextAlarm, pendingIntent);
}
}
์ข์ ์ ๋ณด ๊ณ ๋ง์ต๋๋ค~^^
๋ณ๋ง์์์ ^_^