“清除内存"后服务未重新启动+ appWidget 崩溃

编程入门 行业动态 更新时间:2024-10-28 21:15: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 崩溃

本文发布于:2023-11-27 02:45:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重新启动   内存   quot   appWidget

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!