Java Android IntentReceiver从不从WidgetProvider接收自定义意图
(这是我有史以来的第一个SE问题,但我非常依赖这个网站,因为它是一个非常棒的社区,因为我一直在学习Java和Android!) 我正在创建一个Android小部件,它(目前)只有一个按钮,可以充当双面骰子。最终,这个小部件将支持所有主要的RPG骰子大小(d6、d8、d20等),但现在我只想让intent/receiver系统正常工作 目前,当我点击我的小部件中的d2按钮时,什么也没有发生。据我所知,通过调试,我的自定义意图被激发,但我的intentreceive中的onReceive从未捕捉到它。如果您能提供任何帮助,我们将不胜感激 这是我的WidgetProvider:Java Android IntentReceiver从不从WidgetProvider接收自定义意图,java,android,android-intent,android-widget,Java,Android,Android Intent,Android Widget,(这是我有史以来的第一个SE问题,但我非常依赖这个网站,因为它是一个非常棒的社区,因为我一直在学习Java和Android!) 我正在创建一个Android小部件,它(目前)只有一个按钮,可以充当双面骰子。最终,这个小部件将支持所有主要的RPG骰子大小(d6、d8、d20等),但现在我只想让intent/receiver系统正常工作 目前,当我点击我的小部件中的d2按钮时,什么也没有发生。据我所知,通过调试,我的自定义意图被激发,但我的intentreceive中的onReceive从未捕捉到它
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提供程序:
<?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>
非常感谢所有提供的帮助,非常感谢这个社区通过回答这么多其他问题为我提供的帮助 我看不到您的代码中有任何地方正在使用您创建的PendingEvent,您可以使用alarmmanager来安排消息,可能是立即,使用如下代码:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);
广播消息非常复杂,所以如果你想使用它们,你可能想称重,你可以考虑使用常规的意图和<代码>上下文。我没有考虑过,所以我要试一试。