我有一个小部件,当应用程序在后台运行时它能很好地工作。但是当我使用应用程序管理器杀死应用程序时,小部件无法触发点击事件。然后我重新打开应用程序,小部件仍然无法触发点击事件。有任何想法吗?
public class Widget extends AppWidgetProvider {
private ArrayList<Widget> widgets;
private static String listing1 = "";
private static String listing2 = "";
private static String listing3 = "";
private static String listing4 = "";
private static String listing5 = "";
RemoteViews views;
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds));
// Perform this loop procedure for each App Widget that belongs to this
HashMap<String, String> params = new HashMap<>();
params.put("islem", "recommendation");
ServiceConnector.secretcvAPI.getWidgetItems(params, new SuccessCallback<GetWidgetResponse>() {
@Override
public void success(GetWidgetResponse getWidgetResponse, Response response) {
widgets = getWidgetResponse.widgets;
for (int appWidgetId : appWidgetIds) {
listing1 = widgets.get(0).ilan_id;
listing2 = widgets.get(1).ilan_id;
listing3 = widgets.get(2).ilan_id;
listing4 = widgets.get(3).ilan_id;
listing5 = widgets.get(4).ilan_id;
// Get the layout for the App Widget and attach an on-click listener
// to the button
views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.listing1, getPendingSelfIntent(context, widgets.get(0).ilan_id, appWidgetId));
views.setOnClickPendingIntent(R.id.listing2, getPendingSelfIntent(context, widgets.get(1).ilan_id, appWidgetId));
views.setOnClickPendingIntent(R.id.listing3, getPendingSelfIntent(context, widgets.get(2).ilan_id, appWidgetId));
views.setOnClickPendingIntent(R.id.listing4, getPendingSelfIntent(context, widgets.get(3).ilan_id, appWidgetId));
views.setOnClickPendingIntent(R.id.listing5, getPendingSelfIntent(context, widgets.get(4).ilan_id, appWidgetId));
views.setTextViewText(R.id.listingNameTV, widgets.get(0).firma.isim);
views.setTextViewText(R.id.listingNameTV2, widgets.get(1).firma.isim);
views.setTextViewText(R.id.listingNameTV3, widgets.get(2).firma.isim);
views.setTextViewText(R.id.listingNameTV4, widgets.get(3).firma.isim);
views.setTextViewText(R.id.listingNameTV5, widgets.get(4).firma.isim);
views.setTextViewText(R.id.positionNameTV, widgets.get(0).ilan_baslik);
views.setTextViewText(R.id.positionNameTV2, widgets.get(1).ilan_baslik);
views.setTextViewText(R.id.positionNameTV3, widgets.get(2).ilan_baslik);
views.setTextViewText(R.id.positionNameTV4, widgets.get(3).ilan_baslik);
views.setTextViewText(R.id.positionNameTV5, widgets.get(4).ilan_baslik);
Picasso.with(context)
.load("http:" + widgets.get(0).firma.logo)
.into(views, R.id.widgetIV, new int[]{appWidgetId});
Picasso.with(context)
.load("http:" + widgets.get(1).firma.logo)
.into(views, R.id.widgetIV2, new int[]{appWidgetId});
Picasso.with(context)
.load("http:" + widgets.get(2).firma.logo)
.into(views, R.id.widgetIV3, new int[]{appWidgetId});
Picasso.with(context)
.load("http:" + widgets.get(3).firma.logo)
.into(views, R.id.widgetIV4, new int[]{appWidgetId});
Picasso.with(context)
.load("http:" + widgets.get(4).firma.logo)
.into(views, R.id.widgetIV5, new int[]{appWidgetId});
// Tell the AppWidgetManager to perform an update on the current app
// widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
});
}
private PendingIntent getPendingSelfIntent(Context context, String action, int appWidgetId) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (listing1.equals(intent.getAction())){
startApplciationWithIntent(context, listing1);
}
else if (listing2.equals(intent.getAction())){
startApplciationWithIntent(context, listing2);
}
else if (listing3.equals(intent.getAction())){
startApplciationWithIntent(context, listing3);
}
else if (listing4.equals(intent.getAction())){
startApplciationWithIntent(context, listing4);
}
else if (listing5.equals(intent.getAction())){
startApplciationWithIntent(context, listing5);
}
}
private void startApplciationWithIntent(Context context, String ilanid){
Intent i = new Intent(context, LoggedOutActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle bundle = new Bundle();
bundle.putString("DeepLink", ilanid);
i.putExtras(bundle);
context.startActivity(i);
}
}
清单
<!-- Widget-->
<receiver android:name=".Widget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>