我是Android应用程序的新手,因此有一些问题。 使用下面的代码,我正在尝试对ImageButton采取行动。重要的是要记住,这是一个 Widget ,而不是一个Activity。
我在网上搜索了在Widget中使用ImageButton的例子,发现的信息非常少。但是,这个工作示例似乎在Android的不同版本中存在问题。
现在,代码适用于Android的旧版本(2.1-update1),而不适用于新版手机(2.3.5)或平板电脑(3.2.1)
似乎API已经发生了一些变化,因为它已停止工作。 (我建议的可能性。虽然不确定。)
2.1-update1的LogCat
日志:
D / AAAAAA(3408):======= onReceive D / AAAAAA(3408):======= onUpdate在其他版本中看起来甚至函数onUpdate()和onReceive()都不起作用。
我的代码:(测试代码...... :))
请您查看代码并解释此行为。
public class A_Org_WidgetActivity extends AppWidgetProvider {
private static final String LOG_TAG = "AAAAAA";
Button btn;
TextView txt1;
ImageButton button;
boolean bIcon = true;
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.d(LOG_TAG, " ======= onUpdate");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.hor_bott_4x3);
Intent configIntent = new Intent(context, ClickOneActivity.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
Intent active = new Intent(context, A_Org_WidgetActivity.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("msg", "Message for Button 1");
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.ImageButton06, actionPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.ImageButton07, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
String msg = "null";
Log.d(LOG_TAG, " ======= onReceive");
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID );
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId } );
}
} else {
// check, if our Action was called
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
try {
msg = intent.getStringExtra("msg");
Log.d(LOG_TAG, " ======= msg = null");
} catch (NullPointerException e) {
Log.d(LOG_TAG, " Error msg: " + e);
}
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noty = new Notification(R.drawable.icon, "Button 1 clicked",
System.currentTimeMillis());
noty.setLatestEventInfo(context, "Notice", msg, contentIntent);
notificationManager.notify(1, noty);
}
super.onReceive(context, intent);
}
}
}
和...
我的清单 * .xml
<application android:icon="@drawable/red_circle" android:label="@string/app_name">
<receiver android:name="A_Org_WidgetActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
<!--
<action android:name="com.widget.bla-bla.A_Org_WidgetActivity.ACTION_WIDGET_RECEIVER" />
-->
<!-- Service to perform web API queries -->
<service android:name="A_Org_WidgetActivity$UpdateService" />
<!-- this activity will be called, when we fire our self created ACTION_WIDGET_CONFIGURE -->
<activity android:name="CommonActivity">
<intent-filter>
<action android:name="com.widget.bla-bla.A_Org_WidgetActivity.ACTION_WIDGET_CONFIGURE" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7"/>