Android IntentReceiver永远不会从WidgetProvider接收自定义意图

时间:2013-09-04 19:21:06

标签: java android android-intent android-widget

(这是我有史以来的第一个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>

非常感谢您提供的任何帮助,非常感谢社区通过回答其他许多问题为我提供的帮助!

1 个答案:

答案 0 :(得分:0)

我无法在你的代码中看到你使用你创建的PendingIntent的任何地方,你可以使用alarmmanager来安排一条消息,大概是立即使用这样的代码:

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

广播信息非常复杂,所以如果你想使用它们,你可能想要权衡一下,你可以考虑使用常规意图而不是context.sendBroadcast