Skip to main content

[Android] Widget

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);

}

}

2 thoughts to “[Android] Widget”

๋Œ“๊ธ€ ๋‚จ๊ธฐ๊ธฐ