Jes*_* MJ 123 android android-intent android-permissions incoming-call
我正在尝试创建一个应用程序,当一个电话来到手机我想要检测数字.以下是我尝试过的,但它没有检测来电.
我想MainActivity在后台运行,我该怎么做?
我已经在manifest档案中给予了许可.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Run Code Online (Sandbox Code Playgroud)
我应该在清单中提供其他任何东西吗?
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
}
public class myPhoneStateChangeListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if (state == TelephonyManager.CALL_STATE_RINGING) {
String phoneNumber = incomingNumber;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Gab*_*han 324
这是我用来做这件事:
表现:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<!--This part is inside the application-->
<receiver android:name=".CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
我的基座可重复使用的呼叫探测器
package com.gabesechan.android.reusable.receivers;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
public abstract class PhonecallReceiver extends BroadcastReceiver {
//The receiver will be recreated whenever android feels like it. We need a static variable to remember data between instantiations
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber; //because the passed incoming is only valid in ringing
@Override
public void onReceive(Context context, Intent intent) {
//We listen to two intents. The new outgoing call only tells us of an outgoing call. We use it to get the number.
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
}
else{
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
state = TelephonyManager.CALL_STATE_IDLE;
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
state = TelephonyManager.CALL_STATE_OFFHOOK;
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
state = TelephonyManager.CALL_STATE_RINGING;
}
onCallStateChanged(context, state, number);
}
}
//Derived classes should override these to respond to specific events of interest
protected abstract void onIncomingCallReceived(Context ctx, String number, Date start);
protected abstract void onIncomingCallAnswered(Context ctx, String number, Date start);
protected abstract void onIncomingCallEnded(Context ctx, String number, Date start, Date end);
protected abstract void onOutgoingCallStarted(Context ctx, String number, Date start);
protected abstract void onOutgoingCallEnded(Context ctx, String number, Date start, Date end);
protected abstract void onMissedCall(Context ctx, String number, Date start);
//Deals with actual events
//Incoming call- goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
//Outgoing call- goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
public void onCallStateChanged(Context context, int state, String number) {
if(lastState == state){
//No change, debounce extras
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallReceived(context, number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing done on them
if(lastState != TelephonyManager.CALL_STATE_RINGING){
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
}
else
{
isIncoming = true;
callStartTime = new Date();
onIncomingCallAnswered(context, savedNumber, callStartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//Went to idle- this is the end of a call. What type depends on previous state(s)
if(lastState == TelephonyManager.CALL_STATE_RINGING){
//Ring but no pickup- a miss
onMissedCall(context, savedNumber, callStartTime);
}
else if(isIncoming){
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
}
else{
onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
}
break;
}
lastState = state;
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用它,只需从中派生一个类并实现一些简单的函数,无论你关心哪种调用类型:
public class CallReceiver extends PhonecallReceiver {
@Override
protected void onIncomingCallReceived(Context ctx, String number, Date start)
{
//
}
@Override
protected void onIncomingCallAnswered(Context ctx, String number, Date start)
{
//
}
@Override
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end)
{
//
}
@Override
protected void onOutgoingCallStarted(Context ctx, String number, Date start)
{
//
}
@Override
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end)
{
//
}
@Override
protected void onMissedCall(Context ctx, String number, Date start)
{
//
}
}
Run Code Online (Sandbox Code Playgroud)
另外,你可以看到我写的为什么代码就像在我的博客上一样.要点链接:https://gist.github.com/ftvs/e61ccb039f511eb288ee
编辑:更新到更简单的代码,因为我已经重新编写了我自己使用的类
sti*_*ike 21
private MyPhoneStateListener phoneStateListener = new MyPhoneStateListener();
Run Code Online (Sandbox Code Playgroud)
注册
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Run Code Online (Sandbox Code Playgroud)
并取消注册
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
Run Code Online (Sandbox Code Playgroud)
ata*_*oyh 13
使用Android P - Api等级28: 您需要获得READ_CALL_LOG权限
限制访问呼叫日志
Android的P移动CALL_LOG,READ_CALL_LOG,WRITE_CALL_LOG,和PROCESS_OUTGOING_CALLS从权限PHONE权限组新的CALL_LOG权限组.该组可让用户更好地控制和查看需要访问有关电话的敏感信息的应用,例如阅读电话记录和识别电话号码.
要从PHONE_STATE意图操作中读取数字,您需要READ_CALL_LOG权限和READ_PHONE_STATE权限.要从中读取数字onCallStateChanged(),您现在READ_CALL_LOG只需要权限.您不再需要该READ_PHONE_STATE权限.
更新:除非您明确要求用户授予必要的权限,否则Gabe Sechan发布的真棒代码将不再起作用。您可以在主要活动中放置以下代码来请求这些权限:
if (getApplicationContext().checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission has not been granted, therefore prompt the user to grant permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
if (getApplicationContext().checkSelfPermission(Manifest.permission.PROCESS_OUTGOING_CALLS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission has not been granted, therefore prompt the user to grant permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.PROCESS_OUTGOING_CALLS},
MY_PERMISSIONS_REQUEST_PROCESS_OUTGOING_CALLS);
}
Run Code Online (Sandbox Code Playgroud)
还:正如某人在Gabe的帖子下方的评论中提到的那样,您必须向android:enabled="true接收方添加一些代码段,以便在应用程序当前未在前台运行时检测传入的呼叫:
<!--This part is inside the application-->
<receiver android:name=".CallReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
只是为了更新 Gabe Sechan 的回答。如果您的清单请求对 READ_CALL_LOG 和 READ_PHONE_STATE 的权限,则 onReceive 将调用TWICE。其中一个包含 EXTRA_INCOMING_NUMBER,另一个没有。您必须测试哪个拥有它并且它可以以任何顺序发生。
这可能对您有所帮助,并且还可以添加要求权限
public class PhoneListener extends PhoneStateListener
{
private Context context;
public static String getincomno;
public PhoneListener(Context c) {
Log.i("CallRecorder", "PhoneListener constructor");
context = c;
}
public void onCallStateChanged (int state, String incomingNumber)
{
if(!TextUtils.isEmpty(incomingNumber)){
// here for Outgoing number make null to get incoming number
CallBroadcastReceiver.numberToCall = null;
getincomno = incomingNumber;
}
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("CallRecorder", "CALL_STATE_RINGING");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
123955 次 |
| 最近记录: |