0

I would like to open another installed app by button/imageButton click from my app widget provider. Here is my code,

 @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int count = appWidgetIds.length;

    for (int i = 0; i < count; i++) {
        int widgetId = appWidgetIds[i];
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.activity_main);


        Intent intent = new Intent(context, MainActivity.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.imageViewBus, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
}

Thanks in advance.

Rifan
  • 424
  • 6
  • 17
  • A specific app? If so, which app? Do you know the package name? – Mike M. Jan 30 '17 at 05:09
  • Yes a specific app. Yes I know the package name. – Rifan Jan 30 '17 at 05:21
  • You just need to use the `PackageManager#getLaunchIntentForPackage()` method to get the `Intent` you're giving to the `PendingIntent`, as shown in the linked duplicate – Mike M. Jan 30 '17 at 05:23
  • can you give me some reference or code, which I can add in my code ? – Rifan Jan 30 '17 at 05:32
  • Have a look at [this answer there](http://stackoverflow.com/a/31684505), as it's the closest fit to your situation. Change `"com.example.package"` to the package name for the app you want to open. Then, instead of calling `context.startActivity(launchIntent);`, use `PendingIntent.getActivity(context, 0, launchIntent, 0)` to get the `PendingIntent` for your `Button`, and use that with `remoteViews.setOnClickPendingIntent()` just like you are in your posted code. – Mike M. Jan 30 '17 at 05:38
  • Its doesn't work. Can you please check my project? [GitHub](https://github.com/rifanalam/AndroidWidget.git) – Rifan Jan 30 '17 at 06:07
  • You _don't_ want to call `context.startActivity(launchIntent)` there anywhere. Remove those calls. And notice my example uses `PendingIntent.getActivity()`, instead of `PendingIntent.getBroadcast()`. – Mike M. Jan 30 '17 at 06:15
  • yes `PendingIntent.getActivity()` is working. Sorry for bother you and thank you for your kind help. – Rifan Jan 30 '17 at 06:29
  • No worries. Glad you got it working. Cheers! – Mike M. Jan 30 '17 at 06:30

0 Answers0