use*_*234 7 android broadcastreceiver android-intent
我正在尝试在启动拨出呼叫后识别并转移到活动.我ACTION_NEW_OUTGOING_CALL
在Intent过滤器中使用过.但是我怎么认识到呼叫是外向的.我为来电做了这个(如下所示),但我可以用什么而不是EXTRA_STATE_RINGING
.
public class OutgoingBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
Intent i = new Intent(context, OutgoingCallScreenDisplay.class);
i.putExtras(intent);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
public class OutgoingBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
// If it is to call (outgoing)
Intent i = new Intent(context, OutgoingCallScreenDisplay.class);
i.putExtras(intent);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
ACTION_NEW_OUTGOING_CALL是类中的常量声明Intent
,而不是TelephonyManager
.当出现呼出呼叫时,系统使用此常量广播意图.如果您真的想通过使用来接听拨出电话,TelephonyManager
请执行以下操作:
TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
PhoneStateListener listener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);
switch(state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(incomingNumber==null) {
//outgoing call
} else {
//incoming call
}
break;
case TelephonyManager.CALL_STATE_RINGING:
if(incomingNumber==null) {
//outgoing call
} else {
//incoming call
}
break;
}
}
};
Run Code Online (Sandbox Code Playgroud)
小智 5
检测去电事件
1.创建OutgoingCallBroadcastReceiver
public class OutgoingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(OutgoingCallReceiver.class.getSimpleName(), intent.toString());
Toast.makeText(context, "Outgoing call catched!", Toast.LENGTH_LONG).show();
//TODO: Handle outgoing call event here
}
}
Run Code Online (Sandbox Code Playgroud)
2.在AndroidManifest.xml中注册OutgoingCallBroadcastReceiver
<receiver android:name=".OutgoingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
3.在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
20669 次 |
最近记录: |