在 Android 上如何知道我是否正在通话?

发布于 2024-11-06 16:22:21 字数 103 浏览 1 评论 0原文

我想知道我是否正在通话中。

如果我正在通话,则启动服务(服务部分是明确的)。我该怎么做?

在参加电话会议时,我需要致电服务...我不知道该怎么做?有什么帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清旖 2024-11-13 16:22:21

您需要广播接收器...

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

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

还声明使用权限...

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

广播接收器类...

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

    }

}

以及一个用于自定义电话状态侦听器的类...

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

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 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;
        }
    }
}
二智少女猫性小仙女 2024-11-13 16:22:21

TelephonyManager.getCallState()< 之一

  • /a> 返回CALL_STATE_IDLE
  • CALL_STATE_OFFHOOK
  • CALL_STATE_RINGING

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

TelephonyManager.getCallState() returns one of

  • 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.

止于盛夏 2024-11-13 16:22:21

毫无疑问,这是一个老问题。但它作为谷歌上的第一个结果出现。因此,我决定添加另一个有用的方法。

当电话状态更改时,系统会发送 TelephonyManager.ACTION_PHONE_STATE_CHANGED 广播。此广播有一个额外的状态,可以使用 TelephonyManager.EXTRA_STATE 提取。

额外状态可以有三种选择:

  • EXTRA_STATE_IDLE
  • EXTRA_STATE_OFFHOOK
  • EXTRA_STATE_RINGING

您可以设计一个广播接收器来处理所有这些:

public class PhoneStateReceiver extends BroadcastReceiver {

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

        if (intent.getAction().equals(TelephonyManager.ACTION_CALL_STATE_CHANGED){

            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE){

                // No call currently active.

            } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING){

                // A call is ringing

            } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK){

                // Call has been answered.

            }
        }
    }
}

您可以阅读更多关于它在这里:

https://developer.android.com/reference/android/telephony/TelephonyManager#ACTION_PHONE_STATE_CHANGED

注意: 广播接收器将在后台触发,自Android O+,您无法从后台进程启动后台服务。考虑使用 context.startForegroundService(intent) 启动前台服务。在服务的 onStartCommand(...) 中,调用 startForeground(...)。否则,系统将引发致命异常。但是,在 Android O 以下,您可以安全地使用 context.startService(intent) 而不是 startForegroundService(...)

This is an old question, no doubt. But it pops up as the first result on Google. Hence, I decided to add another useful method.

When the phone state is changed, the system sends the TelephonyManager.ACTION_PHONE_STATE_CHANGED broadcast. This broadcast has an extra state that can be extracted using TelephonyManager.EXTRA_STATE.

The extra state can have three alternatives:

  • EXTRA_STATE_IDLE
  • EXTRA_STATE_OFFHOOK
  • EXTRA_STATE_RINGING

You can design a broadcast receiver that will take care of all these:

public class PhoneStateReceiver extends BroadcastReceiver {

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

        if (intent.getAction().equals(TelephonyManager.ACTION_CALL_STATE_CHANGED){

            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE){

                // No call currently active.

            } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING){

                // A call is ringing

            } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK){

                // Call has been answered.

            }
        }
    }
}

You can read more about it here:

https://developer.android.com/reference/android/telephony/TelephonyManager#ACTION_PHONE_STATE_CHANGED

Note: The broadcast receiver will be triggered in background and as of Android O+, you cannot start a background service from a background process. Consider starting a foreground service with context.startForegroundService(intent). In the onStartCommand(...) of the service, call startForeground(...). Otherwise, the system raise a fatal exception. However, below Android O, you can safely use context.startService(intent) instead of startForegroundService(...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文