开发者

about broadcast receiver

Hi all I want to create an application in which there is an activity say My activity. Now I wa开发者_如何学运维nt 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++;

 } 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜