我创建了一个服务和广播接收器来获得电话状态.
以下是我的代码:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_OUT);
filter.addAction(ACTION_IN);
if (br_call == null) {
br_call = new CallBr();
registerReceiver(br_call, filter);
}
return super.onStartCommand(intent, flags, startId);
}
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_IN)) {
if ((bundle = intent.getExtras()) != null) {
state =bundle.getString(TelephonyManager.EXTRA_STATE);
Log.d("tag", "state ::" + state);
if (!wasRinging) {
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
inCall=bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
wasRinging = true;
Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG).show();
}
}
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){ …Run Code Online (Sandbox Code Playgroud)