gpt4 book ai didi

android - ListView 小部件未在锁定屏幕上更新

转载 作者:行者123 更新时间:2023-11-30 02:20:30 25 4
gpt4 key购买 nike

package com.Widget;

import java.util.Random;
import com.Launcher.LauncherActivity;
import com.Widget.wifiwidget.R;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;

/**
* A very basic {@link AppWidgetProvider} implementation that delegates the
* actual processing to the {@link WidgetService}.
*/

public class WidgetProvider extends AppWidgetProvider {

public static String ACTION_WIDGET_EXPEND = "ActionReceiverExpend";
public static String ACTION_WIDGET_EMERGENCY = "ActionReceiverEmergency";
public static String ACTION_WIDGET_DIAL = "ActionReceiverDial";
public static String EXTRA_WORD = "com.Widget.WORD";

int id;

@SuppressWarnings("deprecation")
@Override
public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
/*
* AppWidgetProvider extends BroadcastReceiver, so we must not spend
* lots of processing time in this class. Actual processing is done in a
* Service so that this method can return as quickly as possible.
*/
Log.d("onUpdate", "onUpdate");
for (int i = 0; i < appWidgetIds.length; i++) {

id = new Random().nextInt(10000000);

Intent svcIntent = new Intent(ctxt, ListItemsService.class);
svcIntent.setAction(ACTION_WIDGET_EXPEND);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetIds[i] + id);
svcIntent.setData(Uri.parse(svcIntent
.toUri(Intent.URI_INTENT_SCHEME)));

RemoteViews widget = new RemoteViews(ctxt.getPackageName(),
R.layout.widget);

widget.setRemoteAdapter(appWidgetIds[i] + id, R.id.userData,
svcIntent);

Intent clickIntent = new Intent(ctxt, LauncherActivity.class);
PendingIntent clickPI = PendingIntent.getActivity(ctxt, 0,
clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

widget.setPendingIntentTemplate(R.id.userData, clickPI);

appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
}

ctxt.startService(openIntentForlistService(ctxt));
ctxt.startService(getIntentForService(ctxt));

super.onUpdate(ctxt, appWidgetManager, appWidgetIds);

}

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
Log.d("onReceive", "onReceive");
context.startService(openIntentForlistService(context));
}

/**
* Stops the background service when the widget is removed.
*/
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Log.d("onDeleted", "onDeleted");
context.stopService(getIntentForService(context));
context.stopService(openIntentForlistService(context));

super.onDeleted(context, appWidgetIds);
}

/**
* Helper method to create the correct {@link Intent} to use when working
* with the {@link WidgetService}.
*
* @param context
* Context to use for the Intent
* @return Intent that can be used to interact with the
* {@link WidgetService}
*/
private Intent getIntentForService(Context context) {
Intent widgetService = new Intent(context.getApplicationContext(),
WidgetService.class);
return widgetService;
}

private Intent openIntentForlistService(Context context) {
Intent dataListService = new Intent(context.getApplicationContext(),
ListItemsService.class);
return dataListService;
}

}

public class ListItemsService extends RemoteViewsService {

@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
Log.d("Start ListItemsService", "Start ListItemsService");
return (new WordListViewsFactory(this.getApplicationContext(), intent));
}
}
}

My listView of widget is not updating on lock screen, it seems like RemoteViewsService is not calling from lock screen.

请看我的WidgetProvider类代码

我认为这些代码足以解释问题

最佳答案

我遇到了同样的问题并得到了解决方案。在 WidgetProvider 类的 onReceive() 方法中,只需放入以下代码即可在锁定屏幕上更新小部件:

try {
// Code for update on lock screen
int ids[] = AppWidgetManager.getInstance(context).getAppWidgetIds(
new ComponentName(context, WidgetProvider.class));
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
onUpdate(context, appWidgetManager, ids);
appWidgetManager.notifyAppWidgetViewDataChanged(ids,
R.layout.widget);
} catch (Exception e) {
e.printStackTrace();
}

关于android - ListView 小部件未在锁定屏幕上更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28610601/

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