开发者

How to get call end event in Android App

I have made small app for Android mob开发者_如何学运维ile.

In one situation I am not getting any solution. Actually my app has small functionality for calling to customer.

So after call ended I need that event of which last number will dialed or which app is runs.


AndroidManifest:

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

Add following permission:

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

PhoneStateBroadcastReceiver.java (refactored the code a bit)

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

Read more here, Source : http://mobisys.in/blog/2011/09/is-your-call-ended-on-android-phone/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜