我一直在尝试运行一个可以收听phone_state通话的应用。 但它似乎没有这样做。它可以,但它需要时间来最终听,或者它根本不听。
这一直困扰着我,因为它适用于我测试过的所有设备,除了Sony Xperia Z5。 我已在Activity上添加了必要的权限和运行时权限。 主要目的是从应用程序中拨打号码。然后通过电话听取状态变化。
BroadcastReceiver代码:
public class OutgoingCallReceiver extends BroadcastReceiver {
static CallPromptView mCallPromptView;
Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if(!TextUtils.isEmpty(number)){
setPrefPhoneNumber(number);
}
}
else if(intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
String number = getPrefPhoneNumber();
if(tm.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK ||
tm.getCallState() == TelephonyManager.CALL_STATE_RINGING) {
Log.d("STATE", "Phonestate : " + tm.getCallState());
}else if(tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
Log.d("STATE", "Phonestate idle");
}
}
}
}
}
的AndroidManifest.xml:
<!-- Phone call permissions -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<!-- GPS permissions -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".ui.MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".receiver.OutgoingCallReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
</application>
<!-- Window permissions -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />