我有一个应用程序小部件,可以onUpdate()在我的提供程序中接收click事件.
但是,当我尝试强制关闭主启动器时,单击事件将丢失.
我甚至把断点在所有的onEnabled(),onReceived()...等:连接似乎丢失.
因此,我如何"重新连接"按钮事件?
WidgetProvider扩展AppWidgetProvider:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.d(TAG, "onUpdate()");
Log.d(TAG, "isLoading: " + CONSTANT.isLoading);
// update each of the widgets with the remote adapter from updateService
// Get all ids
ComponentName thisWidget = new ComponentName(context, ScoreWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
// Build the intent to call the service
Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
// Update the widgets via the service
context.startService(intent); …Run Code Online (Sandbox Code Playgroud) 我通过AppWidgetManager.updateAppWidget手动更新我的小部件时遇到了问题.平台是Android 2.2.
这是代码:
我在Manifest中另外声明了一个现有的Activity:
<receiver android:name=".Widget" 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" />
</receiver>
Run Code Online (Sandbox Code Playgroud)
widget小类在Widget.java中声明:
public class Widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int use_static_ip;
RemoteViews remoteViews;
try {
use_static_ip = Settings.System.getInt(context.getContentResolver(), Settings.System.WIFI_USE_STATIC_IP);
if (use_static_ip == 0) { //DHCP
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_dhcp);
} else { //static IP
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_static);
}
Intent call_activity = new Intent(context, StaticIPToggle.class);
PendingIntent pending_call_activity = PendingIntent.getActivity(context, 0, call_activity, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_icon, …Run Code Online (Sandbox Code Playgroud)