android中Service类的PhoneListener

时间:2013-06-18 13:20:59

标签: android service

我正在开发一个Android应用程序,我在其中使用Phone类的Phone类。

我需要在通话结束后将用户带回我的应用程序。

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

我该如何解决这个问题?

这是我正在使用的Service类。

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

1 个答案:

答案 0 :(得分: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"/>