“清除内存"后服务未重新启动+ appWidget 崩溃 [英] Service does not restart after "Clear Memory" + appWidget crashes

查看:25
本文介绍了“清除内存"后服务未重新启动+ appWidget 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个 appWidget,它在他的 onEnabled() 方法上注册了一些服务.

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

问题是,在我使用内置的任务管理器的 Clean Memmory/Ram 后,appWidget 视图崩溃(所有 appWidgets TextView 的文本都设置为默认值(TextView))并且服务停止运行并且永远不会重新启动.

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.

所以最后,我的问题是:有没有办法告诉 android 系统重新启动这些服务?因为我通过市场下载的其他 appWidgets 在此过程后似乎继续正常工作.

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.

会很高兴有想法和解决方案!谢谢先进,Gal :)

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

我使用的一些代码:

appWidget 中的 onEnabled() 方法:

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

推荐答案

经过大量研究和尝试,解决了!:)

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

刚刚添加了 BroadcastReciever 监听包的变化和更新:

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 - 专门调用以通知应用程序更新.
  • PACKAGE_RESTARTED - 在大多数内存清理器清理内存时调用.
  • 数据"行用于监控针对特定包应用的操作.
  • 在这个接收器中,我再次启动我的服务,并重新启动小部件视图(在这种情况下调用它的 onUpdate() 方法):

    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) 是我的 AppWidgetProvider 类 (MyWidget_1x1/MyWidget_2x2) 中注册所需服务的静态方法.

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

    希望对你也有帮助=]

    这篇关于“清除内存"后服务未重新启动+ appWidget 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆