从小部件启动 2 个意图时如何将应用程序置于最前面?

How to bring the app to front when starting 2 intents from a widget?

我有一个小部件按钮,按下它会启动 2 个意图。其中之一是拨打某个号码,第二个是打开我的应用程序。我得到的代码(见下文)在 Lollipop 上按预期工作。问题出在旧版本上,它会拨打号码并打开应用程序,但不会将其带到下面的 front.See 我的代码,在此先感谢您。

public class 小部件扩展 AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
        updateAppWidget(context, appWidgetManager, appWidgetIds[i]);


    }
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {

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

    //the intent that opens the app
    Intent openAppIntent = new Intent(context, SplashScreen.class);
   // openAppIntent.addFlags(Intent.);
    //dial in the conference
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:" + 123));

    Intent intents [] = {openAppIntent,callIntent};

    PendingIntent pendingIntent = PendingIntent.getActivities(context,0, intents,  PendingIntent.FLAG_UPDATE_CURRENT);

    views.setOnClickPendingIntent(R.id.widget_button,pendingIntent);




    appWidgetManager.updateAppWidget(appWidgetId,views);
}

}

我已经在 API 17 中用 Samsung [GT-S7582] 试过你的代码,它有效,你试过哪个旧版本?

但是,如果您查看有关 startActivities(Intent[]) 的文档,这是您使用 PendingIntent.getActivities 创建的悬垂意图,您会注意到一个非常重要的声明:

Note that unlike that approach, generally none of the activities except the last in the array will be created at this point, but rather will be created when the user first visits them (due to pressing back from the activity on top).

这意味着至少根据我的理解,您的调用意图可能不会执行,基本上 startActivies 可用于创建合成后退堆栈。

在任何情况下,只启动您的应用并传递 phone 号码,然后您的应用启动呼叫意图不是更好吗?

类似

Intent openAppIntent = new Intent(context, SplashScreen.class)
    .setData(Uri.parse("tel:" + 123));

在 SplashScreen 上

public void onCreate(...) {
    Uri phoneUri = getIntent().getData();

    if(phoneUri != null) {
      Intent callIntent = new Intent(Intent.ACTION_CALL)
        .setData(phoneUri);
      startActivity(callIntent); 
    }