2011-10-22 98 views
3

我已經構建了一個appWidget,它在onEnabled()方法上註冊了一些服務。「清除內存」+ appWidget崩潰後服務不會重新啓動

問題是,在我使用內置任務管理器的Clean Memmory/Ram後,appWidget視圖崩潰(所有appWidgets TextView的文本都設置爲默認值(TextView)),服務停止運行並且從不重新啓動。

這隻會發生在一段時間後的小部件設置,如果我清潔Memmory/Ram的小部件設置後立即錯誤沒有發生,但我想這是有關任務管理器的清理RAM的方法。

最後,我的問題是:有沒有辦法告訴android系統重新啓動這些服務?作爲我通過市場下載的其他appWidgets似乎在此過程之後繼續正常工作。

會很高興的想法和解決方案!由於先進的,加:)

一些代碼,我使用:

在appWidget的onEnabled()方法:

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

從服務的一個有些方法(其它服務內容有非常相似這是從他們的抽象方法):

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

回答

9

經過大量的研究和一些嘗試,解決了它! :)

只需添加BroadcastReciever聽包裝的變化和更新:

登記接收清單文件:

<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 - 當大多數存儲器清潔工正在清理存儲器時調用。
  • 「data」行用於監視爲特定程序包應用的操作。

在這個rec​​iever我要重新開始我的服務,並重新啓動小工具視圖(調用它在這種情況下的onUpdate()方法):

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(上下文)是我的AppWidgetProvider靜態方法類(MyWidget_1x1/MyWidget_2x2),用於註冊所需的服務。

希望它也能幫到你=]

+0

不工作,內存清理後Widget凍結... – Monzur

0

感謝您成功研究您自己的解決方案。你指出我正確的方向。我遇到類似的問題,appwidget在重新啓動或重新啓動後不再更新,也不會響應按鈕點擊。

我所做的第一次添加如下清單:

<receiver android:name=".BootReceiver"> 
    <intent-filter> 
    <action android:name="android.intent.action.BOOT_COMPLETED"/> 
    </intent-filter> 
</receiver> 

然後,我創建了一個BootReceiver.java文件,類似於下面的東西。在我的情況下,我不得不重新創建按鈕的警報和未決意圖。

public class BootReceiver extends BroadcastReceiver 
{ 
    /* 
    * after reboot widget appears to stop working and becomes unresponsive to clicks 
    * this broadcast receiver will create new alarm and refresh pending intents 
    */ 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
    /* do your stuff here, mostly just copy&paste from other places */ 
    } 
} 

有些功勞,這也去:Proximity Alerts not working after phone reboot

1

上面的東西確實趕上重啓事件,但未能趕上清晰記憶的事件,因爲android.intent.action.PACKAGE_RESTARTED不會被髮送到包本身。

我通過添加服務解決了這個問題。這個服務獲得了一個明確的內存事件調用的onCreate()。請注意,您必須使用從AppWidgetProvider調用的startService調用來啓動服務。

Intent i= new Intent(context, CTService.class); 
// potentially add data to the intent 
i.putExtra("KEY1", "Value to be used by the service"); 
ComponentName name = context.startService(i); 

服務代碼是這樣的:

@Override 
public void onCreate() 
{ 
    Log.d(TAG,"onCreate"); 
    ping(); 
} 
@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.d(TAG,"onStartCommand: "+intent.getAction()); 
    //TODO do something useful 
    ping(); 
    return Service.START_STICKY; 
    } 

ping實際的東西,這使得對小部件的觸摸事件的工作。

相關問題