gpt4 book ai didi

android - 当应用程序被杀死时,应用程序小部件点击不起作用

转载 作者:行者123 更新时间:2023-12-05 07:53:16 31 4
gpt4 key购买 nike

我有一个小部件,当应用程序在后台运行时它运行良好。但是当我用应用程序管理器终止应用程序时,小部件不会触发点击事件。然后我重新打开应用程序,小部件仍然没有触发点击事件。有什么想法吗?

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);
}
}

list

<!-- 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>

最佳答案

我目前也在寻找解决方案,希望这会有所帮助:

如果你查看 soundcloud 的小部件,你会注意到当应用程序被终止时,

  1. 小部件没有布局来指示“关闭”状态
  2. 你点击它,它首先启动应用程序,更新小部件的布局和按钮
  3. 您再次点击以播放您的音乐。

所以,我在想,您应该运行一个服务,该服务会在用户单击小部件时首先启动您的应用。

关于android - 当应用程序被杀死时,应用程序小部件点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32826279/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com