0

(これは私の初めての SE の質問ですが、Java と Android を学習するのに苦労したので、このサイトの素晴らしいコミュニティに大きく依存しています!)

(当面は) 両面サイコロとして機能するボタンが 1 つしかない Android ウィジェットを作成しています。最終的に、このウィジェットはすべての主要な RPG ダイス サイズ (d6、d8、d20 など) をサポートする予定ですが、今のところ、インテント/レシーバー システムを機能させようとしています。

現在、ウィジェットで d2 ボタンを押しても何も起こりません。カスタムインテントをデバッグすることでわかる限り、起動されますが、IntentReceiver の onReceive はそれをキャッチしません。ご提供いただけるご支援をいただければ幸いです。

ここに私の WidgetProvider があります:

    public class MyWidgetProvider extends AppWidgetProvider {

    @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager,
          int[] appWidgetIds) { 

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        remoteViews.setOnClickPendingIntent(R.id.btnReset, buildButtonPendingIntent(context));
        Log.e(null, "Setup remote views & Onclick");
        Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show();
        pushWidgetUpdate(context, remoteViews);
    }

    public static PendingIntent buildButtonPendingIntent(Context context) {
        Intent intent = new Intent();
        intent.setAction("ca.sulli.rpgdicewidget.intent.action.D2");
        Log.e(null, "Setting new ButtonPendingIntent");
        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
        Log.e(null, "Updating widget!");
        ComponentName myWidget = new ComponentName(context, MyWidgetProvider.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(myWidget, remoteViews); 
    }  
}

そして、ここに私の受信機があります:

public class MyWidgetIntentReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(null,"Checking intent by receiver");
        if(intent.getAction().equals("ca.sulli.rpgdicewidget.intent.action.D2")){
            Log.e(null,"Correct intent received!");
            updateWidgetPictureAndButtonListener(context);
        }
    }

    private void updateWidgetPictureAndButtonListener(Context context) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        Log.e(null,"Updating text since the intent was received!");
        remoteViews.setTextViewText(R.id.txtResult, "Intent Received! ");

        remoteViews.setOnClickPendingIntent(R.id.btnD2, MyWidgetProvider.buildButtonPendingIntent(context));

        MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
    }
}

そして完成のために、私のマニフェスト:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ca.sulli.rpgdicewidget"
    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="MyWidgetProvider" >
            <intent-filter >
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="ca.sulli.rpgdicewidget.intent.action.D2" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>

    </application>

</manifest>

そして appwidget-provider:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="200dp" android:minHeight="70dp" android:initialLayout="@layout/main" android:updatePeriodMillis="30000">
</appwidget-provider>

提供されたすべてのヘルプに感謝します。また、このコミュニティが他の多くの質問に答えて私に提供してくれたヘルプに感謝します!

4

1 に答える 1