如何知道我是否正在使用Android进行通话?

时间:2021-10-13 07:32:33

I want to know whether I am in a call.

我想知道我是否正在打电话。

If I am in a call then start the service (service part is clear). How do I do this?

如果我正在通话,则启动服务(服务部分清除)。我该怎么做呢?

While attending the call I need to call the service... I am unaware of how to do this? Any help?

在接听电话时我需要拨打电话......我不知道该怎么办?有帮助吗?

3 个解决方案

#1


53  

You need broadcast receiver ...

你需要广播接收器......

In manifest declare broadcast receiver ...

在清单声明广播接收器中......

<receiver android:name=".PhoneStateBroadcastReceiver">
        <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"/>     
        </intent-filter>
</receiver>

Also declare uses-permission ...

同时声明uses-permission ...

<uses-permission android:name="android.permission.READ_PHONE_STATE" />  

The broadcast receiver class ...

广播接收机班......

package x.y;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class PhoneStateBroadcastReceiver extends BroadcastReceiver{

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

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);

    }

}

And one class to customize phone state listener...

并且有一类来定制手机状态监听器......

package x.y;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class CustomPhoneStateListener extends PhoneStateListener {

    //private static final String TAG = "PhoneStateChanged";
    Context context; //Context to make Toast if required 
    public CustomPhoneStateListener(Context context) {
        super();
        this.context = context;
    }

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

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            //when Idle i.e no call
            Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //when Off hook i.e in call
            //Make intent and start your service here
            Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            //when Ringing
            Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();
            break;
        default:
            break;
        }
    }
}

#2


29  

TelephonyManager.getCallState() returns one of

TelephonyManager.getCallState()返回其中一个

  • CALL_STATE_IDLE
  • CALL_STATE_OFFHOOK
  • CALL_STATE_RINGING

If this fits your requirements, it's much less code than Pied Piper's more comprehensive solution.

如果这符合您的要求,它的代码比Pied Piper更全面的解决方案要少得多。

#3


1  

You can only come to know call is coming but you can't modify this. :( see this why 2.3 version of android does not hava android.permission.MODIFY_PHONE_STATE ? and what is the solution for this?

你只能知道电话即将到来,但你不能修改它。 :(看到这个为什么2.3版本的android没有hava android.permission.MODIFY_PHONE_STATE?这是什么解决方案?

#1


53  

You need broadcast receiver ...

你需要广播接收器......

In manifest declare broadcast receiver ...

在清单声明广播接收器中......

<receiver android:name=".PhoneStateBroadcastReceiver">
        <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"/>     
        </intent-filter>
</receiver>

Also declare uses-permission ...

同时声明uses-permission ...

<uses-permission android:name="android.permission.READ_PHONE_STATE" />  

The broadcast receiver class ...

广播接收机班......

package x.y;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class PhoneStateBroadcastReceiver extends BroadcastReceiver{

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

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);

    }

}

And one class to customize phone state listener...

并且有一类来定制手机状态监听器......

package x.y;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class CustomPhoneStateListener extends PhoneStateListener {

    //private static final String TAG = "PhoneStateChanged";
    Context context; //Context to make Toast if required 
    public CustomPhoneStateListener(Context context) {
        super();
        this.context = context;
    }

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

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            //when Idle i.e no call
            Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //when Off hook i.e in call
            //Make intent and start your service here
            Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            //when Ringing
            Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();
            break;
        default:
            break;
        }
    }
}

#2


29  

TelephonyManager.getCallState() returns one of

TelephonyManager.getCallState()返回其中一个

  • CALL_STATE_IDLE
  • CALL_STATE_OFFHOOK
  • CALL_STATE_RINGING

If this fits your requirements, it's much less code than Pied Piper's more comprehensive solution.

如果这符合您的要求,它的代码比Pied Piper更全面的解决方案要少得多。

#3


1  

You can only come to know call is coming but you can't modify this. :( see this why 2.3 version of android does not hava android.permission.MODIFY_PHONE_STATE ? and what is the solution for this?

你只能知道电话即将到来,但你不能修改它。 :(看到这个为什么2.3版本的android没有hava android.permission.MODIFY_PHONE_STATE?这是什么解决方案?