AppWidget alarmmanager not updating - android

So, I've got a widget, I want it to update every 60 seconds. When the widget is first added to the homescreen, it runs its update function just fine. Beyond that it's supposed to start an AlarmManager, which will rerun the update method every 60 seconds. That's the part that it doesn't seem to be actually doing. Here's my code:
public class ClockWidget extends AppWidgetProvider {
public static String CLOCK_WIDGET_UPDATE = "com.nickavv.cleanwidget.CLEANCLOCK_UPDATE";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetIds[]) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clocklayout);
appWidgetManager.updateAppWidget(appWidgetId, views);
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Log.d("log","Entered update cycle");
//Unimportant for these purposes
appWidgetManager.updateAppWidget(appWidgetId, views);
}
private PendingIntent createClockTickIntent(Context context) {
Intent intent = new Intent(CLOCK_WIDGET_UPDATE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
#Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.d("onEnabled","Widget Provider enabled. Starting timer to update widget every minute");
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 60000, createClockTickIntent(context));
}
#Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.d("onDisabled", "Widget Provider disabled. Turning off timer");
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(createClockTickIntent(context));
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.d("onReceive", "Received intent " + intent);
if (CLOCK_WIDGET_UPDATE.equals(intent.getAction())) {
Log.d("onReceive", "Clock update");
// Get the widget manager and ids for this widget provider, then
// call the shared
// clock update method.
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), getClass().getName());
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
for (int appWidgetID: ids) {
updateAppWidget(context, appWidgetManager, appWidgetID);
}
}
}
}
It's the product of a few tutorials I've found on the matter, and my own knowledge of Android. According to my logcats, it never gets to the Log.d("onReceive", "Clock update"); line. And yes, my Manifest is set up with the clock update intent. Thanks!
EDIT: Additional info. I put a log line in the createClockTickIntent method, and it fires off. So I guess this means that my application is running the alarmManager.setRepeating line, no idea why is isn't actually repeating.

Arggghhh, it was a simple typo. The intent filter was "com.nickavv.cleanwidgets.CLEANCLOCK_UPDATE", and I had written "com.nickavv.cleanwidget.CLEANCLOCK_UPDATE"
What a pain, but hey, now I know.
So, moral of the story for anybody with a similar problem to me: Check your spelling! Check it twice, or ten times. On everything!

You have to make an appwidget-provider and set updatePeriodMillis on 0.6 second , then it will be update per 60 seconds.
<?xml version="1.0" encoding="utf-8" ?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dp"
android:initialLayout="#layout/YourLayout"
android:updatePeriodMillis="0.6"
android:minHeight="144dp"/>

Related

In Appwidget RemoteViews' setOnClickPendingIntent is not getting called after user force stops application

Here i have made one widget and added its click listener where i'm starting one service.It works perfectly.But once user force stops application from appinfo my widget's click is not responding.I m not getting why this is happening.Help on this will be appreciated.Thank you
My app widget class code:
MyWidget.java:
public class MyWidget extends AppWidgetProvider {
static Context cont;
static SharedPreferences preferences;
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
cont = context;
Intent intent2 = new Intent();
intent2.setAction("....");
PendingIntent pendingIntent = PendingIntent.
getBroadcast(context, 0,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);
views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
context.startService(new Intent(context, MyService.class));
}
#Override
public void onEnabled(Context context) {
.................. }
#Override
public void onDisabled(Context context) {
.................}
Always use an explicit Intent whenever possible. Your code would not work on Android 8.0 and higher, where implicit broadcasts are banned. And an explicit Intent can move your app out of the stopped state.

Multiple widgets giving same appWidgetId on click on each widgets

I need to create Widgets in my android app similar work flow of contact widgets in android. But the problem facing is if there are n widgets of the same application each widgets have different functionality. how to differentiate the button click on each widgets.?
public class MyWidgetActivity extends AppWidgetProvider {
private static final String MyOnClick = "myOnClickTag";
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = MyWidgetActivityConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.simple_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
views.setOnClickPendingIntent(R.id.actionButton, getPendingSelfIntent(context, MyOnClick,views.getLayoutId()));
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
// Tell the AppWidgetManager to perform an update on the current app
// widget
}
}
#Override
public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) {
MyWidgetActivityConfigureActivity.deleteTitlePref(context, appWidgetId);
}
}
#Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
#Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (MyOnClick.equals(intent.getAction()))
{
int appWidgetId = intent.getIntExtra(EXTRA_APPWIDGET_ID,0);
Log.e("KOKOKOKO :: ",""+appWidgetId);
MyWidgetActivityConfigureActivity.loadIdPrefCallQuick(context,appWidgetId);
//your onClick action is here
Toast.makeText(context,"Quick Action Triggered",Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}
static PendingIntent getPendingSelfIntent(Context context, String action, int appWidgtId)
{
// if we brodcast than definetly we have to define ONRECEIVE
Intent intent = new Intent(context, MyWidgetActivity.class);
intent.putExtra(EXTRA_APPWIDGET_ID,appWidgtId);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
Instead of this code (static PendingIntent getPendingSelfIntent) :
return PendingIntent.getBroadcast(context, 0, intent, 0);
You should do this :
return PendingIntent.getBroadcast(context, appWidgtId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This way you should get unique intent and also unique appWidgtId.
It is important to set requestCode (2nd attribute of getBroadcast), so it wouldn't conflict.

Android: Home screen Widgets: Use of Broadcast Intents?

I am trying to understand an app that communicates with a widget on the home screen. But i do not understand what the following code does within the application:
Intent i = new Intent(this, AppWidget.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
updateViews.setOnClickPendingIntent(R.id.phoneState, pi);
return updateViews;
Full Class:
public class AppWidget extends AppWidgetProvider {
// This is called for every broadcast. We normally don't need to implement this
// method because the default AppWidgetProvider implementation filters all App Widget
// broadcasts and calls the above methods as appropriate
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == null) {
context.startService(new Intent(context, ToggleService.class));
} else {
super.onReceive(context, intent);
}
}
#Override
// This is called to update the App Widget at intervals defined by
// the updatePeriodMillis attribute in the AppWidgetProviderInfo. This method is also called when
// the user adds the App Widget, so it should perform the essential setup, such as define event
// handlers for Views and start a temporary Service, if necessary.
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
context.startService(new Intent(context, ToggleService.class));
}
// This class is used to set up the intent service in order to provide views for
// the widget. It also supports to set up a pending intent. Furthermore, the app widget can be
// updated with a remote adapter.
public static class ToggleService extends IntentService {
public ToggleService() {
super("AppWidget$ToggleService");
}
#Override
protected void onHandleIntent(Intent intent) {
ComponentName me = new ComponentName(this, AppWidget.class);
AppWidgetManager mgr = AppWidgetManager.getInstance(this);
mgr.updateAppWidget(me, buildUpdate(this));
}
private RemoteViews buildUpdate(Context context) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
AudioManager audioManager = (AudioManager) context
.getSystemService(Activity.AUDIO_SERVICE);
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
updateViews.setImageViewResource(R.id.phoneState,
R.drawable.phone_state_normal);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
} else {
updateViews.setImageViewResource(R.id.phoneState,
R.drawable.phone_state_silent);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
//KNOW THIS CODE
Intent i = new Intent(this, AppWidget.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
updateViews.setOnClickPendingIntent(R.id.phoneState, pi);
return updateViews;
}
}
}
All it does is open the ToggleService when the user taps on the viewId R.id.phoneState.
It's kinda of a silly way of doing it, the more straight forward would be:
Intent i = new Intent(this, ToggleService.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
updateViews.setOnClickPendingIntent(R.id.phoneState, pi);
you see, this code you posted calls the BroadcastReceiver that then calls the service. Makes no sense.

Android Widget: OnClickPendingIntent won't Work

Why doesn't recieve get called when I click on Button wid??
Code:
public class Widget extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, Widget.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.wid, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.d("ARH","CLICKK");
}
Becuase I need a button to manual refresh the widget but it seems that Log.d("ARH","CLICKK"); only gets called when i add the Widget.
Thanks!
You using a PendingIntent to call an Activity but your Widget class is not an activity.
If you want to update to your widget, then you need to use getBroadcast that sends an APPWIDGET_UPDATE action.

Android OnClickPendingIntent ( widget -> service ) firing by itself

I've ran into a pretty interesting problem - I have a widget that when pressed, would fire a OnClickPendingIntent to invoke a service that would play a sound. But from time to time, about one an hour, it would fire by itself.
widget:
public class WidgetActivity extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent intent = new Intent(context.getApplicationContext(), SilentService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getService(
context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.layout, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
context.startService(intent);
}
}
service:
public class SilentService extends Service {
#Override
public void onStart(Intent intent, int startId) {
Log.e("UpdateWidgetService", "Called");
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (appWidgetIds.length > 0) {
for (final int widgetId : appWidgetIds) {
final RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget_layout);
remoteViews.setImageViewResource(R.id.ivc, R.drawable.pic1);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
MediaPlayer mpl = MediaPlayer.create(this, R.raw.music1);
int intRand = random.nextInt(5)+1;
Log.e("r",String.valueOf(intRand));
mpl.start();
mpl.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
remoteViews.setImageViewResource(R.id.ivc, R.drawable.pic2);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
});
}
stopSelf();
}
super.onStart(intent, startId);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
Thanks!
I have a feeling Android is killing your service and then your code is trying to start it again. I have seen similar issues. Give this a read: http://developer.android.com/guide/topics/fundamentals.html#proclife
When your service restarts after Android tries to get memory back, it is trying to start the service, which creates a new media player, and starts it back up. This is my assumption without knowing exactly how the code is setup, but based on what I see it's most likely the issue.

Categories

Resources