2011-05-03 11 views
5

He hecho una pequeña aplicación para Android móvil.Cómo obtener el evento de fin de llamada en la aplicación de Android

En una situación, no estoy obteniendo ninguna solución. En realidad, mi aplicación tiene una pequeña funcionalidad para llamar al cliente.

De modo que una vez finalizada la llamada necesito el evento del cual se marcará el último número o la aplicación que se ejecuta.

Respuesta

11

AndroidManifest:

<receiver android:name=".PhoneStateBroadcastReceiver"> 
     <intent-filter> 
       <action android:name="android.intent.action.PHONE_STATE">  
     </action></intent-filter> 
</receiver> 

Agregar permiso siguiente:

<uses-permission android:name="android.permission.READ_PHONE_STATE"> 
</uses-permission> 

PhoneStateBroadcastReceiver.java (refactorizado el código un poco)

package com.mobisys.android.salesbooster; 

import com.mobisys.android.salesbooster.database.HelperDatabase; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.provider.ContactsContract.PhoneLookup; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.util.Log; 

public class PhoneStateBroadcastReceiver extends BroadcastReceiver { 

    private static final String TAG = "PhoneStateBroadcastReceiver"; 
    Context mContext; 
    String incoming_number; 
    private int prev_state; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object 
     CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener(); 
     telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager 

     Bundle bundle = intent.getExtras(); 
     String phoneNr = bundle.getString("incoming_number"); 
     Log.v(TAG, "phoneNr: "+phoneNr); 
     mContext = context; 
    } 

    /* Custom PhoneStateListener */ 
    public class CustomPhoneStateListener extends PhoneStateListener { 

     private static final String TAG = "CustomPhoneStateListener"; 

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

      if(incomingNumber != null && incomingNumber.length() > 0) 
      incoming_number = incomingNumber; 

      switch(state){ 
       case TelephonyManager.CALL_STATE_RINGING: 
         Log.d(TAG, "CALL_STATE_RINGING"); 
         prev_state=state; 
         break; 

       case TelephonyManager.CALL_STATE_OFFHOOK: 
           Log.d(TAG, "CALL_STATE_OFFHOOK"); 
           prev_state=state; 
           break; 

       case TelephonyManager.CALL_STATE_IDLE: 

        Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_number); 

        if((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)){ 
         prev_state=state; 
         //Answered Call which is ended 
        } 
        if((prev_state == TelephonyManager.CALL_STATE_RINGING)){ 
         prev_state=state; 
         //Rejected or Missed call 
        } 
        break; 
      } 
     } 
    } 
} 

Lea más aquí, Fuente: http://mobisys.in/blog/2011/09/is-your-call-ended-on-android-phone/

Cuestiones relacionadas