Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: android: set do not terminate widget after screen off

Codigo:

My Widget shows some price-data when a button is pushed. On some Android phones (e.g. Huawei P9), my Widget gets terminated (is "dead" although still on the screen) after the screen turns off. It is possible, to change the relevant setting manually in Settings -> Applications -> MyWidget -> Battery -> Do not terminate after screen turns off -> On. Then it works fine also after the screen is off and then on again.

I would like to set the setting programmatically in the widget right after its download/installation, so even a user without special knowledge does need to search for the reason why the widget is terminated after the screen turns off. Some apps on the phone (Huawei P9) have, after download, the battery setting automatically set to "do not terminate after screen turn off", so it should be possible. I have tried to resolve the issue with a WakeLock, but no result. Please, can anybody advice, how to programmatically set the battery setting "Do not terminate after the screen turns off" -> ON, right after widget installation on the screen (or even after its download)?

This is my WidgetProvider code:




public class MyWidgetProvider extends AppWidgetProvider {

private static final String TAG = "ExampleWidget";
PowerManager powerManager;
PowerManager.WakeLock wakeLock;


@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(OUROWESOME_WIDGET_UPDATE)) {
makeWidget(context);
}
}

@Override
public void onDisabled(Context context) {
wakeLock.release();
}


@Override
public void onEnabled(Context context) {
super.onEnabled(context);
powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

makeWidget(context);

}


public static PendingIntent buildButtonPendingIntent(Context context) {

// initiate widget update request
Intent intent = new Intent();
intent.setAction(WIDGET_UPDATE_ACTION);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

protected static CharSequence getDesc() {
return "";
}

protected static CharSequence getTitle() {
return "Push the button to get the actual price";
}

public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {

ComponentName myWidget = new ComponentName(context, MyWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
int[] allWidgetIds = manager.getAppWidgetIds(myWidget);
manager.updateAppWidget(allWidgetIds, remoteViews);

}

public static void makeWidget(Context context) {

// initializing widget layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widgetlayout);

// register for button event
remoteViews.setOnClickPendingIntent(R.id.sync_button, buildButtonPendingIntent(context));

// updating view with initial data
remoteViews.setTextViewText(R.id.title, getTitle());
remoteViews.setTextViewText(R.id.desc, getDesc());

// request for widget update
pushWidgetUpdate(context, remoteViews);
}
}




Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: android: set do not terminate widget after screen off

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×