Problems starting Activity from AppWidgetProvider

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problems starting Activity from AppWidgetProvider

Postby Sprucie » Thu Nov 26, 2009 6:48 am

I have a Widget that should launch an Activity I made (called MyActivity) when you click on it. It doesn't do that, however - when I click on it nothing happens. In an earlier version of this code I had the Widget launch the browser when you click on it, and that works fine. I just can't make it work with my own personal Activity.

I have a feeling that the problem has to do with me specifying actions and categories and such in the manifest (which is why I'm posting this question here), but I'm not certain.

Here are the relevant portions of my code. Also probably some irrelevant portions. =)

Widget.java (AppWidgetProvider):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void onReceive(Context context, Intent intent)
  3.  
  4.     {
  5.  
  6.         super.onReceive(context, intent);
  7.  
  8.         String action = intent.getAction();
  9.  
  10.         if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
  11.  
  12.         {
  13.  
  14.                 Toast.makeText(context, "onReceive()", Toast.LENGTH_SHORT).show();    
  15.  
  16.                 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
  17.  
  18.                        
  19.  
  20.         /*  
  21.  
  22.                         //The five lines below worked just fine for launching the browser.
  23.  
  24.                         Intent browsingIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.somethingawful.com"));
  25.  
  26.             browsingIntent.addCategory(Intent.CATEGORY_BROWSABLE);
  27.  
  28.             browsingIntent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
  29.  
  30.             PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, browsingIntent, 0);
  31.  
  32.             views.setOnClickPendingIntent(R.id.txt_input, pendingIntent);
  33.  
  34.           */
  35.  
  36.            
  37.  
  38.                         //This code here doesn't seem to work at all. No errors, but no Activity starts.
  39.  
  40.             Intent i = new Intent(context, MyActivity.class);
  41.  
  42.             //i.setAction(Intent.ACTION_MAIN);
  43.  
  44.             //i.setComponent(new ComponentName("com.spiffy", "com.spiffy.MyActivity"));
  45.  
  46.             PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
  47.  
  48.             views.setOnClickPendingIntent(R.id.txt_input, pendingIntent);
  49.  
  50.  
  51.  
  52.             AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
  53.  
  54.            
  55.  
  56.            //R.id.btn_go
  57.  
  58.         }
  59.  
  60.     }
  61.  
  62.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


MyActivity.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class MyActivity extends Activity {
  3.  
  4.  
  5.  
  6.         private EditText txtname;
  7.  
  8.         private Button btnclickme;
  9.  
  10.     /** Called when the activity is first created. */
  11.  
  12.     @Override
  13.  
  14.     public void onCreate(Bundle savedInstanceState) {
  15.  
  16.         super.onCreate(savedInstanceState);
  17.  
  18.         Toast.makeText(getBaseContext(), "MyActivity onCreate()", Toast.LENGTH_SHORT).show();
  19.  
  20.         setContentView(R.layout.myactivitylayout);
  21.  
  22.        
  23.  
  24.         txtname = (EditText) findViewById(R.id.txt_name);
  25.  
  26.         btnclickme = (Button) findViewById(R.id.btn_clickme);
  27.  
  28.        
  29.  
  30.         btnclickme.setOnClickListener(new View.OnClickListener()  {
  31.  
  32.                 public void onClick(View v)  {
  33.  
  34.                         String str = "http://www.google.com"
  35.  
  36.                         Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(str));
  37.  
  38.                                 startActivity(i);
  39.  
  40.                 }
  41.  
  42.         });
  43.  
  44.     }
  45.  
  46. }
  47.  
  48.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


AndroidManifest.xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.spiffy" android:versionCode="1" android:versionName="1.0">
  5.  
  6.     <application android:icon="@drawable/w" android:label="@string/app_name">        
  7.  
  8.         <activity android:name=".MyWidget" android:label="@string/app_name">
  9.  
  10.             <intent-filter>
  11.  
  12.                 <action android:name="android.intent.action.MAIN" />
  13.  
  14.                 <category android:name="android.intent.category.INFO" />
  15.  
  16.             </intent-filter>
  17.  
  18.             <intent-filter>
  19.  
  20.                         <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
  21.  
  22.                 </intent-filter>
  23.  
  24.         </activity>        
  25.  
  26.         <activity android:name=".Config">
  27.  
  28.                 <intent-filter>
  29.  
  30.                 <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
  31.  
  32.                 </intent-filter>
  33.  
  34.                 </activity>
  35.  
  36.                  <activity android:name="com.spiffy.MyActivity"
  37.  
  38.                  android:label="@string/app_name"
  39.  
  40.                  android:theme="@style/Theme.Transparent">
  41.  
  42.          </activity>
  43.  
  44.         <receiver android:name=".Widget" android:label="My Widget!">
  45.  
  46.             <intent-filter>
  47.  
  48.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  49.  
  50.             </intent-filter>
  51.  
  52.             <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" />
  53.  
  54.         </receiver>
  55.  
  56.         </application>
  57.  
  58.     <uses-sdk android:minSdkVersion="4" />
  59.  
  60. </manifest>
  61.  
  62.  
Parsed in 0.006 seconds, using GeSHi 1.0.8.4


I also have a configuration Activity, but I don't think that's related and so I won't post it.

If you have any ideas why MyActivity isn't starting, please let me know. Thanks! And happy Thanksgiving if you're somebody who celebrates it. =)
Sprucie
Freshman
Freshman
 
Posts: 5
Joined: Wed Nov 04, 2009 5:11 am

Top

Postby Sprucie » Sun Nov 29, 2009 9:02 pm

I've been doing some more troubleshooting but still without success. Here's the logcat output for when I click on the Widget. The first section is when the browser code is in (and it behaves as expected). The second section is when the MyActivity code is in (and it doesn't work).


---------------------------------------
Logcat output with the browser code:
---------------------------------------

D/Launcher( 632): dumping extras content=Bundle[{appWidgetId=3}]

I/ActivityManager( 583): Starting activity: Intent { act=android.intent.action.
VIEW cat=[android.intent.category.BROWSABLE] dat=http://www.google.com cmp=com.
android.browser/.BrowserActivity }

W/ActivityManager( 583): startActivity called from non-Activity context; forcin
g Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.intent.action.VIEW cat
=[android.intent.category.BROWSABLE] dat=http://www.google.com cmp=com.android.
browser/.BrowserActivity }

I/ActivityManager( 583): Start proc com.android.browser for activity com.androi
d.browser/.BrowserActivity: pid=748 uid=10012 gids={3003}

I/jdwp ( 748): received file descriptor 20 from ADB

D/ddm-heap( 748): Got feature list request

I/ActivityThread( 748): Publishing provider browser: com.android.browser.Browse
rProvider

E/ActivityThread( 748): Failed to find provider info for com.google.settings

W/ActivityManager( 583): Unable to start service Intent { cmp=com.google.androi
d.googleapps/.GoogleLoginService }: not found

W/InputManagerService( 583): Starting input on non-focused client com.android.i
nternal.view.IInputMethodClient$Stub$Proxy@437734a0 (uid=10001 pid=632)

I/ActivityManager( 583): Displayed activity com.android.browser/.BrowserActivit
y: 7994 ms (total 7994 ms)

I/ActivityManager( 583): Starting activity: Intent { act=android.intent.action.
VIEW cat=[android.intent.category.BROWSABLE] dat=http://www.google.com/ cmp=
com.android.browser/.BrowserActivity }

D/dalvikvm( 748): GC freed 3026 objects / 223144 bytes in 555ms

-------------------------------------------
Logcat output with the MyActivity code:
-------------------------------------------

D/Launcher( 632): dumping extras content=Bundle[{appWidgetId=4}]

I/ActivityManager( 583): Starting activity: Intent { act=android.appwidget.acti
on.APPWIDGET_UPDATE cmp=com.spiffy/.Widget (has extras) }

D/dalvikvm( 735): GC freed 266 objects / 11792 bytes in 248ms
Sprucie
Freshman
Freshman
 
Posts: 5
Joined: Wed Nov 04, 2009 5:11 am

Postby moritzz » Sun Nov 29, 2009 10:52 pm

I had that problem too and no one here could help. So after some more work and research I found a solution that works perfectly for me:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Intent clickIntent = new Intent();
  3.  
  4. clickIntent.setAction(action);
  5.  
  6. clickIntent.setData(Uri.parse(uri));
  7.  
  8. clickIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  9.  
  10.  
  11.  
  12. PendingIntent pendingIntentClick = PendingIntent.getActivity(context, 0, clickIntent, Inttent.FLAG_ACTIVITY_NEW_TASK);
  13.  
  14.  
  15.  
  16. views.setOnClickPendingIntent(R.id.button1, pendingIntentClick);
  17.  
  18.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

It's not adapted to your problem but it should give you a hint what to do. In your case the action should be Intent.ACTION_VIEW and uri can also be sthg like market://, which will then call the Market-Application while http:// calls the Browser.
Guess your main mistake is the missing Flag "Intent.FLAG_ACTIVITY_NEW_TASK".
moritzz
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sat Nov 07, 2009 9:20 pm

Postby i4nc4mp » Sat Jan 09, 2010 7:21 pm

I am having the exact same problem. I built the code from the examples and my problem is that nothing I want to happen will happen with the pendingintent. I've tried setting it to activities I have, various broadcasts, and service (mainly I want the service because my widget just tries to toggle something when clicked and that's what the service start command does, but I had tried making it launch a broadcast that I declared in the manifest with no results).

I'll report back as I try a few workarounds and fixes
Last edited by i4nc4mp on Sat Jan 09, 2010 10:19 pm, edited 1 time in total.
i4nc4mp
Developer
Developer
 
Posts: 39
Joined: Sat Dec 12, 2009 6:44 pm

Postby i4nc4mp » Sat Jan 09, 2010 8:48 pm

updated - posted solution below
Last edited by i4nc4mp on Sat Jan 09, 2010 10:25 pm, edited 1 time in total.
i4nc4mp
Developer
Developer
 
Posts: 39
Joined: Sat Dec 12, 2009 6:44 pm

Postby i4nc4mp » Sat Jan 09, 2010 10:16 pm

Solution:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.     public void onEnabled(Context context) {   
  5.  
  6.                 //Log.v("toggle_widget","Enabled is being called");
  7.  
  8.                        
  9.  
  10.                 AppWidgetManager mgr = AppWidgetManager.getInstance(context);
  11.  
  12.                 //retrieve a ref to the manager so we can pass a view update
  13.  
  14.                
  15.  
  16.                 Intent i = new Intent();
  17.  
  18.                 i.setClassName("yourdoman.yourpackage", "yourdomain.yourpackage.yourclass");
  19.  
  20.                 PendingIntent myPI = PendingIntent.getService(context, 0, i, 0);
  21.  
  22.                 //intent to start service
  23.  
  24.                
  25.  
  26.         // Get the layout for the App Widget
  27.  
  28.         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.togglelayout);
  29.  
  30.        
  31.  
  32.         //attach the click listener for the service start command intent
  33.  
  34.         views.setOnClickPendingIntent(R.id.toggleButton, myPI);
  35.  
  36.        
  37.  
  38.         //define the componenet for self
  39.  
  40.         ComponentName comp = new ComponentName(context.getPackageName(), ToggleWidget.class.getName());
  41.  
  42.        
  43.  
  44.         //tell the manager to update all instances of the toggle widget with the click listener
  45.  
  46.         mgr.updateAppWidget(comp, views);
  47.  
  48. }
  49.  
  50.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


To make this work, you also have to call the same thing in onUpdated. Reason is, that enabled isnt called when device is first booting, but updated is. as far as I've testing, the two redundant calls to it seems to cause no problems when all the code does is add the listener to the view. luckily onupdated already passes the reference to the manager in the params.
i4nc4mp
Developer
Developer
 
Posts: 39
Joined: Sat Dec 12, 2009 6:44 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests