Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TelephonyManager.EXTRA_INCOMING_NUMBER is null in android 9 [duplicate]

I am not able to get the caller phone number using TelephonyManager.EXTRA_INCOMING_NUMBER in android 9 devices. Its always null. I have added the required permissions in my manifest file.

<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);
    }
}

In the above onReceive() method, I have accessed the phone's state which goes well..

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();
    }

}

Here, TelephonyManager.EXTRA_INCOMING_NUMBER gives null when call is made. But actually it should return the caller's phone number.

like image 573
Divakar Thirunavukarasu Avatar asked Mar 22 '19 09:03

Divakar Thirunavukarasu


People also ask

What is telephonymanager in Android?

TelephonyManager extends Object java.lang.Object android.telephony.TelephonyManager Class Overview Provides access to information about the telephony services on the device. Applications can use the methods in this class to determine telephony services and states, as well as to access some types of subscriber information.

How to unregister a telephony listener?

At registration, and when a specified telephony state changes, the telephony manager invokes the appropriate callback method on the listener object and passes the current (udpated) values. To unregister a listener, pass the listener object and set the events argument to LISTEN_NONE(0). Parameters

How to get the phone number string for Line 1?

String getLine1Number() Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. List<NeighboringCellInfo>

How do I instantiate a telephony class in Java?

You do not instantiate this class directly; instead, you retrieve a reference to an instance through Context.getSystemService(Context.TELEPHONY_SERVICE). Note that access to some telephony information is permission-protected.


1 Answers

You are almost there. What you missing is READ_CALL_LOG runtime permission. You need to ask for runtime permission just like the way you did for READ_PHONE_STATE.

like image 91
Passionate Androiden Avatar answered Oct 06 '22 16:10

Passionate Androiden