我在带有android n的手机中测试我的应用程序。该手机中的小部件没有问题。但是,当我尝试使用其他手机时,当我点击ImageView刷新并转换为观看小部件然后显示错误消息时,它会做出奇怪的事情。 logcat中没有任何内容。
公共类RadioWidget扩展了AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.radio_widget);
//Go to radio/track info fragment
Intent radiointent = new Intent(context, MainActivity.class);
radiointent.setAction("**********.play");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, radiointent, 0);
views.setOnClickPendingIntent(R.id.wlastp, pendingIntent);
radiointent.setAction("*********.track.info.last");
pendingIntent = PendingIntent.getActivity(context , 0, radiointent, 0);
views.setOnClickPendingIntent(R.id.artwork, pendingIntent);
//Play/Stop radio
Intent controls = new Intent(context, NotificationControls.class);
controls.setAction("********.widget.play");
PendingIntent play = PendingIntent.getBroadcast(context.getApplicationContext(),1,controls,0);
views.setOnClickPendingIntent(R.id.wplay, play);
controls.setAction("********.widget.stop");
PendingIntent stop = PendingIntent.getBroadcast(context.getApplicationContext(),2,controls,0);
views.setOnClickPendingIntent(R.id.wstop, stop);
views.setTextViewText(R.id.trackname, "now playing"));
//Load image
Glide.with(context.getApplicationContext())
.load(/* artwork url*/)
.asBitmap()
.error(R.mipmap.album_placeholder)
.into(new AppWidgetTarget(context.getApplicationContext(),views,R.id.artwork,appWidgetId));
else views.setImageViewResource(R.id.artwork, R.mipmap.album_placeholder);
int button_state = R.drawable.ic_play_arrow_black_24dp;
if(/* checking if radio is playing */) button_state = R.drawable.ic_pause_black_24dp;
views.setImageViewResource(R.id.wplay, button_state);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/BG"
android:padding="@dimen/widget_margin">
<ImageView
android:layout_width="80dp"
android:layout_height="match_parent"
android:src="@mipmap/album_placeholder"
android:contentDescription="@string/cd_artwork"
android:id="@+id/artwork"
android:layout_alignParentLeft="true"
android:scaleType="centerCrop"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/media_play_now"
android:id="@+id/trackname"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/artwork"
android:layout_toEndOf="@+id/artwork"
android:textSize="16sp"
android:minLines="2"
android:layout_marginTop="4dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/trackname"
android:layout_alignLeft="@+id/trackname"
android:layout_alignStart="@+id/trackname"
>
<ImageView
android:id="@+id/wplay"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/ic_play_arrow_black_24dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
/>
<ImageView
android:id="@+id/wstop"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/ic_stop_black_24dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
/>
<ImageView
android:id="@+id/wlastp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/ic_last_played_black_24dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
/>
</LinearLayout>
</RelativeLayout>
清单
<receiver android:name=".RadioWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/radio_widget_info" />
</receiver>
这是我的方法从服务
更新小部件public void updateRadioWidget(Context context) {
AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
ComponentName widgetComponent = new ComponentName(context, RadioWidget.class);
int[] widgetIds = widgetManager.getAppWidgetIds(widgetComponent);
Intent update = new Intent();
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
context.sendBroadcast(update);
}
我尝试了另一篇文章,但没有运气。它正在使用android N而不是其他人。
答案 0 :(得分:0)
我编辑了我的更新方法,它现在似乎正在运作
public void updateRadioWidget() {
int[] ids = AppWidgetManager.getInstance(context.getApplicationContext()).getAppWidgetIds(new ComponentName(context.getApplicationContext(), RadioWidget.class));
RadioWidget radioWidget = new RadioWidget();
radioWidget.onUpdate(context.getApplicationContext(), AppWidgetManager.getInstance(context.getApplicationContext()),ids);
}