2017-04-05 26 views
0

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

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; 
 
    } 
 
}

+0

我想你应该提供你想要打开的活动的类名,而不是MainActivity。 intent.setComponent(new ComponentName(this.getPackageName(),ActivityToBeOpened.class.getName())); –

+0

Intent应该像'Intent in = new Intent(“android.intent.category.DEFAULT”);'或'Intent in = new Intent();'? @AkashBisariya –

+0

它应该是Intent in = new Intent(); –

回答

0

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

Intent.FLAG_ACTIVITY_CLEAR_TOP

0

试试这个

Intent dialogIntent = new Intent(this, YourActivity.class); 
     dialogIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(dialogIntent); 
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); 
相关问题