我正在制作一款需要在onCreate上接听电话的应用。如果收到电话,我的应用程序必须隐藏在后台。我使用了PhoneStateListener。但是,它仅在手机状态发生变化时才会激活。
如何在onCreate功能上检测手机状态?
答案 0 :(得分:2)
对于传入呼叫,我们需要使用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);
}
}
在您的主要活动中,您只需启动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());
}
}
卸载应用程序时,它会自动停止您的服务。另外,它会检测到电话呼叫,无论是应用程序是活动的还是非活动的。
答案 1 :(得分:0)
它有两个步骤。首先,你应该有一个扩展PhoneStateListener的监听器。当手机状态改变时,它将触及onCallStateChanged。手机有三种状态:CALL_STATE_RINGING, CALL_STATE_IDLE和CALL_STATE_OFFHOOK。您可以检测到这些状态。其次,正如您所说,它还需要broadcastreceiver。您可以在后台隐藏您的APP。 最后但并非最不重要的是,不要忘记在AndroidManifest.xml中注册接收器和读取手机状态权限。