gpt4 book ai didi

android - 检测手机状态 OnCreate

转载 作者:行者123 更新时间:2023-11-29 00:23:12 25 4
gpt4 key购买 nike

我正在制作一个应用程序,需要在创建时检测电话调用。如果接到电话,我的应用程序必须隐藏在后台。我使用了 PhoneStateListener。但是,它仅在电话状态发生变化时激活。

如何在 onCreate 函数上检测手机状态?

最佳答案

对于来电,我们需要使用 TelephonyManager 类及其方法 listen 来注册一个监听器,该监听器将接收调用状态、数据连接、网络、SIM 和其他与电话相关的事件。我们只对调用状态通知感兴趣。这需要 android.permission.READ_PHONE_STATE 权限。

因此,创建我们的派生自 PhoneStateListener 的监听器类,并重写 onCallStateChanged 方法,如下所示:

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneState extends Service {
private CallStateListener mCallStateListener = new CallStateListener();
private TelephonyManager mTelephonyManager;
private int mCallState;

@Override
public void onCreate() {
super.onCreate();
mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
mCallState = mTelephonyManager.getCallState();
mTelephonyManager.listen(mCallStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

@Override
public void onDestroy() {
Log.d("onDestroy", "onDestroy");
mTelephonyManager.listen(mCallStateListener, PhoneStateListener.LISTEN_NONE);
super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
return null; //-- not a bound service--
}

private final class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {

switch (mCallState) {

case TelephonyManager.CALL_STATE_IDLE:
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
Log.d("state", "idle --> off hook = new outgoing call");
// idle --> off hook = new outgoing call
//triggerSenses(Sense.CallEvent.OUTGOING);
} else if (state == TelephonyManager.CALL_STATE_RINGING) {
Log.d("state", "idle --> ringing = new incoming call");
// idle --> ringing = new incoming call
//triggerSenses(Sense.CallEvent.INCOMING);
}
break;

case TelephonyManager.CALL_STATE_OFFHOOK:
if (state == TelephonyManager.CALL_STATE_IDLE) {
Log.d("state", "off hook --> idle = disconnected");
// off hook --> idle = disconnected
//triggerSenses(Sense.CallEvent.ENDED);
} else if (state == TelephonyManager.CALL_STATE_RINGING) {
Log.d("state", "off hook --> ringing = another call waiting");
// off hook --> ringing = another call waiting
//triggerSenses(Sense.CallEvent.WAITING);
}
Log.d("CALL_STATE_OFFHOOK", String.valueOf(state));
break;

case TelephonyManager.CALL_STATE_RINGING:
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
Log.d("state", "ringing --> off hook = received");
// ringing --> off hook = received
//triggerSenses(Sense.CallEvent.RECEIVED);
} else if (state == TelephonyManager.CALL_STATE_IDLE) {
Log.d("state", "ringing --> idle = missed call");
// ringing --> idle = missed call
//triggerSenses(Sense.CallEvent.MISSED);
}
break;
}

mCallState = state;
}
}

public static void init(Context c) {
c.startService(new Intent(c, PhoneState.class));
Log.d("Service enabled","Service enabled: " + true);
}
}

在您的主 Activity 中,您只需启动 Phonestate 服务,

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class SpinnerSample extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PhoneState.init(getBaseContext());
}
}

当您卸载该应用程序时,它会自动停止您的服务。否则,它会检测到应用程序处于 Activity 状态还是非 Activity 状态的电话调用。

关于android - 检测手机状态 OnCreate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21642212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com