Getting the incoming number using BroadcastReceiver didn't work very well, so I'll write a solution.
I want to get the number I received when I received a call.
First, I searched and tried with the well-written code.
xml:Android.Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.callreceivetest">
<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>
</application>
</manifest>
MainActivity.java
TextView callinfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callinfo=findViewById(R.id.callinfo);
//Listener settings
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(mListener, PhoneStateListener.LISTEN_CALL_STATE);
}
PhoneStateListener mListener = new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String callNumber) {
//Log.d(TAG, ":" + state+"-PhoneNumber:"+callNumber);
switch(state){
case TelephonyManager.CALL_STATE_IDLE: //Standby (at the end)
Toast.makeText(MainActivity.this, "End call\nCALL_STATE_IDLE", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_RINGING: //Incoming call*
if(callNumber==null){
callNumber="";
}
Toast.makeText(MainActivity.this, "Incoming call\nCALL_STATE_RINGING: " + callNumber, Toast.LENGTH_SHORT).show();
callinfo.setText("Incoming call:"+callNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //Call
Toast.makeText(MainActivity.this, "talking\nCALL_STATE_OFFHOOK", Toast.LENGTH_SHORT).show();
break;
}
}
};
When I run this code on an Android 10 emulator, it looks like the image and I can get the phone status, but for some reason I can't get the phone number.
From Android 9/10 (API level 28 or higher), it seems that the permissions required to get a phone number using BroadcastReceiver have changed. Reference site: https://developer.android.com/about/versions/pie/android-9.0-changes-all?hl=ja#restrict-access-phone-numbers This time, we use onCallStateChanged () to get it, so we need ** READ_CALL_LOG (call history) permission in addition to READ_PHONE_STATE (phone) **.
However, the Android Developers site says "READ_PHONE_STATE permission is not required", but without READ_PHONE_STATE, it was not possible to get the phone number in Android 9 and earlier versions. (I actually checked it with an emulator) So, ** you need two permissions, READ_PHONE_STATE and READ_CALL_LOG, to support all versions **.
xml:Android.Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.callreceivetest">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<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>
</application>
</manifest>
I was able to get the phone number properly even on Android10!
Program: https://storyboard.jp/blog/android_telmanager/
If you want to get a status other than incoming call, in-call, or end of call, it is easy to understand by referring to here. https://qiita.com/syukai/items/d4911590a001d401a028
Recommended Posts