Sal*_*lv0 5 eclipse android android-widget
我正在尝试调试AppWidget,但是我遇到了一个问题:D如果没有设置断点,那么小部件可以在没有ANR的情况下工作,并且命令Log.v可以完美地执行.然后我在方法的顶部放置了一个断点:
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");
}
Run Code Online (Sandbox Code Playgroud)
断点按预期停止执行,但随后进程终止.问题是断点(我猜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.
Run Code Online (Sandbox Code Playgroud)
这会导致调试停止.所以问题是:有一种方法来调试小部件而不会引起ANR?提前感谢您的答案
小智 2
你说得对。据此,如果您的 Widget 的 BroadcastReceiver 处理其 onReceive 方法的时间超过 10 秒,系统将杀死它。
从我的头顶上我可以看到两个解决方案:
或者你可以尝试以下操作。我不确定它会如何工作,但也许值得尝试这种方法。
使用您自己的活动作为小部件的 RemoteView 的主机。我做过这样的事情:
<?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>
Run Code Online (Sandbox Code Playgroud)
在此布局中,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);
}
}
Run Code Online (Sandbox Code Playgroud)
将您的小部件更新逻辑放入 updateWidget 方法中(或者仅使用假参数调用小部件的 BroadcastReceiver 的正确 onUpdate 方法),您可以通过单击 Activity 上的按钮来调试它。
再说一遍,我从未在真正的小部件上尝试过它,我只是想出了想法并尝试为其编写代码。自行承担使用风险 :)
我把我的快速而肮脏的项目放到了github上,以防你想尝试一下。它是一个Idea项目,但是将它导入到Eclipse中应该很容易。
归档时间: |
|
查看次数: |
1661 次 |
最近记录: |