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

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

问题描述

我已经建立了一个appWidget这对他的onEnabled()方法注册某些服务。

现在的问题是,在我使用内置的任务管理器的清洁Memmory /拉姆,在appWidget观点崩溃(该appWidgets TextView中的所有文本设置为默认(TextView的))和服务停止运行,并不会重新启动。

这只会发生在一段时间后小部件设置,如果我清洁Memmory /拉姆小部件之后设置错误一点儿也不发生,但我想这是关系到清洁的RAM的任务管理器的方法。

所以最后,我的问题是:有没有办法告诉了Android系统重新启动这些服务?其他appWidgets我已经通过了市场下载的似乎继续执行此过程后,工作正常。

将竭诚为理念和解决方案!由于先进的,加:)

一些code,我使用:

在appWidget的onEnabled()方法:

  @覆盖
公共无效onEnabled(上下文的背景下){
    super.onEnabled(上下文);

    意图newinIntent =新的意图(背景下,CallService_1x1.class);
        context.startService(newinIntent);

    newinIntent =新的意图(背景下,SmsService_1x1.class);
        context.startService(newinIntent);
}
 

从服务之一某些方法(其它服务都非常相似,因为这是他们的抽象方法):

  @覆盖
  公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
      //我们希望这项服务继续运行,直到它被明确
      //停止,因此返回粘​​。
      Log.d(服务短信,CallMonitorService  -  onStartCommand创建);
      返回START_STICKY;
  }

@覆盖
  公共无效的onCreate(){
    super.onCreate();
    上下文= this.getApplicationContext();
    Log.d(服务短信,CallMonitorService创建);
    registerObserver();
  }

@覆盖
  公共无效ONSTART(意向意图,诠释startId){
    super.onStart(意向,startId);
  }

  @覆盖
  公共无效的onDestroy(){
    unregisterObserver();
    super.onDestroy();
  }

  @覆盖
  公众的IBinder onBind(意向意图){
    返回null;
  }
  / **
   *启动服务程序将运行内容观察员
   * /
  公共静态无效beginStartingService(上下文的背景下){
   Log.d(服务短信,CallMonitorService:beginStartingService());
    context.startService(新意图(背景下,CallService.class));
  }

  / **
   *回调的服务时,它已经完成处理的通知,
   *解除锁定之后,如果该服务被停止,现在。
   * /
  公共静态无效finishStartingService(Service服务){
    Log.d(服务短信,CallMonitorService:finishStartingService());
    service.stopSelf();
  }
 

解决方案

了大量的研究和一些尝试后,解决了这个问题! :)

刚刚加入BroadcastReciever听包装的更改和更新:

在清单文件寄存器接收器:

 <接收机器人:名称=。receivers.onRestartReciever>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.PACKAGE_REPLACED/>
            <作用机器人:名称=android.intent.action.PACKAGE_RESTARTED/>
            <数据机器人:计划=包机器人:路径=my.Package.Path/>
        &所述; /意图滤光器>
 

  • PACKAGE_REPLACED - 所谓的特别通知应用程序更新
  • PACKAGE_RESTARTED - 当大多数memmory清洁工正在打扫memmory名为
  • 数据行用于监视所申请的具体包的动作。

在这个rec​​iever我重新开始我的服务,并重新启动了小部件视图(调用它的OnUpdate()方法,在这种情况下):

  onRestartReciever延伸的BroadcastReceiver {public类

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    // TODO自动生成方法存根
    Log.d(调试,onRestartReciever);

    //注册服务
    MyWidget_1x1.registerServices(上下文);
    MyWidget_2x2.registerServices(上下文);

    //初始化appWidgets
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(上下文);

    MyWidget_1x1 widget1x1 =新CallWidgi();
    widget1x1.onUpdate
    (背景下,
         AppWidgetManager.getInstance(上下文),
            widget1x1.getIDs(上下文,appWidgetManager));

    MyWidget_2x2 widget2x2 =新CallWidgi2()​​;
    widget2x1.onUpdate(背景下,
                        AppWidgetManager.getInstance(上下文),
                            widget2x2.getIDs(上下文,appWidgetManager));
    }
}
 

registerServices(上下文)是我AppWidgetProvider类的静态方法(MyWidget_1x1 / MyWidget_2x2)的注册所需要的服务。

希望这将有助于你太=]

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

解决方案

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 =]

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

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