android PIE 9 结束通话后来电号码为空
Incoming number is null after ending the call in android PIE 9
几周以来我一直在研究这个问题。我正在制作一个应用程序,它会选择来电号码并在通话结束后将其显示在对话框中。在 android PIE 9.0 以下一切正常。 android PIE 中的数字始终为空。我已经授予了所有权限,包括 READ_CALL_LOGS
但同样的问题。来电号码为空。所以请任何人帮助我...
这是我的清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.softixtechnologies.phonemanager">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/calllogo"
android:label="@string/app_name"
android:persistent="true"
android:roundIcon="@drawable/calllogo"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".activities.CallLogEntryActivity"></activity>
<activity android:name=".activities.CallLogsActivity" />
<activity android:name=".activities.IgnoredNumbersActivity" />
<activity android:name=".activities.IContactsActivity" />
<activity android:name=".activities.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.CategoryPhoneActivity" />
<activity
android:name=".activities.LogsActivity"
android:label="@string/title_activity_logs"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".activities.CategoryActivity"
android:label="@string/title_activity_category"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".MainActivity" />
<receiver
android:name=".callhelpers.CallReciever"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
这是我的代码:
public class PhoneCallReceiver extends BroadcastReceiver {
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber;
public String phoneNr ;
DatabaseHelper databaseHelper ;
@Override
public void onReceive(final Context context, Intent intent) {
databaseHelper = new DatabaseHelper(context);
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);
}
}
protected void onIncomingCallStarted(Context ctx, String number, Date start){}
protected void onOutgoingCallStarted(Context ctx, String number, Date start){}
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end){}
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end){}
protected void onMissedCall(final Context ctx, final String number, Date start){}
public void onCallStateChanged(Context context, int state, String number){
if (lastState == state) {
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallStarted(context, number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
onMissedCall(context, savedNumber, callStartTime);
} else if (isIncoming) {
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
} else {
onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
}
break;
}
lastState = state;
}
public boolean contactExists(Context context, String number) {
Uri lookupUri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
}
}
非常感谢您的帮助!!
有两种不同的 "types" 的 phone 状态接收器,具有 Manifest.permission.READ_CALL_LOG
权限的接收器和没有权限的接收器。
如果一个接收者没有Manifest.permission.READ_CALL_LOG
权限,它会被调用所有状态改变一次,没有EXTRA_INCOMING_NUMBER
额外的。
如果接收者确实具有上述权限,就像您的情况一样,每次状态更改时都会调用它两次,一次没有 EXTRA_INCOMING_NUMBER
权限,另一次有权限。
因为你有这个代码:
if (lastState == state) {
return;
}
在您的 onCallStateChanged
方法中,您基本上跳过了第二次调用,因此丢失了 EXTRA_INCOMING_NUMBER
信息。
如果你确定你有 READ_CALL_LOG
权限,你可以尝试完全跳过重复的接收者调用(但请注意,获得 "null" 值是有区别的额外的意思是私人号码呼叫,并且根本没有得到额外的),就像这样:
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
if (!intent.getExtras().containsKey(TelephonyManager.EXTRA_INCOMING_NUMBER)) {
Log.i("Call receiver", "skipping intent=" + intent + ", extras=" + intent.getExtras() + " - no number was supplied");
return;
}
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
几周以来我一直在研究这个问题。我正在制作一个应用程序,它会选择来电号码并在通话结束后将其显示在对话框中。在 android PIE 9.0 以下一切正常。 android PIE 中的数字始终为空。我已经授予了所有权限,包括 READ_CALL_LOGS
但同样的问题。来电号码为空。所以请任何人帮助我...
这是我的清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.softixtechnologies.phonemanager">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/calllogo"
android:label="@string/app_name"
android:persistent="true"
android:roundIcon="@drawable/calllogo"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".activities.CallLogEntryActivity"></activity>
<activity android:name=".activities.CallLogsActivity" />
<activity android:name=".activities.IgnoredNumbersActivity" />
<activity android:name=".activities.IContactsActivity" />
<activity android:name=".activities.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.CategoryPhoneActivity" />
<activity
android:name=".activities.LogsActivity"
android:label="@string/title_activity_logs"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".activities.CategoryActivity"
android:label="@string/title_activity_category"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".MainActivity" />
<receiver
android:name=".callhelpers.CallReciever"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
这是我的代码:
public class PhoneCallReceiver extends BroadcastReceiver {
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber;
public String phoneNr ;
DatabaseHelper databaseHelper ;
@Override
public void onReceive(final Context context, Intent intent) {
databaseHelper = new DatabaseHelper(context);
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);
}
}
protected void onIncomingCallStarted(Context ctx, String number, Date start){}
protected void onOutgoingCallStarted(Context ctx, String number, Date start){}
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end){}
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end){}
protected void onMissedCall(final Context ctx, final String number, Date start){}
public void onCallStateChanged(Context context, int state, String number){
if (lastState == state) {
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallStarted(context, number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
onMissedCall(context, savedNumber, callStartTime);
} else if (isIncoming) {
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
} else {
onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
}
break;
}
lastState = state;
}
public boolean contactExists(Context context, String number) {
Uri lookupUri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
}
}
非常感谢您的帮助!!
有两种不同的 "types" 的 phone 状态接收器,具有 Manifest.permission.READ_CALL_LOG
权限的接收器和没有权限的接收器。
如果一个接收者没有Manifest.permission.READ_CALL_LOG
权限,它会被调用所有状态改变一次,没有EXTRA_INCOMING_NUMBER
额外的。
如果接收者确实具有上述权限,就像您的情况一样,每次状态更改时都会调用它两次,一次没有 EXTRA_INCOMING_NUMBER
权限,另一次有权限。
因为你有这个代码:
if (lastState == state) {
return;
}
在您的 onCallStateChanged
方法中,您基本上跳过了第二次调用,因此丢失了 EXTRA_INCOMING_NUMBER
信息。
如果你确定你有 READ_CALL_LOG
权限,你可以尝试完全跳过重复的接收者调用(但请注意,获得 "null" 值是有区别的额外的意思是私人号码呼叫,并且根本没有得到额外的),就像这样:
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
if (!intent.getExtras().containsKey(TelephonyManager.EXTRA_INCOMING_NUMBER)) {
Log.i("Call receiver", "skipping intent=" + intent + ", extras=" + intent.getExtras() + " - no number was supplied");
return;
}
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);