我希望我的应用能够检测是否发生了新的拨出电话
我想做这样的事情:
if (there was a new outgoing call) {
do...
} else {
do...
}
答案 0 :(得分:0)
您可以使用android Broadcast Reciever。要使用它,您首先要做的是
创建一个新的处理程序类来处理广播接收器。
public class OutgoingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//You can do this to get the phone number, it is only optional
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//Handle outgoing call event here
}
}
然后在android清单中注册它,以便它知道这个方法的位置。
<receiver android:name=".OutgoingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
最后,在android清单中创建一个权限。
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
应该这样做,希望我能提供帮助。 :)
答案 1 :(得分:0)
创建广播接收器以捕获拨出电话:
public class CallReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// your code
}
}
然后在清单中注册:
<receiver android:name=".CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
最后设置权限:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
答案 2 :(得分:0)
检测外拨电话事件
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
}
}
在AndroidManifest.xml中注册OutgoingCallBroadcastReceiver
在AndroidManifest.xml中添加权限