问题描述
我创建了一个非常好用的小部件,然后由于Google Play开发者控制台的要求,我将targetSDK从23更改为26。
I created a widget, which was working quite fine, and then I changed targetSDK from 23 to 26, due to the requirement from Google Play Developer Console.
之后切换到Android SDK 26,我的app小部件不再更新screen_on / User_present事件。我可以看到,由于(后台执行限制),后续运行任务在Android 26中有很多变化。
After switching to Android SDK 26, my app widget is no more updating on the screen_on/User_present event. I can see that there are a bunch of changes in Android 26 for background running task due to (Background Execution Limits).
以下是我的问题?
Q1-如何更新我的应用每个screen_on事件上的小部件,以便用户在小部件上看到正确的状态?
Q1- How can I update my app widget on every screen_on event, so that the user will see the right status on widget?
Q2-如何每隔1小时定期更新我的应用小部件?
Q2- How can I update my app widget periodically after every 1 hour?
以下代码我正在使用。
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
String token = getToken();
getUUID();
getGatewayActivationStatus();
getEnvironment();
if (token != null) {
remoteViews.setViewVisibility(R.id.layoutBtn, View.VISIBLE);
AppWidgetIntentReceiver appWI = new AppWidgetIntentReceiver();
appWI.getMode(context);
} else {
remoteViews.setTextViewText(R.id.txt_mode, "Please sign into your App to see status.");
remoteViews.setViewVisibility(R.id.layoutBtn, View.INVISIBLE);
}
remoteViews.setOnClickPendingIntent(R.id.btn_refresh, buildButtonPendingIntent(context, _refresh));
remoteViews.setOnClickPendingIntent(R.id.txt_mode, buildButtonPendingIntent(context, _openApp));
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);//add this line
AppWidgetIntentReceiver appWI = new AppWidgetIntentReceiver();
if (_standby.equals(intent.getAction()) || _home.equals(intent.getAction()) || _away.equals(intent.getAction()) || _refresh.equals(intent.getAction()) || _openApp.equals(intent.getAction())) {
appWI.handleClickEvent(context, intent, intent.getAction());
} else if (intent.getAction().equals("android.intent.action.USER_PRESENT") || intent.getAction().equals("android.appwidget.action.APPWIDGET_ENABLED") || intent.getAction().equals("android.intent.action.MY_PACKAGE_REPLACED") || intent.getAction().equals("android.appwidget.action.APPWIDGET_UPDATE")) {
getToken();
getUUID();
getGatewayActivationStatus();
getEnvironment();
appWI.handleClickEvent(context, intent, _refresh);
}
};
static PendingIntent buildButtonPendingIntent(Context context, String event) {
if (!event.equals("OPEN_APP")) {
Intent intent = new Intent(context, AppWidgetProvider.class);
intent.setAction(event);
return PendingIntent.getBroadcast(context, 0, intent, 0);
} else {
Intent intent2 = new Intent(context, MainActivity.class);
return PendingIntent.getActivity(context, 0, intent2, 0);
}
}
推荐答案
最后,我找到了解决这个问题的方法。
Finally, I've found solution to this problem.
我们可以在这里找到答案:
We can simply find answer here:
要解决上述问题,我们可以使用Alarm Manager。
To solve the above problem, we can use Alarm Manager.
因此,在打盹模式完成之后,或在维护窗口期间,它将自动执行在警报管理器服务中编写的代码。
So right after the doze mode finishes, or during a maintenance window, it will automatically execute the code, written in alarm manager service.
您可以在此处获得帮助,从此stackoverflow帖子中的窗口小部件中使用Alarm Manager:
You can get help here to use Alarm Manager in your widget from this stackoverflow post:https://stackoverflow.com/a/14319020/3497865
这篇关于在screen_on上更新android小部件的最佳方法是什么。是否支持Android Oreo(API 26)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!