I want to get the incoming caller's phone number in my flutter app I found this code that gives me the number in Android but for some reason, it doesn't work with flutter
I got the broadcastreceiver from here Access phone number of incoming calls in flutter
And this is my main
package com.example.docapp;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.telephony.PhoneStateListener;
import android.os.Build;
import android.os.Bundle;
public class MainActivity extends FlutterActivity {
private static final String PhoneNumber = "www.this.com/callerid";
private Intent forService;
String num;
int state;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.d("Oncreate Main","MAIN MAIN MAIN");
}
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine){
super.configureFlutterEngine(flutterEngine);
forService = new Intent(MainActivity.this,MyService.class);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(),PhoneNumber)
.setMethodCallHandler(
(call,result)->{
if(call.method.equals("getcallerid")){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
startForegroundService(forService);
} else{
startService(forService);
}
result.success("this");
Log.d("My Activity","Method in Java");
}else{
result.notImplemented();
}
}
);
}
}
I am trying this on my phone with Android 9.
I can suggest you more easy solution for this with sharedPreferences and BroadcastReceiver only for android (on ios i have not many experience and not trying this yet)
Paste this code in <application>
tag
/android/app/src/main/AndroidManifest.xml
<receiver android:exported="true" android:name=".ServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
And inside <manifest>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
Then create in your project folder file /android/app/src/main/kotlin/your/package/name/ServiceReceiver.java
With content:
package your.package.name;
import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.telephony.TelephonyManager;
import android.telephony.PhoneStateListener;
import android.content.SharedPreferences;
public class ServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(incomingNumber != "" && incomingNumber != null && !incomingNumber.isEmpty()){
SharedPreferences settings = context.getSharedPreferences("FlutterSharedPreferences", context.MODE_PRIVATE); //Name of SP database for flutter
SharedPreferences.Editor editor = settings.edit();
editor.putString("flutter.verification_phone", incomingNumber).apply();
System.out.println(settings.getString("flutter.verification_phone", "NOTFOUND"));
System.out.println(incomingNumber);
}
super.onCallStateChanged(state, incomingNumber);
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
}
And inside your dart file (where you need read this value):
SharedPreferences sp = await SharedPreferences.getInstance();
await sp.reload(); //needed for getting updated value
String someVariable = "";
if(sp.containsKey('verification_phone'))
{
someVariable = sp.getString('verification_phone')!;
}
print(someVariable);
Don't forget about request for permissions in dart code (I use permission_handler package)
await Permission.contacts.request();
await Permission.phone.request();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.