Widget does not works after “Clear Memory”

1.7k views Asked by At

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);
        }
    } 
 }
1

There are 1 answers

12
Ilya Gazman On

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.