Pergunta

My application is based on setting the alarm time and make the call start automatically at perfect time with speaker on . I am Using

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>

in my application. Its working fine if we consider about the application only . But weird behavior is after installation of this app , when i dont use the application than also every time this application is open and every call start with speaker . How to overcome this ??? I dont want the speaker on if i am not using this app or want to make this all permission limit within my application . Please any idea ??

Foi útil?

Solução

Define boolean callFromApp=false; // To control the call has been made from the application boolean callFromOffHook=false; // To control the change to idle state is from the app call

start call :

 PhoneCallListener phoneListener = new  PhoneCallListener();
 TelephonyManager telephonyManager = 
(TelephonyManager).getSystemService(Context.TELEPHONY_SERVICE);
                 telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
                 Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+phoneNumber));             
                callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
                startActivity(callIntent);
 callFromApp= true ; 

and

In PhoneStateListener class

class PhoneCallListener extends PhoneStateListener {

    private boolean isPhoneCalling = false;



    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing


            if (callFromApp)
            {
              AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
              audioManager.setMode(AudioManager.MODE_IN_CALL);
              audioManager.setSpeakerphoneOn(true);
            }

        }

    if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active


            isPhoneCalling = true;

            if (callFromApp)
            {
              AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
              audioManager.setMode(AudioManager.MODE_IN_CALL);
              audioManager.setSpeakerphoneOn(true);
            }

    }

        if (TelephonyManager.CALL_STATE_IDLE == state) {
        // run when class initial and phone call ended, need detect flag



            if (isPhoneCalling) {

              isPhoneCalling = false;
              callFromApp = false;
                  AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
                    audioManager.setMode(AudioManager.MODE_NORMAL); //Deactivate loudspeaker
// Remove listener
                      PhoneCallListener phoneListener = new  PhoneCallListener();
                     TelephonyManager telephonyManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
                     telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_NONE);

            }

            }
        }
    }

Hope .. This will help you. Its working perfectly without affecting other :)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top