TelephonyManager.EXTRA_INCOMING_NUMBER 在 android 9 中为空
TelephonyManager.EXTRA_INCOMING_NUMBER is null in android 9
我无法在 android 9 台设备中使用 TelephonyManager.EXTRA_INCOMING_NUMBER 获取呼叫者 phone 号码。它总是空的。我已经在清单文件中添加了所需的权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
////
<receiver android:name="packagename" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
////
public void onReceive(final Context context, Intent intent) {
Log.d(" onReceive: ", "flag1");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)
|| state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Log.d("Ringing", "Phone is ringing");
Intent i = new Intent(context, MainActivity.class);
i.putExtras(intent);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
在上面的 onReceive() 方法中,我访问了 phone 的状态,一切顺利..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SYSTEM_ALERT_WINDOW) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.SYSTEM_ALERT_WINDOW},
1);
}
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1=(LinearLayout)findViewById(R.id.l1);
l1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finishAndRemoveTask();
android.os.Process.killProcess(android.os.Process.myPid());
}
});
number = getIntent().getStringExtra(
TelephonyManager.EXTRA_INCOMING_NUMBER);
if (number.substring(0,1).equalsIgnoreCase("+")){
number=number.substring(3);
Log.e("bbbb",number);
}
String state = getIntent().getStringExtra(
TelephonyManager.EXTRA_STATE);
progressBarf = (ProgressBar)findViewById(R.id.progressBar1);
mobile_number = (TextView) findViewById(R.id.mobile_number);
vechile = (TextView) findViewById(R.id.vechile);
name=(TextView)findViewById(R.id.name);
name.setText("Searching");
mobile_number.setText(number);
close=(ImageView)findViewById(R.id.close);
db = new DatabaseHandler(this);
if (ConnectionDetector.isConnectingToInternet(MainActivity.this)) {
makeJsonArrayRequest();
}else{
Pojo pp= db.getContact(number);
progressBarf.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),"Get From Offline",Toast.LENGTH_SHORT).show();
if (pp.getS_vechile().equals("0")){
name.setText(pp.getS_name());
}else {
name.setText(pp.getS_name());
vechile.setText("("+pp.getS_vechile()+")");
}
Log.e("dayalen","aa"+String.valueOf(db.getContactsCount()));
}
startService(new Intent(this, UpdateService.class));
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, UpdateService.class);
PendingIntent pintent = PendingIntent
.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Start service every hour
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
3600*1000, pintent);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finishAndRemoveTask();
android.os.Process.killProcess(android.os.Process.myPid());
}
});
}
catch (Exception e) {
Log.d("Exception", e.toString());
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这里,TelephonyManager.EXTRA_INCOMING_NUMBER调用时给出null。但实际上它应该 return 来电者的 phone 号码。
你快到了。您缺少的是 READ_CALL_LOG
运行时权限。您需要像 READ_PHONE_STATE
那样请求运行时许可。
我无法在 android 9 台设备中使用 TelephonyManager.EXTRA_INCOMING_NUMBER 获取呼叫者 phone 号码。它总是空的。我已经在清单文件中添加了所需的权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
////
<receiver android:name="packagename" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
////
public void onReceive(final Context context, Intent intent) {
Log.d(" onReceive: ", "flag1");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)
|| state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Log.d("Ringing", "Phone is ringing");
Intent i = new Intent(context, MainActivity.class);
i.putExtras(intent);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
在上面的 onReceive() 方法中,我访问了 phone 的状态,一切顺利..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SYSTEM_ALERT_WINDOW) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.SYSTEM_ALERT_WINDOW},
1);
}
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1=(LinearLayout)findViewById(R.id.l1);
l1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finishAndRemoveTask();
android.os.Process.killProcess(android.os.Process.myPid());
}
});
number = getIntent().getStringExtra(
TelephonyManager.EXTRA_INCOMING_NUMBER);
if (number.substring(0,1).equalsIgnoreCase("+")){
number=number.substring(3);
Log.e("bbbb",number);
}
String state = getIntent().getStringExtra(
TelephonyManager.EXTRA_STATE);
progressBarf = (ProgressBar)findViewById(R.id.progressBar1);
mobile_number = (TextView) findViewById(R.id.mobile_number);
vechile = (TextView) findViewById(R.id.vechile);
name=(TextView)findViewById(R.id.name);
name.setText("Searching");
mobile_number.setText(number);
close=(ImageView)findViewById(R.id.close);
db = new DatabaseHandler(this);
if (ConnectionDetector.isConnectingToInternet(MainActivity.this)) {
makeJsonArrayRequest();
}else{
Pojo pp= db.getContact(number);
progressBarf.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),"Get From Offline",Toast.LENGTH_SHORT).show();
if (pp.getS_vechile().equals("0")){
name.setText(pp.getS_name());
}else {
name.setText(pp.getS_name());
vechile.setText("("+pp.getS_vechile()+")");
}
Log.e("dayalen","aa"+String.valueOf(db.getContactsCount()));
}
startService(new Intent(this, UpdateService.class));
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, UpdateService.class);
PendingIntent pintent = PendingIntent
.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Start service every hour
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
3600*1000, pintent);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finishAndRemoveTask();
android.os.Process.killProcess(android.os.Process.myPid());
}
});
}
catch (Exception e) {
Log.d("Exception", e.toString());
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这里,TelephonyManager.EXTRA_INCOMING_NUMBER调用时给出null。但实际上它应该 return 来电者的 phone 号码。
你快到了。您缺少的是 READ_CALL_LOG
运行时权限。您需要像 READ_PHONE_STATE
那样请求运行时许可。