Android appwidget turns other widget into the widget added & second widget wont work?

I am making an android app that has 2 widgets. I have 1 widget set up and working, and the second widget is recognized in the widget list & loads correctly, but:

If I already have widget 1 on the screen when I add widget 2, both widgets change to widget 2. If I have a widget 2 and a widget 1, and add a widget 2, they all change to widget 2. The same happens if I add widget 1 - they all change to widget 1.

Also, nothing happens when widget 2 is clicked, even though I have used pretty much the same code from widget 1.

Below are the relevant files:

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 (For widget 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 (for widget 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 (for widget 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 (for widget 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>

Can ANYONE tell me what the problem is? I am doing my nut in trying to figure it out :(

-------------Problems Reply------------

You havent made use of the onUpdate param int[] appWidgetIds.

You should loop through this, and apply any code to each one. At the moment you are not telling it which widget to update. Nor do you try and update more than the one widget.

Example below, note the loop on N. And the sub method also uses the id of the widget, which is passed from the loop on 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);
}

Category:android Views:4 Time:2012-04-21

Related post

  • onUpdate not being called in widget, even though I see the android.appwidget.action.APPWIDGET_UPDATE intent in onreceive 2010-07-14

    I'm working with the emulator using the debugger and I've noticed that onUpdate is not getting called. When I add the widget to the emulator homescreen I see my breakpoint being hit in the onReceive method. The onReceive method does get the android.a

  • Are Android AppWidgets restricted to particular sizes? 2010-09-17

    I'm making an Android AppWidget to sit on the home screen. The user's home screen space is precious, so I want the widget to be small. The ideal size would be 2x1 cells. Some documentation suggests you can have any size you like? But the AppWidget gr

  • Android AppWidget and App Single Signon 2010-09-20

    I have an App and an AppWidget associated with it. The AppWidget is updated by an Update Service. What I want to implement is that the AppWidget only starts getting updates only when the User has logged in, much like the Facebook/Twitter Application

  • Android: AppWidget with custom view not working 2010-10-31

    I am creating a appwidget that consists of a single custom view called Foo. xml/widget.xml: <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="72dp" android:updatePeriodMillis

  • Android AppWidget size 2011-03-07

    i am creating a few appwidgets on android. I would also like to resuse the layouts of the appwidgets so i can make controls in my activities so it looks the same. So that working fine the problem i have is the sizes I would like to get it the same si

  • Android - AppWidget AppWidgetManager.updateAppWidget() not being called instantly 2011-07-26

    I am developing an appwidget that uses the RemoteViews to display a ListView. For simplicity's sake, I will give an analogy of the appwidget's functionality: The user will select to add the appwidget to the home screen. Upon selecting the widget, a c

  • Android Appwidget textview not updating 2011-08-04

    Hi im having a very weird problem with my android widget, ive looked extensivly in many places but I cant seem to figure out whats wrong. basically im calling a pendingintent broadcast in my widget and in sucessfully catching that intent in the onrec

  • Android appwidget crashes process com.android.settings on android v2.1, need work around 2011-08-16

    I'm working on an android appwidget and it works fine in all android versions I tried (emulator 1.5, 1.6, 2.3), but not 2.1 (physical device, LG GT540 upgraded to 2.1). I tested it on a Samsung GT I5801 fresh out of the box with the 2.1 OS and it wor

  • App Widget does not appear in the widget list on Honeycomb devices until Launcher is restarted 2011-10-12

    Possible Duplicate: Android AppWidget does not show up in the menu in honeycomb until reboot It seems like the default launcher in Android 3 (Honeycomb) has a bug. It doesn't seem to refresh the widget list when you install a new application. When yo

  • Android AppWidget not receiving APPWIDGET_ENABLED intent 2011-11-19

    I'm trying to create my first AppWidget using the AlarmManager class so that I can update more frequently than every 30 minutes. I followed this tutorial as a basis for setting up my widget, but for some reason I cannot get the updates to begin prope

  • Android AppWidget - Not getting a result back from an activity 2012-01-12

    I've created an Android Widget. I have a problem of getting a result from an activity back to it's AppWidgetProvider parent. I looked for an answer over the web, did exactly what written in all the sites and it's not working. I put a break point in o

  • Customisation of android AppWidget launcher icon 2012-01-20

    I have created a AppWidget for android with 2x1 ratio. The launcher icon of the widget is compressed in the centre. By seeing the attached screen shot we can see the WiFi widget and WorldClock are compressed into centre where as YouTube is filled its

  • Android appwidget onUpdate() 2012-02-09

    I have a question with the onUpdate function of an android appwidget: I am updating the appwidget once a day with that function. Before this I used a TimerTask class to update the widget but Android sometime closes that TimerTask and my widget stops

  • Android AppWidget Not Displaying On Certain Phones 2012-03-30

    I have a really simple appwidget (two text views and a button). I've tested it on a Touchpad, Droid 1, and a Droid Razr. It works on everything except the Razr. When I add the widget to the homescreen it doesn't display; it's just invisible. If I hol

  • How can I update the UI for an android appwidget 2010-11-16

    I'm sure I'm missing something, but I'm just trying to get an app widget with a button and a counter. Every time I click the button I want the counter to update by 1. I've set the onUpdate() function of the WidgetProvider to register a pending event

  • Android AppWidget Configuration - Start in new task 2011-03-08

    I've searched and searched and searched for this! So I've got an app widget and it has a configuration activity that I can open when pressing a button on the app. The scenario is: Had the app opened. Closed the app with the home button. Selected to a

  • How to restrict only single instance of android appwidget 2011-06-07

    How can Irestrict users from creating a only single instance of my appwidget? --------------Solutions------------- In the configuration activity (which gets started after the widget was added) you can check if the user already added a widget (save a

  • How to Launch an Android AppWidget's configuration Activity from another activity? 2012-04-20

    Well this is driving me crazy. I have developed an App-widget. Everything is working fine. I have a configuration activity which launches every time a widget is added on the home screen and works beautiful. I save the user settings per widget id etc.

  • Adding custom widgets to QMenuBar 2010-10-12

    Qt3 used to provide QMenuBar::insertItem with QWidget* parameter. This way any custom widget could be added to menu bar - for example a clock-widget. In Qt4 there is no such overloaded method. What would be the best way to reach the same goal - addin

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.118 (s). 11 q(s)