传入呼叫不会触发广播接收器
Broadcast receiver is not triggered for incoming calls
我有这段代码,但我的应用程序未检测到来电。
我的代码与这个答案非常相似我做错了什么?
How does a Android "OS" detect a incoming call
<?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>
广播接收器
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);
}
}
}
}
如果您使用 android.intent.action.PHONE_STATE
,您需要先请求 READ_PHONE_STATE
许可。
因为android.permission.READ_PHONE_STATE
是危险级别权限,所以必须在运行时间内checkSelfPermission()
。
否则,当 运行在具有 Android 版本高于 Marshmallow (23) 的设备上时,将不会触发广播。
尝试
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);
}
}
}
}
我有这段代码,但我的应用程序未检测到来电。
我的代码与这个答案非常相似我做错了什么?
How does a Android "OS" detect a incoming call
<?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>
广播接收器
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);
}
}
}
}
如果您使用 android.intent.action.PHONE_STATE
,您需要先请求 READ_PHONE_STATE
许可。
因为android.permission.READ_PHONE_STATE
是危险级别权限,所以必须在运行时间内checkSelfPermission()
。
否则,当 运行在具有 Android 版本高于 Marshmallow (23) 的设备上时,将不会触发广播。
尝试
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);
}
}
}
}