gpt4 book ai didi

Android appwidget 将其他小部件变成添加的小部件,而第二个小部件不起作用?

转载 作者:行者123 更新时间:2023-11-29 22:08:13 26 4
gpt4 key购买 nike

我正在制作一个包含 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>

谁能告诉我问题出在哪里?我正在竭尽全力想弄清楚:(

最佳答案

您还没有使用 onUpdate 参数 int[] appWidgetIds

你应该遍历它,并对每个应用任何代码。目前您没有告诉它要更新哪个小部件。您也不会尝试更新多个小部件。

下面的示例,注意 N 上的循环。并​​且 sub 方法还使用小部件的 id,它是从 onUpdate 上的循环传递的。

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;

Log.d(LOG_TAG, "Updating Example Widgets.");

// 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, WidgetExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(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.widget1);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app
// widget
appWidgetManager.updateAppWidget(appWidgetId, views);


// Update The clock label using a shared method
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}

public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
String currentTime = df.format(new Date());

RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget1);
updateViews.setTextViewText(R.id.widget1label, currentTime);
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}

关于Android appwidget 将其他小部件变成添加的小部件,而第二个小部件不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10261065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com