3

我正在开发一个 android 应用程序,在该应用程序中我使用服务类作为电话监听器。

通话结束后,我需要让用户回到我的应用程序。

它工作得非常好,但是当我的应用程序没有关闭并且我尝试从 Android 手机的默认拨号程序拨打电话时,在通话结束时它会返回到我的应用程序(这不是必需的)。

我该如何解决这个问题?

这是我正在使用的服务类。

PhoneStateListener phoneStateListener = new PhoneStateListener() 
    {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) 
        {
            super.onCallStateChanged(state, incomingNumber);

            if (state == TelephonyManager.CALL_STATE_RINGING)
            {
            } 
            else if(state == TelephonyManager.CALL_STATE_IDLE)
            {
                if(end.equalsIgnoreCase("YES"))
                {
                    Log.e("Service","end of cal - CAL IDLE ");
                    Intent i = new Intent(ServiceforPhone.this, PhoneCall.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);
                    end = "NO";
                    StopServ();
                }
            } 
            else if(state == TelephonyManager.CALL_STATE_OFFHOOK)
            {
                end = "YES";
                Log.e("Service"," of HOOK ");
            }
        }
    };

    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if(mgr != null)
    {
        mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    } 
4

1 回答 1

0

嗯......我认为在这种情况下你需要更复杂的逻辑。尝试在通话结束时分析当前活动 - 如果它们包含您的活动,那么您应该跳过返回,否则 - 像现在一样返回到您的应用程序。

这是代码示例,它可以在运行中找到您的活动:

PhoneStateListener phoneStateListener = new PhoneStateListener() 
    {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) 
        {
            super.onCallStateChanged(state, incomingNumber);

            if (state == TelephonyManager.CALL_STATE_RINGING)
            {
            } 
            else if(state == TelephonyManager.CALL_STATE_IDLE)
            {
                if(end.equalsIgnoreCase("YES"))
                {
                    Log.e("Service","end of cal - CAL IDLE ");

                    ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
                    List<RunningTaskInfo> tasks = am.getRunningTasks(100);
                    for (RunningTaskInfo runningTaskInfo : tasks) {
                        String packageName = runningTaskInfo.topActivity.getPackageName();
                        if packageName.equals(YOUR_ACTIVITY_PACKAGE)
                            // found our activity, exit
                            return;
                    }

                    // activity not founded, start
                    Intent i = new Intent(ServiceforPhone.this, PhoneCall.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);
                    end = "NO";
                    StopServ();
                }
            } 
            else if(state == TelephonyManager.CALL_STATE_OFFHOOK)
            {
                end = "YES";
                Log.e("Service"," of HOOK ");
            }
        }
    };

另请注意,此代码需要许可:

<uses-permission android:name="android.permission.GET_TASKS"/>
于 2013-06-20T18:21:19.187 回答