简体   繁体   中英

about broadcast receiver

Hi all I want to create an application in which there is an activity say My activity. Now I want to start it with incoming call, if it is not answered within 20 seconds.Please Help me.

You would first need to register your receiver such as..

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

Here you register to listen for the phones state to change.

Next you would want to extend the phonestateListener for your specifications.

public class CustomPhoneStateListener extends PhoneStateListener {



private static final String TAG = "CustomPhoneStateListener";

public void onCallStateChange(int state, String incomingNumber){
//Here you recieve the phone state being changed and are able to get the number and state.  

 switch(state){
            case TelephonyManager.CALL_STATE_RINGING:
                    Log.d(TAG, "RINGING");
                  //Here you could count with for() for 20 seconds and then create a method to do what you want.
                    break;

Here you create your BroadCastReceiver...

public class CustomBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "CustomBroadcastReceiver";

@Override
public void onReceive(Context context, Intent intent) {
    Log.v(TAG, "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);

}

EDIT:

To count you could create a method such as

public void increment() {
    if (count < maxCount) count++;

 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM