6

The problem is that after I use the built in Task Manager's Clean Memory/Ram, My widget stops working .I guess this is related to the Task Manager's method of cleaning RAM.After a lot of research and some attempts, I found that i need BroadcastReciever to listen to package changes and updates: So i implemented but its not working because document says that the Restarted/Cleared package does not receive this broadcast

register receiver in the manifest file:

<receiver android:name="com.app.lab.receiver.onRestartReciever">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACED" />
        <action android:name="android.intent.action.PACKAGE_RESTARTED" />
         <action android:name="android.intent.action.PACKAGE_DATA_CLEARED"/>
        <data android:scheme="package"  />
    </intent-filter>

PACKAGE_REPLACED - called in particular to notify application update.

PACKAGE_RESTARTED - called when most memory cleaners are cleaning memmory.

the "data" row is used to monitor action applied for the specific package.

public class onRestartReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("DEBUG", "onRestartReciever");//I am not getting this log on clearing memory from task manager



}
}

I tried to use dummy service to get its lifecycle ie to check when onDestroy is called but what I found it not a reliable way ,onDestroy may not be called when application is killed by Task Manager.

So finally, my question : Is there any way to tell the android system to reStart appWidgets when Task manager or OS cleans memory .

Note: My widget contains only one button that launches an Activity.It works most of the time but stops responding if OS itself cleans memory or user forcefully do it from task manager.I've downloaded some of the widget it seem to continue working fine after cleaning memory also.

Update: To under my problem no need of going through complete code it is simple Application . My application dose not contain any Activty or Service. It contains only widget with one button which gives toast message.There is only two class in my application(WidgetProvider and onRestartReciever) thats it Widget class WidgetProvider.class

 public class WidgetProvider extends AppWidgetProvider {

private RemoteViews remoteViews;
private ComponentName watchWidget;
PendingIntent pi;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.touchwidget);
   Intent toggleClickPlayer = new Intent(context.getApplicationContext() ,WidgetProvider.class);
    toggleClickPlayer.setAction("PLAYER");
    PendingIntent toggleIntentPlayer = PendingIntent.getBroadcast(context,0, toggleClickPlayer,endingIntent.FLAG_CANCEL_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.player, toggleIntentPlayer);
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    watchWidget = new ComponentName(context,WidgetProvider.class);
    remoteViews = new RemoteViews(context.getPackageName(),R.layout.touchwidget);
Toast.makeText(context, " Player started",Toast.LENGTH_SHORT).show();
(AppWidgetManager.getInstance(context)).updateAppWidget(watchWidget, remoteViews);
        }
    } 
 }
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64

1 Answers1

0

Widget does not bound to application life cycle. All the widgets are bound together. If all what your widget got is a button than there is no reason for it to stop working. You problem is some place else. For some reason your button intent is fail to start what you set it to start.

If you show me your code for setting the button, I be able to help you more, but it is another question, and you better Google for answer before posting.

EDIT: It looks like you didn't understood the idea of widgets. Right now what your button is doing is starting the widget. It looks weird to me, I am not sure what exactly is happening there... I suggest that your button will start a completely new service. That service will show your toast. You defiantly do not need to listen for restart package broadcast.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • I have update my question with complete java code of my application .My application dose not contain any Activty or Service. It contains only widget with one button which gives toast message.There is only two class in my application(WidgetProvider and onRestartReciever) thats it Widget class – Shakeeb Ayaz Dec 19 '13 at 13:47
  • there are several issues related to widget https://code.google.com/p/android/issues/detail?id=28216 – Shakeeb Ayaz Dec 19 '13 at 13:52
  • my widget works fine all the time except after cleaning memory.. Button broadcasts an event from OnUpdate which calls my onReceive of Appwidgetprovider .My widget is not starting widget(wat u said) it is broadcasting an event.I think u did not went through my code – Shakeeb Ayaz Dec 19 '13 at 14:01
  • why to use service at all if AppwidgetProvider class itself contains onReceive ..Why to unnecessary create another service class if it can be handled by its on onReceive method..It is just a toast msg – Shakeeb Ayaz Dec 19 '13 at 14:07
  • @ShakeebAyaz because AppWidgetProvider is not a service is not even a context. it's just a BroadcastReceiver. And there for it cannot be used for such propose. – Ilya Gazman Dec 19 '13 at 14:10
  • @ShakeebAyaz it was a technical explanation to simple answer, you can't do it! You should use service instead. Service and AppWidgetProvider are two completely different things. – Ilya Gazman Dec 19 '13 at 14:13
  • then what is use of the onRecieve of AppwidgetProvider class.That means google simple added it to this class without any use..By the way I already tried it using service too..same scene no improvement – Shakeeb Ayaz Dec 19 '13 at 14:16
  • whenever u get time just create a dummy widget and then clean the momory from Task manager see what happens...u will be shocked widget will stop working – Shakeeb Ayaz Dec 19 '13 at 14:19
  • @ShakeebAyaz Check out this [app](https://play.google.com/store/apps/details?id=com.rechild.advancedtaskkiller&hl=en) it's widget working just fine, and they only use service and activity for their intent. Your just need to use service as I told you. – Ilya Gazman Dec 19 '13 at 14:27
  • I know there r several apps in market whose widget works fine after cleaning memory. But my question is how they do that (it is not because of service),why is onReceive there in AppwidgetProvider ,why to use service for sort running task which can be handled by onReceive ... – Shakeeb Ayaz Dec 19 '13 at 14:32
  • if u dont believe me just make a widget – Shakeeb Ayaz Dec 19 '13 at 14:34
  • @ShakeebAyaz I am facing same issue in 2020 let me know if you figured out something, Regards. – Deep Dave Jun 04 '20 at 06:07