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.