Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch activity from android home screen widget

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!

like image 584
strange quark Avatar asked Apr 24 '10 22:04

strange quark


People also ask

What is special activity in Android?

An activity represents a single screen with a user interface just like window or frame of Java. Android activity is the subclass of ContextThemeWrapper class. If you have worked with C, C++ or Java programming language then you must have seen that your program starts from main() function.


1 Answers

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.

like image 147
CommonsWare Avatar answered Sep 21 '22 01:09

CommonsWare