我开发了一个简单的小部件,它有两个按钮可以打开和关闭我的双卡手机SIM卡。我在我的AppWidgetProvider实现的onUpdate方法中点击按钮。在几次成功点击后,发射器重新加载(我在设备屏幕上观察到短暂闪烁)并且我的小部件在按钮点击时不再执行任何操作。
我试着查看我的LogCat,但没有找到任何有趣的发射器应用程序。
现在的问题是如何调试这种情况?
编辑: 这是我的AppWidgetProvider的一些代码。我知道代码是天真的,需要一些重构,但我仍然需要以某种方式调试该启动器崩溃:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews;
ComponentName widget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
widget = new ComponentName(context, ExampleAppWidgetProvider.class);
int currentState = Settings.System.getInt(context.getContentResolver(), "dual_sim_mode_setting", -1);
String sim1Text = currentState == 1 || currentState == 3 ? "on" : "off";
String sim2Text = currentState == 2 || currentState == 3 ? "on" : "off";
remoteViews.setTextViewText(R.id.first_button, sim1Text);
remoteViews.setTextViewText(R.id.second_button, sim2Text);
remoteViews.setOnClickPendingIntent(R.id.first_button, getPendingSelfIntent(context, SIM1_CLICKED));
remoteViews.setOnClickPendingIntent(R.id.second_button, getPendingSelfIntent(context, SIM2_CLICKED));
appWidgetManager.updateAppWidget(widget, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if ("android.appwidget.action.APPWIDGET_UPDATE".equals(intent.getAction()))
{
super.onReceive(context, intent);
return;
}
if (SIM1_CLICKED.equals(intent.getAction())) {
switchSim(1, context);
return;
}
else if (SIM2_CLICKED.equals(intent.getAction())){
switchSim(2, context);
}
else if ("android.intent.action.DUAL_SIM_MODE".equals(intent.getAction())){
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
watchWidget = new ComponentName(context, ExampleAppWidgetProvider.class);
int mode = intent.getIntExtra("mode", -1);
switch (mode)
{
case 0:
remoteViews.setTextViewText(R.id.first_button, "off");
remoteViews.setTextViewText(R.id.second_button, "off");
break;
case 1:
remoteViews.setTextViewText(R.id.first_button, "on");
remoteViews.setTextViewText(R.id.second_button, "off");
break;
case 2:
remoteViews.setTextViewText(R.id.first_button, "off");
remoteViews.setTextViewText(R.id.second_button, "on");
break;
case 3:
remoteViews.setTextViewText(R.id.first_button, "on");
remoteViews.setTextViewText(R.id.second_button, "on");
break;
}
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
}
答案 0 :(得分:1)
嗯,因为你没有发布我只能猜测的任何代码,但我认为你实现了View.OnClickListener
。我只想做以下事情:
声明用于调试的标记:
final String tag = "#OnClick";
记录信息:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(tag, "I've been clicked!") // or even add useful information.
// code ..
}
});
这样你至少可以确定是否仍然会在按钮上调用OnClickListener
。
为了查看消息,您可以像这样添加过滤器到logCat:
至少这是LogCat在Eclipse中的工作方式,我不确定其他API,但可能有类似的东西。