Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

java - Service does not restart after "Clear Memory" + appWidget crashes

I've built an appWidget which register some services on his onEnabled() method.

The problem is that after I use the built in Task Manager's Clean Memmory/Ram, the appWidget view crashes (all the appWidgets TextView's text is set to default (TextView)) and the Services stop running and never restarts.

This only happen after some time the widget is set, and if I Clean Memmory/Ram right after the widget is set the bug does'nt happen, but I guess this is related to the Task Manager's method of cleaning RAM.

So finally, my question is: Is there a way to tell the android system to reStart those services? as other appWidgets I've downloaded through the market is seem to continue working fine after this procedure.

Will be happy for ideas and solutions! Thanks advanced, Gal :)

some code that I use:

the onEnabled() method in the appWidget:

@Override
public void onEnabled(Context context) {
    super.onEnabled(context);

    Intent newinIntent = new Intent(context, CallService_1x1.class);
        context.startService(newinIntent);

    newinIntent = new Intent(context, SmsService_1x1.class);
        context.startService(newinIntent);
}

Some methods from one of the Services (others services are very similiar as this is from their abstract method):

 @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      // We want this service to continue running until it is explicitly
      // stopped, so return sticky.
      Log.d("SERVICE-SMS","CallMonitorService - onStartCommand created");
      return START_STICKY;
  } 

@Override
  public void onCreate() {
    super.onCreate();
    context = this.getApplicationContext();
    Log.d("SERVICE-SMS","CallMonitorService created");
    registerObserver();
  }

@Override
  public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
  }

  @Override
  public void onDestroy() {
    unregisterObserver();
    super.onDestroy();
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
  /**
   * Start the service to process that will run the content observer
   */
  public static void beginStartingService(Context context) {
   Log.d("SERVICE-SMS","CallMonitorService: beginStartingService()");
    context.startService(new Intent(context, CallService.class));
  }

  /**
   * Called back by the service when it has finished processing notifications,
   * releasing the wake lock if the service is now stopping.
   */
  public static void finishStartingService(Service service) {
    Log.d("SERVICE-SMS","CallMonitorService: finishStartingService()");
    service.stopSelf();
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After a lot of research and some attempts, Solved it! :)

Just added BroadcastReciever listening to package changes and updates:

register receiver in the manifest file:

<receiver android:name=".receivers.onRestartReciever">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <action android:name="android.intent.action.PACKAGE_RESTARTED" />
            <data android:scheme="package" android:path="my.Package.Path" />
        </intent-filter>
  • PACKAGE_REPLACED - called in particular to notify application update.
  • PACKAGE_RESTARTED - called when most memmory cleaners are cleaning memmory.
  • the "data" row is used to monitor action applied for the specific package.

Within this reciever I start my services again, and restarts the widget view (calling it's onUpdate() method in this case):

public class onRestartReciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.d("DEBUG", "onRestartReciever");

    // Register Services
    MyWidget_1x1.registerServices(context);
    MyWidget_2x2.registerServices(context);

    // reInitialize appWidgets
    AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(context);

    MyWidget_1x1 widget1x1=new CallWidgi();
    widget1x1.onUpdate
    (context,
         AppWidgetManager.getInstance(context),
            widget1x1.getIDs(context, appWidgetManager));

    MyWidget_2x2 widget2x2=new CallWidgi2();
    widget2x1.onUpdate(context,
                        AppWidgetManager.getInstance(context),
                            widget2x2.getIDs(context, appWidgetManager));
    }
}

registerServices(Context) is a static method in my AppWidgetProvider classes (MyWidget_1x1/MyWidget_2x2) which registers the needed services.

Hope it will help you too =]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...