2011-01-07 69 views
5

我試圖調試AppWidget但我在問題發生:d 如果沒有設置部件工作沒有ANR斷點和命令Log.v被完美執行。 然後我放置一個斷點上的方法的頂部:調試窗口小部件引起ANR

public void onReceive(Context context, Intent intent) { 
    Log.v(TAG, "onReceive 1"); // BP on this line 
    super.onReceive(context, intent); 
    String action = intent.getAction(); 

    // Checks on action and computations ... 

    Log.v(TAG, "onReceive 2"); 
    updateWidget(context); 
    Log.v(TAG, "onReceive 3"); 
} 

斷點停止執行如預期但隨後的進程死亡。 問題是斷點(我猜是xD)會導致ANR,ActivityManager會終止進程。這就是日誌:

01-07 14:32:38.886: ERROR/ActivityManager(72): ANR in com.salvo.wifiwidget 
01-07 14:32:38.886: INFO/Process(72): Sending signal. PID: 475 SIG: 9 
...... 
...... 
01-07 14:32:38.906: INFO/ActivityManager(72): Process com.salvo.wifiwidget (pid 475) has died. 

這會導致調試停止。 所以問題是:有一種方法來調試小部件而不會導致ANR? 在此先感謝您的答案

+0

嘿,你有沒有找到解決辦法呢? – span 2012-02-11 23:42:28

回答

2

你說得對。根據this,如果處理其onReceive方法所需的時間超過10秒,則Widget的BroadcastReceiver將被系統殺死。

從我的頭頂,我可以看到兩種解決方案:

  1. 從表現和調試在活動邏輯獨立的邏輯,然後將其納入窗口小部件。
  2. 去「舊好」的日誌記錄。

或者你可以嘗試以下的事情。我不確定它會如何工作,但也許有必要嘗試一下這種方法。

使用自己的活動爲主機部件的RemoteViews。我做了這樣的事情:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <LinearLayout 
      android:id="@+id/widget_view" 
      android:layout_width="fill_parent" 
      android:layout_height="200dp"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Inflate widget" 
     android:onClick="onInflateClick"/> 
    <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Update widget" 
      android:onClick="onUpdateClick"/> 
</LinearLayout> 

在使用id = widget_view此佈局的LinearLayout將播放窗口部件主機。活動代碼本身看起來是這樣的:

package com.example; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.RemoteViews; 

public class MyActivity extends Activity { 
    private LinearLayout widgetHolder; 
    private View widgetView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     widgetHolder = (LinearLayout) findViewById(R.id.widget_view); 
    } 

    public void onInflateClick(View v) { 
     RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget); 
     views.setImageViewResource(R.id.image, R.drawable.img1); 
     views.setTextViewText(R.id.text, "Widget created"); 
     widgetView = views.apply(this, null); 
     widgetHolder.addView(widgetView); 
    } 

    public void onUpdateClick(View v) { 
     onUpdateWidget(0); 
    } 

    public void onUpdateWidget(int widgetId) { 
     RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget); 
     views.setImageViewResource(R.id.image, R.drawable.img2); 
     views.setTextViewText(R.id.text, "Widget updated"); 

     // Tell the AppWidgetManager to perform an update on the current app widget 
     updateWidget(widgetId, views); 
    } 

    private void updateWidget(int widgetId, RemoteViews views) { 
     views.reapply(this, widgetView); 
     // appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 
} 

把你的小窗口更新邏輯到updateWidget方法(或只是調用控件的廣播接收器與假參數正確的onUpdate方法),你可以用一個按鈕上點擊調試它你活動。

再次,我從來沒有嘗試過真實部件,我只是想出了主意,試圖編寫代碼了。使用它在您自己的風險:)

我把我的快速和骯髒的項目github你想嘗試一下情況。它是和Idea項目,但它應該很容易導入到Eclipse中。

相關問題