Lags on widget update - android

I'm interesting building a simple clock widget here. And I wonder what is the best practice to do it? Most of the time it works fine but some says my clock widget lags behind. Actual time is 10.00am then my widget shows perhaps 9.48am
I have this on my manifest
<receiver
android:name="my.package.name.MyClock"
android:label="#string/widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/my_clock" />
</receiver>
<service
android:name="MyClock$UpdateService"
android:label="UpdateService" >
<intent-filter>
<action android:name="my.package.name.UPDATE" />
</intent-filter>
</service>
And this is my main java class
public class MyClock extends AppWidgetProvider {
#Override
public void onDisabled(Context context) {
super.onDisabled(context);
context.stopService(new Intent(context, UpdateService.class));
}
#Override
public void onEnabled(Context context) {
super.onEnabled(context);
context.startService(new Intent(UpdateService.ACTION_UPDATE));
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
context.startService(new Intent(UpdateService.ACTION_UPDATE));
}
public static final class UpdateService extends Service {
static final String ACTION_UPDATE = "my.package.name.UPDATE";
private final static IntentFilter sIntentFilter;
private String mMinuteFormat;
private String mHourFormat;
private Calendar mCalendar;
static {
sIntentFilter = new IntentFilter();
sIntentFilter.addAction(Intent.ACTION_TIME_TICK);
sIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
sIntentFilter.addAction(Intent.ACTION_TIME_CHANGED);
}
#Override
public void onCreate() {
super.onCreate();
reinit();
registerReceiver(mTimeChangedReceiver, sIntentFilter);
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mTimeChangedReceiver);
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
reinit();
update();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void update() {
mCalendar.setTimeInMillis(System.currentTimeMillis());
final CharSequence minute = DateFormat.format(mMinuteFormat, mCalendar);
final CharSequence hour = DateFormat.format(mHourFormat, mCalendar);
RemoteViews views = null; views = new RemoteViews(getPackageName(), R.layout.main);
views.setTextViewText(R.id.HOUR, hour);
views.setTextViewText(R.id.MINUTE, minute);
//Refresh the widget
ComponentName widget = new ComponentName(this, MyClock.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(widget, views);
}
private void reinit() {
mHourFormat = "hh";
mMinuteFormat = "mm";
}
private final BroadcastReceiver mTimeChangedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_TIME_CHANGED) ||
action.equals(Intent.ACTION_TIMEZONE_CHANGED))
{
reinit();
}
update();
}
};
} }
What am I missing? Why the widget lags behind?
Can you please help me spot the issue here?
And am I doing correct approach? Using Service not AlarmManager to have clock widget updates each minute?
Regards

Quite a lot of reason that may leads to this problem, but most probably, is the Service is killed by System. There's no way to prevent a background service being killed by System, only making it foreground service will be safe in most of the time, but the notification icon is very annoying to user.
I think using AlarmManager would be the best, I recently updated my clock widget using this technique too. Since AlarmManager makes broadcast, even your application is killed, it will recreate it before sending.

Related

Android - Appwidget with Remoteviews not updating after reboot

I saw similar questions here on SO, but nothing seems to work in my case...
I created an appwidget with an AdapterViewFlipper (Simple ViewAnimator that will animate between two or more views that have been added to it). The appwidget has a Next button that enables the user to navigate to the next view on the widget.
It all works fine when I first add the appwidget. But if the smartphone reboots, the Next button of the widget no longer works on my Samsung S4 (the method onReceive is called, but nothings happens, it doesn't navigate to the next view and is stuck at the first view). I have to delete the widget and add it again in order for it to work...
I suspect that it is a problem of Touchwiz since I tested it on another phone (Moto G) and it worked fine.
Here are some portions of my code :
AppWidgetProvider
public class AppWidgetProvider extends AppWidgetProvider {
public static final String NEXT_ACTION = VersionUtil.getPackageName() + ".action.NEXT";
private static final String TAG = DailyAppWidget.class.getSimpleName();
#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 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, colorValue);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId, int primaryColor) {
Intent intent = new Intent(context, ViewFlipperWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// Instantiate the RemoteViews object for the app widget layout.
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.app_widget);
// open the activity from the widget
Intent intentApp = new Intent(context, MainActivity.class);
intentApp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentApp, 0);
rv.setOnClickPendingIntent(R.id.widget_title, pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
rv.setRemoteAdapter(R.id.adapter_flipper, intent);
} else {
rv.setRemoteAdapter(appWidgetId, R.id.adapter_flipper, intent);
}
// Bind the click intent for the next button on the widget
final Intent nextIntent = new Intent(context,
AppWidgetProvider.class);
nextIntent.setAction(AppWidgetProvider.NEXT_ACTION);
nextIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
final PendingIntent nextPendingIntent = PendingIntent
.getBroadcast(context, 0, nextIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
rv.setOnClickPendingIntent(R.id.widget_btn_next, nextPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, mRemoteViews);
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(NEXT_ACTION)) {
RemoteViews rv = new RemoteViews(context.getPackageName(),
R.layout.daily_app_widget);
rv.showNext(R.id.adapter_flipper);
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
Log.e(TAG, "onReceive APPWIDGET ID " + appWidgetId);
AppWidgetManager.getInstance(context).partiallyUpdateAppWidget(
appWidgetId, rv);
}
super.onReceive(context, intent);
}
Service
public class FlipperRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private Context mContext;
private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
private static final String TAG = "FILPPERWIDGET";
public FlipperRemoteViewsFactory(Context context, Intent intent) {
mContext = context;
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
//... get the data
}
#Override
public void onCreate() {
Log.e(TAG, "onCreate()");
}
#Override
public void onDataSetChanged() {
Log.i(TAG, "onDataSetChanged()");
}
#Override
public void onDestroy() {
}
#Override
public int getCount() {
//... return size of dataset
}
#Override
public RemoteViews getViewAt(int position) {
Log.i(TAG, "getViewAt()" + position);
RemoteViews page = new RemoteViews(mContext.getPackageName(), R.layout.app_widget_item);
//... set the data on the layout
return page;
}
#Override
public RemoteViews getLoadingView() {
Log.i(TAG, "getLoadingView()");
return new RemoteViews(mContext.getPackageName(), R.layout.appwidget_loading);
}
#Override
public int getViewTypeCount() {
Log.i(TAG, "getViewTypeCount()");
return 1;
}
#Override
public long getItemId(int position) {
Log.i(TAG, "getItemId()");
return position;
}
#Override
public boolean hasStableIds() {
Log.i(TAG, "hasStableIds()");
return true;
}
}
Manifest
<receiver android:name=".AppWidgetProvider"
android:label="#string/app_name"
android:enabled="#bool/is_at_least_12_api">
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/app_widget_info" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>
<!-- Service serving the RemoteViews to the collection widget -->
<service android:name=".ViewFlipperWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS"
android:exported="false" />
app wigdet info
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="#layout/app_widget"
android:initialLayout="#layout/app_widget"
android:minHeight="110dp"
android:minWidth="250dp"
android:previewImage="#drawable/widget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="14400000"
android:widgetCategory="home_screen" />
Any help would be appreciated !
Depends on the launcher, there is no guarantee that your AppWidget will be updated immediately after the device started. It may be refreshed immeidately, or wait till the updatePeriodMillis passed after system started.
To solve your problem, define a BroadcastReceiver that will trigger the update of AppWidget after the reboot.
In AndroidManifest.xml, define the BootReceiver to get the boot_complete message.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootReceiver" android:enabled="true" android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And define the BootReceiver.java to start your AppWidgetUpdateService
public class BootReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
//start appwidget update service
}
}

Workaround for AppWidgetProvider's onEnabled & onDisabled is not being called

Since I'm using AlarmManager to perform periodical widget update, I need to ensure onEnabled & onDisabled will work reliably.
However, I realize they will not be triggered sometimes. I'm not the only one who is facing this problem.
Android appWidgetProvider onEnabled never called on tablet
Is there any official bug ticket submitted to Google Android team?
Is there any workaround, especially onDisabled? As I do not want AlarmManager still being triggered repeatably, after the last widget had been removed.
AndroidManifest.xml
<receiver android:name="org.yccheok.MyAppWidgetProvider"
android:exported="true" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
<action android:name="android.appwidget.action.APPWIDGET_DELETED" />
<action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
public class MyAppWidgetProvider extends AppWidgetProvider {
private static PendingIntent createAlarmUpdatePendingIntent(Context context) {
Intent intent = new Intent(context, JStockAppWidgetProvider.class);
intent.setAction(JStockAppWidgetProvider.ALARM_UPDATE_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
#Override
public void onEnabled(Context context)
{
super.onEnabled(context);
PendingIntent pendingIntent = createAlarmUpdatePendingIntent(context);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
int scanSpeed = JStockApplication.instance().getJStockOptions().getScanSpeed();
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + scanSpeed, scanSpeed, pendingIntent);
}
#Override
public void onDisabled(Context context)
{
super.onDisabled(context);
PendingIntent pendingIntent = createAlarmUpdatePendingIntent(context);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}
you can check in method a number of instances that are currently running.
private boolean hasInstances(Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
new ComponentName(context, this.getClass()));
return (appWidgetIds.length > 0);
}
I afraid you have misunderstood the methods of AppWidgetProvider.
onDeleted(Context context, int[] appWidgetIds)
Called in response to the ACTION_APPWIDGET_DELETED broadcast **when one or more** AppWidget instances have been deleted. Override this method to implement your own AppWidget functionality.
onDisabled(Context context)
Called in response to the ACTION_APPWIDGET_DISABLED broadcast, which is sent when the last AppWidget instance for this provider is deleted. Override this method to implement your own AppWidget functionality.
In short, If you have two or more AppWidget instances then if you remove any of them at that time only onDeleted() method for particular widget will be called.
If you have only single AppWidget instance then if you remove that time onDesabled() and onDeleted() both will be called.
So you will have to move your code from onDesabled() method to onDeleted() method and it will get called every time.!
Also take care that onEnabled() will be called only for the first instance and not for every next instance you create.
For what I understood you just need one alarm, and I suppose all widgets will be all the same. So, the idea for your alarm is run on service, not on widget.
You shouldn't run long time actions on BroadcastReceiver.
We will use the onUpdate and not onEnable (because the design of this demo). Then aways we get a new widget we can perform the service.
AppWidgetProvider:
public class MyAppWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
{
ComponentName thisWidget = new ComponentName(context, MyAppWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
Intent intent = new Intent(context.getApplicationContext(), JStockAppWidgetService.class);
intent.setAction(JStockAppWidgetService.ALARM_UPDATE_ACTION);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
context.startService(intent);
}
#Override
public void onDisabled(Context context)
{
super.onDisabled(context);
Intent intent = new Intent(context.getApplicationContext(), JStockAppWidgetService.class);
intent.setAction(JStockAppWidgetService.ALARM_STOP_ACTION);
// I kept this just in case you wanna keep running your alarm without widget. You can just stopService here too.
context.startService(intent);
}
}
And here the Service:
public class JStockAppWidgetService extends Service {
public static final String ALARM_UPDATE_ACTION = "ALARM_UPDATE_ACTION";
public static final String ALARM_STOP_ACTION = "ALARM_STOP_ACTION";
//delay to refresh your widget
private int delay = 10000; //10 secs
private Thread myThread;
#Override
public void onStart(Intent intent, int startId) {
final int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
try {
//running your timeout while is not interrupted
while(!Thread.currentThread().isInterrupted()) {
//we need to back to GUI thread
handler.post(new Runnable() {
#Override
public void run() {
runInMyGuiThread(allWidgetIds);
}
});
//everybody needs to sleep sometime =p
Thread.sleep(delay);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}
};
String action = intent.getAction();
if(action == ALARM_UPDATE_ACTION) {
if(myThread != null)
myThread.interrupt();
myThread = new Thread(runnable);
myThread.start();
} else if(action == ALARM_STOP_ACTION && myThread != null) {
myThread.interrupt();
}
}
//here you are in GUI thread with all your widgets id
public void runInMyGuiThread(int[] allWidgetIds) {
for(int widgetId : allWidgetIds){
//do what you want to update each widget
}
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
This is just a little demo, you can stop the service or keep it running without widget, I will let with you.
I was confused by the following situation.
If you have added the widget to the lock screen and then you are testing by adding and removing the widget to the home screen, then you never see onEnabled/onDisabled called. The reason is that there is still a widget added - the lock screen widget.

My android widget service stops after some time

I have an issue with my clock widget. I run a Service to refresh it when needed (on USER_PRESENT event and each minute while screen on), but this Service seems to be killed after some time, randomly, and is not restarted.
I have set notifications in my Service's onDestroy() and onLowMemory() methods but they are never called.
I never call the stopSelf() method.
The widget is no longer visible in launched application list into the system.
When the WidgetProvider naturally updates (every 6 hours), the service is restarted and all works perfectly for a moment.
Here is my code :
(note that the Application in the manifest is not named nor created in the code. Only the provider and service. don't know if it's correct)
Widget Provider :
public class WidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] ids) {
super.onUpdate(context, appWidgetManager, ids);
Intent serviceIntent = new Intent(context.getApplicationContext(), WidgetService.class);
context.startService(serviceIntent);
}
#Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
#Override
public void onDisabled(Context context) {
Intent serviceIntent = new Intent(context.getApplicationContext(), WidgetService.class);
context.stopService(serviceIntent);
super.onDisabled(context);
}
}
The Service :
public class WidgetService extends Service {
private static String LOG = WidgetService.class.getSimpleName();
public Hashtable<Integer, ClockView> views = new Hashtable<Integer, ClockView>();
AppWidgetManager man;
private static WidgetService self;
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(this, LOG+" configuration change", Toast.LENGTH_LONG).show();
}
#Override
public void onCreate() {
super.onCreate();
man = AppWidgetManager.getInstance(this);
self = this;
// Registering Intent for screen state
...
registerReceiver(screenoffReceiver, filter);
// Registering Intent for click event
...
UpdateAllWidgets();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
UpdateAllWidgets();
return START_STICKY;
}
#Override
public void onDestroy() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.holder)
.setContentTitle("WidgetClock")
.setContentText("Destroyed at "+Calendar.getInstance().getTime());
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(1, mBuilder.build());
super.onDestroy();
}
#Override
public void onLowMemory() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.holder)
.setContentTitle("WidgetClock")
.setContentText("LowMemory at "+Calendar.getInstance().getTime());
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(1, mBuilder.build());
super.onLowMemory();
}
public void onScreenOff() {
}
private void onScreenOn() {
}
public void onUserPresent() {
}
public void UpdateAllWidgets() {
views.clear();
int[] ids = man.getAppWidgetIds(new ComponentName(this, WidgetProvider.class));
for (int id : ids)
createViewIfNecessary(id);
}
private void createViewIfNecessary(int id) {
if (!views.containsKey(id)) {
ClockView view = new ClockView(this, id);
views.put(id, view);
}
}
public static Context getApp(){
return self;
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
The manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.widgetclock"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:clickable="true" >
<service android:name="WidgetService"></service>
<receiver android:name="WidgetProvider"
android:clickable="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/widget_provider_file" />
</receiver>
</application>
The widget provider config XML
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="250dip"
android:minHeight="125dip"
android:updatePeriodMillis="21600000"
android:initialLayout="#layout/widget_main">
</appwidget-provider>

How to stop service when removing widget from homescreen

I have a problem with service. I'm trying to do a simple battery level widget but the problem is that I don't know how to stop service when the widget is removed from homescreen.
Here is my service class
public class batteryService extends Service{
AppWidgetManager widgetManager;
private RemoteViews remoteViews;
private ComponentName thisWidget;
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
widgetManager = AppWidgetManager.getInstance(context);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
thisWidget = new ComponentName(context, Widget.class);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)*100;
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float value = (float)level/scale;
remoteViews.setTextViewText(R.id.widget_textview, String.valueOf(value));
widgetManager.updateAppWidget(thisWidget, remoteViews);
}
};
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
this.registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
super.onCreate();
}
#Override
public void onDestroy() {
this.unregisterReceiver(mBatInfoReceiver);
super.onDestroy();
}
}
and here widget class
public class Widget extends AppWidgetProvider{
private AppWidgetManager widget_manager;
private RemoteViews remoteViews;
private ComponentName thisWidget;
private Intent intent;
#Override
public void onEnabled(Context context) {
if(intent == null)
{
intent = new Intent(context, batteryService.class);
context.startService(intent);
}
super.onEnabled(context);
}
#Override
public void onDisabled(Context context) {
if(intent != null)
{
context.stopService(intent);
intent = null;
}
super.onDisabled(context);
}
}
When I remove widget from screen the service is still a live. I was trying to change onDisable to onDeleted method but it didn't change anything.
Have you any tips for me ?
Make sure you have the appropriate action in your manifest for the disabled event:
<action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
This would be alongside your existing APPWIDGET_ENABLED and perhaps the classic APPWIDGET_UPDATE actions, for the <receiver> element for your AppWidgetProvider.
Also, please consider not having your service run constantly, but rather updating the battery information once every few minutes using AlarmManager. The battery level simply does not change that often to make it necessary to attempt to keep a service in memory all of the time.
Try by using onDeleted(Context, int[])

Android: Clickable imageview widget

I want to make a very simple widget:
It must consist from just one image view
1) on incoming sms it should change the image
2) on click it also should change the image
I tried to make it using ImageButton but failed: there were problems with changing the image on sms received event: new image had wrong scale.
Anyway now I want to make an ImageView without anything else.
The problem is that I can't handle onClick event:
I've got a running service which should handle all events: sms received and click:
widget provider:
public class MyWidgetProvider extends AppWidgetProvider {
#Override
public void onDisabled(Context context) {
context.stopService(new Intent(context, UpdateService.class));
super.onDisabled(context);
}
#Override
public void onEnabled(Context context) {
super.onEnabled(context);
Intent intent = new Intent(context, UpdateService.class);
context.startService(intent);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
Intent widgetClickIntent = new Intent(context, UpdateService.class);
widgetClickIntent.setAction(UpdateService.ACTION_ON_CLICK);
PendingIntent pendingIntentViewClick = PendingIntent.getService(context, 0, widgetClickIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_imageview, pendingIntentViewClick);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
}
service:
public class UpdateService extends Service {
static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
static final String ACTION_ON_CLICK = "android.MyWidget.ACTION_ON_CLICK";
private final static IntentFilter intentFilter;
static {
intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_SMS_RECEIVED);
intentFilter.addAction(ACTION_ON_CLICK);
}
public final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(ACTION_SMS_RECEIVED)) {
reactOnSms(context);
}
if (action.equals(ACTION_ON_CLICK)) {
onCLick(context);
}
}
};
#Override
public void onCreate() {
super.onCreate();
registerReceiver(broadcastReceiver, intentFilter);
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void reactOnSms(Context context) {
// doSomething
}
public void onCLick(Context context) {
// doSomething
}
Manifest:
<receiver a:name="..."...>
<intent-filter>
<action a:name="android.provider.Telephony.SMS_RECEIVED"/>
<action a:name="android.MyWidget.ACTION_ON_CLICK"/>
</intent-filter>
<meta-data a:name="android.appwidget.provider"
a:resource="#xml/my_widget_provider_info"/>
</receiver>
<service a:name=".UpdateService"
a:label="UpdateService">
<intent-filter>
<action a:name="android.provider.Telephony.SMS_RECEIVED"/>
<action a:name="android.MyWidget.ACTION_ON_CLICK"/>
</intent-filter>
</service>
I tried this Clickable widgets in android
I found the solution.
Sorry for those of you who read the question. Too much code inside, I understand.
The problem was that UpdateService was not the real handler of the broadcast intent. Anonymous implementation of BroadcastReceiver made all the work.
So the problem was in this code (widgetProvider):
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
// wrong:
// Intent widgetClickIntent = new Intent(context, UpdateService.class);
// widgetClickIntent.setAction(UpdateService.ACTION_ON_CLICK);
// PendingIntent pendingIntentViewClick = PendingIntent.getService(context, 0, widgetClickIntent, 0);
// correct:
Intent widgetClickIntent = new Intent(UpdateService.ACTION_ON_CLICK);
PendingIntent pendingIntentViewClick = PendingIntent.getBroadcast(context, 0, widgetClickIntent, 0);
///////
remoteViews.setOnClickPendingIntent(R.id.widget_imageview, pendingIntentViewClick);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}

Categories

Resources