0

(这是我有史以来的第一个 SE 问题,但我非常依赖这个网站,因为它是一个很棒的社区,因为我在学习 Java 和 Android 时遇到了麻烦!)

我正在创建一个 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

0

I cannot see anywhere in your code where you are using the PendingIntent that you have created, you can use the alarmmanager to schedule a message, presumably immediately, using code like this:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);

Broadcast messages are quite complex though, so you may want to weigh up if you want to use them, you could consider using a regular intent and context.sendBroadcast instead

于 2013-09-04T19:50:26.587 回答