How to launch activity from android home screen widget

strange quark picture strange quark · Apr 25, 2010 · Viewed 10.5k times · Source

I am desperately trying to get my head wrapped around how to implement home screen widgets. Right now, I (finally) was able to get a button on my widget respond to a button press setting up an intent filter in the manifest.

However, I cannot for the life of me figure out how to launch an activity when the button is pressed. Basically, here's the code i have:

 @Override 
 public void onReceive(Context context, Intent intent) 
 { 
      super.onReceive(context, intent); 
      if(intent.getAction().equals("com.bic.search.searchWidget.CLICK")) 
      { 
           Toast.makeText(context, "It works!!", Toast.LENGTH_SHORT).show(); 
      } 
 } 

What I really want to do, though, is start a new activity, not display a toast message. I know it has something to do with pending intents, but I can't figure out how to get that to work.

Any help and sample code would be appreciated. Thanks a ton to whoever answers this!

Answer

CommonsWare picture CommonsWare · Apr 25, 2010

Well, your app widget should already have a PendingIntent that you tied to the button. Instead of a PendingIntent that triggers a BroadcastReceiver, have it be a PendingIntent that starts up an Activity.