views:

132

answers:

1

I have a widget class and a service class updating the widget.

I have added in the widget class in onUpdate() the following code:

  RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.countdownwidget);
  Intent Intent1 = new Intent(Intent.ACTION_MAIN);
  Intent1.addCategory(Intent.CATEGORY_LAUNCHER);
  PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, Intent1, 0);
  views.setOnClickPendingIntent(R.id.button1, pendingIntent);

  Intent Intent2 = new Intent(Intent.ACTION_MAIN);
  Intent2.addCategory(Intent.CATEGORY_LAUNCHER);
  PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, Intent2, 0);
  views.setOnClickPendingIntent(R.id.button2, pendingIntent2); 

And I have also added the following code in the widget service in the onStart()

    Intent Intent1 = new Intent(Intent.ACTION_MAIN);

    Intent1.addCategory(Intent.CATEGORY_LAUNCHER);
    PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 0, Intent1, 0);
    remoteView.setOnClickPendingIntent(R.id.button1, pendingIntent1);

    Intent Intent2 = new Intent(Intent.ACTION_MAIN);
    Intent2.addCategory(Intent.CATEGORY_LAUNCHER);
    PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 0, Intent2, 0);
    remoteView.setOnClickPendingIntent(R.id.button2, pendingIntent1);

The problem I am having is that once the implicit intent registers the app to launch on button1, the button2 is identical to button1. How can i make the 2 intents behave differently? i.e register and launch different apps.Its working with one button, but the other button launches the same thing of the first button.I have have been looking to get this to work for the last week, reading things all over but with no result. I would appreciate your help. Thanks.

A: 

The problem I am having is that once the implicit intent registers the app to launch on button1, the button2 is identical to button1.

Well, of course. All four of your Intents are identical: ACTION_MAIN, CATEGORY_LAUNCHER, with nothing else specified. I am surprised this Intent even works.

So, the first step to having the buttons do different things is to actually have different Intents.

Also, please do not use getApplicationContext(). Just use this, as a Service is a Context.

CommonsWare
How do i change my code to make the intents different please?
John
@John: I don't know your app. I do not know what the right Intents should be.
CommonsWare
I am trying to have 2 buttons that each button has an implicit intent, that would launch a picker dialog(as it does) and let the user pick the default action for each button.
John
@John: If by "pick the default action for each button", you are referring to the checkbox that appears on picker dialog, that has nothing to do with the button. The user is specifying the default for **anything on the system** that uses this `Intent` structure (`ACTION_MAIN`, `CATEGORY_LAUNCHER`, with nothing else specified).
CommonsWare
So in other words, there cannot exist 2 implicit intents to do different things on a widget.Yes button one when clicked, should present the user to pick what application they need launched(and set it as default actions for it) and so button two(choosing different app)I thought my code does include 2 pending intents listening on 2 different buttons.
John
@John: "there cannot exist 2 implicit intents to do different things on a widget" -- this has nothing to do with app widgets. You need to write more code, whereby *you* let the user choose from a `ListView` that *you* create and *you* populate and *you* store their selection and then *you* use their selection on *your* button to start the activity they chose. "I thought my code does include 2 pending intents listening on 2 different buttons" -- buttons have nothing to do with it.
CommonsWare
Ok, talking in abstract terms, is it possible to have 2 buttons on a widget(well this can be done) but when each button is clicked to present the user to choose which app to launch? I have seen the listview->apps list->onclick select->start activity,l but i thought an easier way was the one i posted above, which works but both buttons behave the same.
John