1

I want to combine my widget and one of my android application..

Is it possible to do that?

My plan is, once user open up my android widget, the widget will directly open up my android application?

Is there any way how?

I made some method here from my widget class:

public class ExampleAppWidgetProvider extends AppWidgetProvider {

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];

Intent intent = startActivity(new Intent("com.xxx.yyy.widget.FlamingoActivity"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);

RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget1);

views.setOnClickPendingIntent(R.id.Image, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

I tried to call the second app, FlamingoActivity, but its failing.. :(

NOT THE FULL SOLUTION:

I should change this line

Intent intent = startActivity(new Intent("com.xxx.yyy.widget.FlamingoActivity"));

to this:

Intent intent = new Intent(context, FlamingoActivity.class);
Nadirah Ibtisam
  • 67
  • 2
  • 11

2 Answers2

0

The activity that you are trying to call needs to be public (exported) in the manifest of its app. There is nothing special about starting activities from a widget, other than the fact that you use a PendingIntent to allow the widget to work as a part of your app (it runs inside the launcher app).

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • Yes. And apparently you were missing the call to `setOnClickPendingIntent()` which actually triggers it. – Nikolay Elenkov Dec 18 '12 at 05:48
  • Yeah,I think because of that, I tried to change the Intent line as I edited the solution, it became error. At first, the app works well, I can click the widget, an it open the second app which contain the splashscreen..and the app suddenly stops :( – Nadirah Ibtisam Dec 18 '12 at 06:06
0

You need to set an onClickpendingIntent on your widget

   Intent intent = new Intent(context, ExampleActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    // Get the layout for the App Widget and attach an on-click listener to the button
     RemoteViews views = new 
                 RemoteViews(context.getPackageName(),R.layout.appwidget_provider_layout);
      views.setOnClickPendingIntent(R.id.button, pendingIntent);

And also See this question Launching activity from widget

Community
  • 1
  • 1
Rahul
  • 10,457
  • 4
  • 35
  • 55
  • tq @rahul for the reply! really appreciate it, my mistake was this line Intent intent = startActivity(new Intent("com.xxx.yyy.widget.FlamingoActivity")); i should change to this : Intent intent = new Intent(context, FlamingoActivity.class); Yeah, the basic concept, I missed that.. :) thank u Rahul, I found this answer from the link u gave me! :D hey add me on facebook..im now looking for Android member.. :) Ibtisam Zahra (pic of cat) – Nadirah Ibtisam Dec 18 '12 at 05:49