小部件:onUpdate、onReceive - appWidgetManager - 在 v2.3.5 和 v3.2.1 中不起作用
我是 Android 应用程序的新手,因此有几个问题。 使用下面的代码,我尝试对 ImageButton 执行操作。重要的是要记住这是一个 Widget,而不是 Activity。
我在网上搜索了在 Widget 中使用 ImageButton 的示例,但发现的信息很少。然而,这个工作示例似乎在不同版本的 Android 上存在问题。
现在,该代码适用于旧版本(2.1-update1)的 Android,而不适用于新版本的手机(2.3.5)或平板电脑(3.2.1)
- 工作 -(2.1-update1,三星 5700)
- 不工作 -(2.3 .5,HTC Desire HD )
- 不工作 - (3.2.1,ASUS)
似乎 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"/>
I am new to Android applications and hence have a few questions.
Using the code below, I am trying action on ImageButton. And it would be important to remember that this is a Widget, not an Activity.
I searched online for examples on using ImageButton in a Widget and found very little information. However, this working example seems to have problems with different versions of Android.
Now, the code works on old version (2.1-update1) of Android and not in new version of phone(2.3.5) or on Tablet (3.2.1)
- Working - (2.1-update1, Samsung 5700)
- Not working - (2.3.5, HTC Desire HD )
- Not working - (3.2.1, ASUS)
It seems as if there have been some changes in the API due to which it has stopped working. (My suggestion of a possibility. Not sure about it though.)
LogCat of 2.1-update1
Log:
D/AAAAAA ( 3408): ======= onReceive
D/AAAAAA ( 3408): ======= onUpdate
Looks like in other versions even functions onUpdate() and onReceive() are not working.
My code: ( Test code... :) )
Could you please look at the code and explain this behaviour.
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);
}
}
}
And...
My manifest *.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"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论