嗨即时通讯我的 Android小部件有一个非常奇怪的问题,香港专业教育学院看起来很多地方,但我似乎无法弄清楚什么是错的.基本上我在我的小部件中调用pendingintent广播并成功地在onrecivie方
但是在onRecive方法中,当我尝试使用RemoteViews为我的组件设置文本时,文本不会更新,也不会调用任何错误.我在下面附上了我的代码,任何帮助都会很棒.
谢谢,
中号
package com.android.FirstWidget;import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageButton; import android.widget.RemoteViews; public class ExampleAppWidgetProvider extends AppWidgetProvider { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Log.e("ds",intent.getAction()); Log.e("f","f"); if(intent.getAction().contains("1")){ RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wid); views.setTextViewText(R.id.textView1,"heyheyhey"); Log.e("fssss","sssf"); } super.onReceive(context, intent); } public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; // Perform this loop procedure for each App Widget that belongs to this provider for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; // Create an Intent to launch ExampleActivity Intent intent = new Intent(context, ExampleAppWidgetProvider.class); intent.setAction("1"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); // Get the layout for the App Widget and attach an on-click listener // to the button RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wid); views.setOnClickPendingIntent(R.id.arrowLeft, pendingIntent); views.setOnClickPendingIntent(R.id.arrowRight, pendingIntent); //views.set // Tell the AppWidgetManager to perform an update on the current app widget appWidgetManager.updateAppWidget(appWidgetId, views); } } }你需要在onRecieve方法的末尾添加几行….它应该像这样..
package com.android.FirstWidget;import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageButton; import android.widget.RemoteViews; public class ExampleAppWidgetProvider extends AppWidgetProvider { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Log.e("ds",intent.getAction()); Log.e("f","f"); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wid); if(intent.getAction().contains("arrow_left")){ views.setTextViewText(R.id.textView1,"left"); Log.e("fssss","sssf"); } else if(intent.getAction().contains("arrow_right")){ views.setTextViewText(R.id.textView1,"right"); Log.e("fssss","sssf"); } else { super.onReceive(context, intent); } ComponentName componentName = new ComponentName(context, ExampleAppWidgetProvider.class); AppWidgetManager.getInstance(context).updateAppWidget(componentName, views); } public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; // Perform this loop procedure for each App Widget that belongs to this provider for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; // Create an Intent to launch ExampleActivity Intent intent = new Intent(context, ExampleAppWidgetProvider.class); intent.setAction("arrow_left"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); // Get the layout for the App Widget and attach an on-click listener // to the button RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wid); views.setOnClickPendingIntent(R.id.arrowLeft, pendingIntent); intent = new Intent(context, ExampleAppWidgetProvider.class); intent.setAction("arrow_right"); pendingIntent = PendingIntent.getBroadcast(context, 0, intent, views.setOnClickPendingIntent(R.id.arrowRight, pendingIntent); //views.set // Tell the AppWidgetManager to perform an update on the current app widget appWidgetManager.updateAppWidget(appWidgetId, views); } } }
此外,虽然我相信你编码的内容应该可以工作,但如果没有,你可以尝试在清单文件中设置按钮的intent操作.它应该是这样的
<intent-filter > <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> <action android:name="com.android.FirstWidget.arrow_left"/> <action android:name="com.android.FirstWidget.arrow_right"/> </intent-filter>
我想现在应该可以了.我自己偶然发现了这个问题,并在此找到了解决方案:Clickable widgets in android