たぼさんの部屋

いちょぼとのんびり

T313 AppWidget OnClickイベントのみ

f:id:donsuka_kk:20121216194311p:plain

MWidget.java

package com.efolab.t313;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MWidget extends AppWidgetProvider {
	RemoteViews rv;
	private final String 
		FILTER_MAP = "com.efolab.t313.OPEN_MAP",
		FILTER_SETTING = "com.efolab.t313.OPEN_SETTING";
	@Override
	public void onReceive(Context context, Intent intent) {
	    // TODO Auto-generated method stub
		super.onReceive(context, intent);	//super 必須
		if(intent.getAction().equals(FILTER_MAP)){
			Toast.makeText(context, "onReceive"+intent.getAction(), Toast.LENGTH_SHORT).show();
		}
		if(intent.getAction().equals(FILTER_SETTING)){
			Toast.makeText(context, "onReceive"+intent.getAction(), Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();
		
		rv = new RemoteViews(context.getPackageName(), R.layout.widget);
		Intent openMap = new Intent(FILTER_MAP);
		Intent openSetting = new Intent(FILTER_SETTING);
		//ペンディングインテントインスタンスを生成
		PendingIntent pendingMap = PendingIntent.getBroadcast(context, 0, openMap, 0);
		PendingIntent pendingSetting = PendingIntent.getBroadcast(context, 0, openSetting, 0);
		//リモートビューのボタンにひもつけ
		rv.setOnClickPendingIntent(R.id.button1, pendingMap);
		rv.setOnClickPendingIntent(R.id.button2, pendingSetting);
		//ウィジェットを更新
		appWidgetManager.updateAppWidget(appWidgetIds, rv);		//必須
	}

}

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.efolab.t313"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="MWidget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
                <action android:name="com.efolab.t313.OPEN_MAP" />
                <action android:name="com.efolab.t313.OPEN_SETTING"/>
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"/>
        </receiver>
    </application>

</manifest>

res/xml/widget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget"
    android:minHeight="72dp"
    android:minWidth="294dp"
    android:updatePeriodMillis="0" >

</appwidget-provider>

res/layout/widget.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="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>