检查呼叫中断 - Android Lollipop

时间:2015-04-08 11:32:59

标签: java android call android-5.0-lollipop interrupt

我在onPause中使用以下代码来检查我的应用中的呼叫中断

//called inside ONPAUSE i.e. whenever my app is interrupted
ActivityManager activityManager = (ActivityManager)getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
String packageLaunched = runningTasks.get(0).baseActivity.getPackageName();

// see package name of app which interrupted my app 
if (!packageLaunched.contains("package.name.of.my.app")) {
    // do whatever when any other app interrups, as its on top.
}

问题是,
packageLaunched用于在以前版本的android上调用中断我的应用程序时更改 但是在Android Lollipop上调用不在任何正在运行的任务中:/

如何从我的Android应用程序跟踪来电?

2 个答案:

答案 0 :(得分:1)

为来电呼叫意图注册BroadCastReciever,并在onRecieveIntent中执行此操作。虽然只是一种解决方法,但在所有情况和所有操作系统版本中都能解决这个问题。

答案 1 :(得分:0)

此解决方案适用于每个版本的android,包括棒棒糖
使用此广播接收器

class TeleListener extends PhoneStateListener {
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                // CALL_STATE_IDLE;
                Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
                        Toast.LENGTH_LONG).show();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                // CALL_STATE_OFFHOOK;
                Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
                        Toast.LENGTH_LONG).show();
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                // CALL_STATE_RINGING
                Toast.makeText(getApplicationContext(), incomingNumber,
                        Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
                        Toast.LENGTH_LONG).show();
                break;
            default:
                break;
            }
        }
    }  

并在onCreate中将其注册为

TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        TelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);  

是的,请不要错过   

干杯!你的问题解决了;)