我正在制作一个有 2 个小部件的 android 应用程序。我有 1 个小部件设置和工作,第二个小部件在小部件列表中被识别并正确加载,但是:
如果我在添加小部件 2 时屏幕上已经有小部件 1,则两个小部件都会更改为小部件 2。如果我有一个小部件 2 和一个小部件 1,并添加一个小部件 2,它们都会更改为小部件 2。如果我添加了小部件 1 - 它们都更改为小部件 1。
此外,单击小部件 2 时没有任何反应,即使我使用了与小部件 1 几乎相同的代码。
以下是相关文件:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.liamwli.reboot_widget"
android:installLocation="internalOnly"
android:versionCode="3"
android:versionName="1.1" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.REBOOT" />
<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
<application
android:allowClearUserData="false"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".reboot_widget_activeity"
android:excludeFromRecents="true"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Reboot_rec"
android:icon="@drawable/icon"
android:label="Reboot Recovery Widget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.liamwli.reboot_widget.Reboot_rec.ACTION_WIDGET_CLICK_RECEIVER" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/rebr_widget" />
</receiver>
<service android:name=".Reboot_rec$ToggleService" />
<receiver
android:name=".Widget"
android:icon="@drawable/icon"
android:label="Reboot Widget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.liamwli.reboot_widget.Widget.ACTION_WIDGET_CLICK_RECEIVER" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widgetstuff" />
</receiver>
<service android:name=".Widget$ToggleService" />
<activity
android:name=".Reboot"
android:excludeFromRecents="true"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.liamwli.reboot_widget.REBOOT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Reboot_rec"
android:excludeFromRecents="true"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.liamwli.reboot_widget.REBOOT_REC" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".hidprefs"
android:excludeFromRecents="true"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.liamwli.reboot_widget.HIDPREFS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Widget.java(对于小部件 1):
package com.liamwli.reboot_widget;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class Widget extends AppWidgetProvider {
public static String ACTION_WIDGET_CLICK_RECEIVER = "ActionReceiverWidget";
public static int appid[];
public static RemoteViews rview;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
updateWidgetState(context, "");
}
@Override
public void onReceive(Context paramContext, Intent paramIntent) {
String str = paramIntent.getAction();
if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_RECEIVER)) {
updateWidgetState(paramContext, str);
} else {
if ("android.appwidget.action.APPWIDGET_DELETED".equals(str)) {
int i = paramIntent.getExtras().getInt("appWidgetId", 0);
if (i == 0) {
} else {
int[] arrayOfInt = new int[1];
arrayOfInt[0] = i;
onDeleted(paramContext, arrayOfInt);
}
}
super.onReceive(paramContext, paramIntent);
}
}
static void updateWidgetState(Context paramContext, String paramString) {
RemoteViews localRemoteViews = buildUpdate(paramContext, paramString);
ComponentName localComponentName = new ComponentName(paramContext,
Widget.class);
AppWidgetManager.getInstance(paramContext).updateAppWidget(
localComponentName, localRemoteViews);
}
private static RemoteViews buildUpdate(Context paramContext, String paramString)
{
// Toast.makeText(paramContext, "buildUpdate() ::"+paramString, Toast.LENGTH_SHORT).show();
rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layout);
Intent active = new Intent(paramContext, Widget.class);
active.setAction(ACTION_WIDGET_CLICK_RECEIVER);
PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, active, 0);
// upadte this R.id.buttonus1 with your layout or image id on which click you want to start Activity
Intent configIntent = new Intent(paramContext, Reboot.class);
configIntent.setAction((ACTION_WIDGET_CLICK_RECEIVER));
PendingIntent configPendingIntent1 = PendingIntent.getActivity(paramContext, 0, configIntent, 0);
rview.setOnClickPendingIntent(R.id.bRB, configPendingIntent1);
if(paramString.equals(ACTION_WIDGET_CLICK_RECEIVER))
{
}
return rview;
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
// Toast.makeText(context, "onEnabled() ", Toast.LENGTH_SHORT).show();
}
// Called each time an instance of the App Widget is removed from the host
@Override
public void onDeleted(Context context, int[] appWidgetId) {
super.onDeleted(context, appWidgetId);
// Toast.makeText(context, "onDeleted() ", Toast.LENGTH_SHORT).show();
}
// Called when last instance of App Widget is deleted from the App Widget
// host.
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
// Toast.makeText(context, "onDisabled() ", Toast.LENGTH_SHORT).show();
}
}
Reboot_rec.java(用于小部件 2):
package com.liamwli.reboot_widget;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class Reboot_rec extends AppWidgetProvider {
public static String ACTION_WIDGET_CLICK_RECEIVER = "ActionReceiverWidget";
public static int appid[];
public static RemoteViews rview;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
updateWidgetState(context, "");
}
@Override
public void onReceive(Context paramContext, Intent paramIntent) {
String str = paramIntent.getAction();
if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_RECEIVER)) {
updateWidgetState(paramContext, str);
} else {
if ("android.appwidget.action.APPWIDGET_DELETED".equals(str)) {
int i = paramIntent.getExtras().getInt("appWidgetId", 0);
if (i == 0) {
} else {
int[] arrayOfInt = new int[1];
arrayOfInt[0] = i;
onDeleted(paramContext, arrayOfInt);
}
}
super.onReceive(paramContext, paramIntent);
}
}
static void updateWidgetState(Context paramContext, String paramString) {
RemoteViews localRemoteViews = buildUpdate(paramContext, paramString);
ComponentName localComponentName = new ComponentName(paramContext,
Widget.class);
AppWidgetManager.getInstance(paramContext).updateAppWidget(
localComponentName, localRemoteViews);
}
private static RemoteViews buildUpdate(Context paramContext, String paramString)
{
// Toast.makeText(paramContext, "buildUpdate() ::"+paramString, Toast.LENGTH_SHORT).show();
rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layout_rec);
Intent active = new Intent(paramContext, Reboot_rec.class);
active.setAction(ACTION_WIDGET_CLICK_RECEIVER);
PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, active, 0);
// upadte this R.id.buttonus1 with your layout or image id on which click you want to start Activity
Intent configIntent = new Intent(paramContext, Reboot_rec_ac.class);
configIntent.setAction((ACTION_WIDGET_CLICK_RECEIVER));
PendingIntent configPendingIntent1 = PendingIntent.getActivity(paramContext, 0, configIntent, 0);
rview.setOnClickPendingIntent(R.id.bRBR, configPendingIntent1);
if(paramString.equals(ACTION_WIDGET_CLICK_RECEIVER))
{
}
return rview;
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
// Toast.makeText(context, "onEnabled() ", Toast.LENGTH_SHORT).show();
}
// Called each time an instance of the App Widget is removed from the host
@Override
public void onDeleted(Context context, int[] appWidgetId) {
super.onDeleted(context, appWidgetId);
// Toast.makeText(context, "onDeleted() ", Toast.LENGTH_SHORT).show();
}
// Called when last instance of App Widget is deleted from the App Widget
// host.
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
// Toast.makeText(context, "onDisabled() ", Toast.LENGTH_SHORT).show();
}
}
Rebr_widget.xml(用于小部件 2):
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minHeight="40dp" android:minWidth="40dp" android:initialLayout="@layout/widget_layout_rec">
</appwidget-provider>
widgetstuff.xml(用于小部件 1):
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minHeight="40dp" android:minWidth="40dp" android:initialLayout="@layout/widget_layout">
</appwidget-provider>
任何人都可以告诉我问题是什么吗?我正在努力解决这个问题:(