【问题标题】:launch app with specific activity from background从后台启动具有特定活动的应用程序
【发布时间】:2017-04-05 05:10:52
【问题描述】:

我想在应用程序处于后台时从广播接收器打开一个非启动器活动。我尝试使用意图,但仍然只打开启动器页面。有没有办法打开任何其他不是启动器活动的活动并仍然打开整个应用程序?在下面的代码中,当收到来电时,我正在检查应用程序是否在后台,以及它是否正在发送打开应用程序的意图。

public class CallStateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        final String action = intent.getAction();

        if(NgnInviteEventArgs.ACTION_INVITE_EVENT.equals(action)){
            NgnInviteEventArgs args =
                    intent.getParcelableExtra(NgnEventArgs.EXTRA_EMBEDDED);
            if(args == null){
                Log.d("DEBUG", "Invalid event args");
                return;
            }

            NgnAVSession avSession = NgnAVSession.getSession(args.getSessionId());
            if (avSession == null) {
                return;
            }

            final NgnInviteSession.InviteState callState = avSession.getState();
            NgnEngine mEngine = NgnEngine.getInstance();

            switch(callState){
                case NONE:
                default:
                    break;
                case INCOMING:
                    Log.i("DEBUG", "Incoming call");


                    if (isAppForground(context)) {

                        context.startActivity(new Intent(context,CallScreen.class));
                      mEngine.getSoundService().startRingTone();
                   } else {
                        System.out.println("sammy_isINbackground");
                        // when App is in Background
                        Intent it = new Intent();
                        it.setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName()));
                        it.putExtra("from", "receiver");
                        intent.putExtra("incomingSessionID", avSession.getId());
                        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(it);

                        Intent in = new Intent("android.intent.category.DEFAULT");
                        in.setClassName("nits_33.sipcall.CallStateReceiver", "nits_33.sipcall.CallScreen");
                        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(in);
                       mEngine.getSoundService().startRingTone();
                    }
                    //for Ringtone
                    mEngine.getSoundService().startRingTone();
                    break;
                case INCALL:
                    Log.i("DEBUG", "Call connected");
                    mEngine.getSoundService().stopRingTone();
                    break;
                case TERMINATED:
                    Log.i("DEBUG", "Call terminated");
                    mEngine.getSoundService().stopRingTone();
                    mEngine.getSoundService().stopRingBackTone();
                    break;
            }
        }
    }



    public boolean isAppForground(Context mContext) {

        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(mContext.getPackageName())) {
                return false;
            }
        }

        return true;
    }
}

【问题讨论】:

  • 我认为您应该提供要打开的活动的类名,而不是 MainActivity。 intent.setComponent(new ComponentName(this.getPackageName(),ActivityToBeOpened.class.getName()));
  • Intent 应该是Intent in = new Intent("android.intent.category.DEFAULT"); 还是Intent in = new Intent();? @AkashBisariya
  • 应该是Intent in = new Intent();
  • 它使用 Intent in = new Intent("android.intent.category.DEFAULT")
  • 这取决于您的要求,它也应该适用于后者。

标签: android android-intent android-broadcastreceiver


【解决方案1】:
 Intent in = new Intent("android.intent.category.DEFAULT");
                            in.putExtra("incomingSessionID", avSession.getId());
                            in.setComponent(new ComponentName(context.getPackageName(), IncomingCallActivity.class.getName()));
                            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(in);

【讨论】:

    【解决方案2】:

    如果你根本不关心历史,看看

    Intent.FLAG_ACTIVITY_CLEAR_TOP

    【讨论】:

      【解决方案3】:

      试试这个

      Intent dialogIntent = new Intent(this, YourActivity.class);
              dialogIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              startActivity(dialogIntent);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多