是的,关于这个主题的问题已经在
Stack-Overflow;
上提出了很多问题,即使我知道CALL_STATES
,但这是一个除了那些之外的课程,我自己试过了两天,最后我决定在这里问专家......
这是我在服务中的broadcastReceiver:
private BroadcastReceiver mCallBroadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d("RECEIVER X: ", "IS UP AGAIN....");
String action = intent.getAction();
if (action.equalsIgnoreCase("android.intent.action.NEW_OUTGOING_CALL"))
{
// Outgoing call
String OutGoingPhoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d("RECEIVER X: ", "Outgoing Number - "+OutGoingPhoneNumber);
}
else if(action.equalsIgnoreCase("android.intent.action.PHONE_STATE"))
{
// Incoming call
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String InComingPhoneNumber;
if(state == null)
{
InComingPhoneNumber = "UNKNOWN";
}
else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
InComingPhoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.d("RECEIVER X: ","Incoming number - "+InComingPhoneNumber);
}
}
}
};
来自onCreate
服务我已经注册了接收者及其工作,并在系统触发NEW_OUTGOING_CALL
或PHONE_STATE
事件时被调用。
IntentFilter CallFilter = new IntentFilter();
CallFilter.addAction("android.intent.action.PHONE_STATE");
CallFilter.addAction("android.intent.action.NEW_OUTGOING_CALL");
this.registerReceiver(mCallBroadcastReceiver, CallFilter);
我的onStartCommand
是:
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d("WatchMan : ", "\nmCallBroadcastReceiver Listening....");
//return super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
并从onDestroy取消注册:
@Override
public void onDestroy()
{
this.unregisterReceiver(mCallBroadcastReceiver);
Log.d("WatchMan : ", "\nDestroyed....");
Log.d("WatchMan : ", "\nWill be created again....");
}
它是注册的接收器,并且注册正在重启,因为BOOT_COMPLETED
接收器也通过正确的通知启动此前台服务...
EXTRA_STATE_RINGING
我想要传入的电话号码
EXTRA_STATE_RINGING
我只需要NEW_OUTGOING_CALL
拨出的电话号码一次,当状态EXTRA_STATE_IDLE
表示呼叫结束时我想开始新的服务。它也应该适用于奥利奥和Android 4.2任何帮助将不胜感激,并提前感谢你。
答案 0 :(得分:0)
高于
Marshmallow
它仅需要Manifest.permission.READ_PHONE_STATE
运行时权限但在运行时也需要高于OreoManifest.permission.PROCESS_OUTGOING_CALLS
。
代码正在运行:
private BroadcastReceiver mCallBroadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d("RECEIVER X: ", "IS UP AGAIN....");
String action = intent.getAction();
String PhoneNumber = "UNKNOWN";
if (action.equalsIgnoreCase("android.intent.action.NEW_OUTGOING_CALL"))
{
PhoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d("RECEIVER X: ", "Outgoing number : " + PhoneNumber);
Intent InsertDb = new Intent(context, CatchNumbers.class);
InsertDb.putExtra("TYPE", "OUTGOING");
InsertDb.putExtra("PHONENUMBER", PhoneNumber);
startService(InsertDb);
}
if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE"))
{
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING))
{
PhoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.d("RECEIVER X: ", "Incoming number : " + PhoneNumber);
Intent InsertDb = new Intent(context, CatchNumbers.class);
InsertDb.putExtra("TYPE", "INCOMING");
InsertDb.putExtra("PHONENUMBER", PhoneNumber);
startService(InsertDb);
}
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE))
{
Log.d("RECEIVER X: ", "CALL ENDED... ");
// start service to send SMS now from Table
Intent InsertDb = new Intent(context, CatchNumbers.class);
InsertDb.putExtra("TYPE", "CALLEND");
startService(InsertDb);
}
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
Log.d("RECEIVER X: ", "ACTIVE OUTGOING CALL : ");
}
}
}
};
希望有人帮我...像我一样