To me, in my application, programmatically count the phone number, the person who calls me. How to do it? Which way to look?
- 2If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦
- Failed to repeat, does not display anything ( - Dmitry Polishchuk
|
3 answers
Create a class that inherits from BroadcastReceiver
public class CustomBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "CustomBroadcastReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!"); TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener(); telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); Bundle bundle = intent.getExtras(); String phoneNr= bundle.getString("incoming_number"); Log.v(TAG, "phoneNr: "+phoneNr); } }
Create this class:
public class CustomPhoneStateListener extends PhoneStateListener { private static final String TAG = "CustomPhoneStateListener"; public void onCallStateChanged(int state, String incomingNumber){ switch(state){ case TelephonyManager.CALL_STATE_RINGING: Log.d(TAG, "RINGING"); Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!"); Log.v(TAG, incomingNumber); break; } }
Further in the manifesto you write:
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> ... <receiver android:name=".CustomBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver>
And now when a call comes in, the caller's number will be displayed
|
Also faced with such a need. Your code is redundant. This number is packed by the system in intent, which arrives in the onReceive method. You can get the number itself like this:
public class PhoneStateListener extends BroadcastReceiver { String inComingStringNumber; @Override public void onReceive(Context context, Intent intent) { inComingStringNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); Toast.makeText(сontext, inComingStringNumber, Toast.LENGTH_LONG).show(); } }
Of course, we don’t forget to register the receiver in the manifest, and give the application rights to read the telephone line status. But it is already much easier to googling and described above.
|
Author's answer:
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) { super.onCallStateChanged(state, incomingNumber); System.out.println("incomingNumber : "+incomingNumber); } },PhoneStateListener.LISTEN_CALL_STATE); }
and
<receiver android:name=".ServiceReceiver" > <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver>
|