最終更新:
moonlight_aska 2013年10月14日(月) 22:29:12履歴
ホーム画面に配置したウィジェットから他のアプリを起動するには, AppWidgetから他のアプリを起動すると同様にペンディングインテント(PendingIntent)クラスを利用する.

↓

- TestService.java
- Serviceクラスを継承した実装クラスを作成する.
- Service#onStartCommandメソッドに起動時の処理を実装する.
- Service#onDestoryメソッドに終了時の処理を実装する.
package com.moonlight_aska.android.service05;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class TestService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
// 開始時にコール
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this, "サービス開始", Toast.LENGTH_LONG).show();
stopSelf();// 自ら停止
return super.onStartCommand(intent, flags, startId);
}
// 終了時にコール
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Toast.makeText(this, "サービス終了", Toast.LENGTH_LONG).show();
super.onDestroy();
}
}
- Widget01.java
- Intentを使って起動するサービスを設定する.
- PendingIntent#getServiceメソッドで, PendingIntentのインスタンスを取得する.
- RemoteViewsのインスタンスを取得する.
- RemoteViews#setOnClickPendingIntentメソッドで, ButtonクリックにPendingIntentを紐付ける.
- appWidgetManager#updateAppWidgetメソッドで, ウィジェットを更新する.
package com.moonlight_aska.android.service05;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class Widget01 extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
// サービスの指定
Intent intent = new Intent(context, TestService.class);
// PendingIntentの取得
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
// インテントによりサービス起動
remoteViews.setOnClickPendingIntent(R.id.service_id, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
- res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="@+id/service_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_label" />
</LinearLayout>
- res/xml/widget.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:updatePeriodMillis="0"
android:minHeight="72dip"
android:minWidth="146dip"
android:initialLayout="@layout/main"
/>
- AndroidManifest.xml
- receiverタグのandroid:name属性に, AppWidgetProviderを継承したクラスを指定する.
- intent-filterタグ内のactionタグに, 更新イベントを受け取るために"android.appwidget.action.APPWIDGET_UPDATE"を設定する.
- meta-detaタグのandroid:resorce属性に, 設定ファイルを指定する.
- serviceタグのandroid:name属性に, Serviceを継承したクラスを指定する.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.moonlight_aska.android.service05"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name=".Widget01"
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/widget" />
</receiver>
<!-- サービスの登録 -->
<service android:name=".TestService" />
</application>
</manifest>

↓

コメントをかく