Launch intent from AppWidget

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

Launch intent from AppWidget

Postby Scoobler » Tue Dec 15, 2009 9:39 pm

I have created a widget for my app, basically the idea is, when the widget is clicked an intent is started within my app. It is working so far but with a problem.

Basically when I click the widget for the first time everything is fine. I get the two expected peices of toast.

Then when I click the widget again, all it does is open the intent where it was, it doesn't start a fresh copy. Thus, the action which is passed to the intent, which is picked up during the onCreate isn't ever picked up. So I only get one peice of toast (from the widget) and the app is shown, but the app's peice of toast doesnt appear.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyWidget extends AppWidgetProvider
  2.  
  3. {
  4.  
  5.      @Override
  6.  
  7.      public void onReceive(Context context, Intent intent)
  8.  
  9.      {
  10.  
  11.           super.onReceive(context, intent);
  12.  
  13.           if(intent.getAction().equals("com.myapp.helloworld.MyWidget.CLICK"))
  14.  
  15.           {
  16.  
  17.  
  18.  
  19.                Toast.makeText(context, "You clicked my widget!!!", Toast.LENGTH_LONG).show();
  20.  
  21.  
  22.  
  23.                    Intent i = new Intent(context, MyWorld.class);
  24.  
  25.                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  26.  
  27.                    i.setAction("widget");
  28.  
  29.                    context.startActivity(i);  
  30.  
  31.           }
  32.  
  33.      }
  34.  
  35. ..........
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


In MyWorld.clss I use the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.        
  2.  
  3.  
  4.  
  5.     @Override
  6.  
  7.     public void onCreate(Bundle savedInstanceState)
  8.  
  9.     {
  10.  
  11.         super.onCreate(savedInstanceState);    
  12.  
  13.                 // Assign the XML file to use:
  14.  
  15.                 setContentView(R.layout.my_world_menu);
  16.  
  17.  
  18.  
  19.         /**
  20.  
  21.                  * Handle widget click:
  22.  
  23.                  */
  24.  
  25.                 // Get the action which was passed
  26.  
  27.                 String intentAction = getIntent().getAction()==null?"nothing":getIntent().getAction();
  28.  
  29.         if(intentAction.compareTo("widget")==0)
  30.  
  31.         {
  32.  
  33.                         Toast.makeText(this, "Widget Click Received", Toast.LENGTH_LONG).show();
  34.  
  35.         }
  36.  
  37. .......................
  38.  
  39.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



Can anyone tell me what I am doing wrong here?
Scoobler
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Jun 20, 2009 12:05 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests