[英]Broadcast receiver is not triggered for incoming calls
I have this code and my app is not detecting the incoming calls. 我有此代码,我的应用未检测到来电。
My code is very similar with this answer what am i doing wrong? 我的代码与此答案非常相似,我在做什么错呢?
How does a Android "OS" detect a incoming call Android“ OS”如何检测来电
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.reporting2you.r2ym">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.reporting2you.services.FloatingViewService"
android:enabled="true"
android:exported="false" />
<activity android:name=".FloatingActivity" />
<receiver
android:name="com.reporting2you.broadcastReceiver.CallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
BroadcastReceiver 广播接收器
public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("MY_DEBUG_TAG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
context.startActivity(new Intent(context, FloatingActivity.class));
((MainActivity)context).finish();
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.w("MY_DEBUG_TAG", phoneNumber);
}
}
}
}
If you use android.intent.action.PHONE_STATE
you need to request READ_PHONE_STATE
permission first. 如果您使用
android.intent.action.PHONE_STATE
,则需要先请求READ_PHONE_STATE
权限。 Because android.permission.READ_PHONE_STATE
is dangerous level permission, so you must checkSelfPermission()
in run time. 由于
android.permission.READ_PHONE_STATE
是危险级别权限,因此您必须在运行时checkSelfPermission()
。
If not the broadcast will not be triggered when running on device with Android version above Marshmallow (23). 如果不是这样,则在具有棉花糖(23)以上版本的Android设备上运行时,不会触发广播。
See: Requesting Permissions at Run Time 请参阅: 在运行时请求权限
Try 尝试
public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); //Change Here
Log.w("MY_DEBUG_TAG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
context.startActivity(new Intent(context, FloatingActivity.class));
((MainActivity)context).finish();
String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); //Change Here
Log.w("MY_DEBUG_TAG", phoneNumber);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.