关于广播接收器

时间:2011-09-21 19:29:39

标签: java android

大家好我想要创建一个活动,其中有一个活动说我的活动。现在我想用来电启动它,如果它在20秒内没有得到答复。请帮助我。

1 个答案:

答案 0 :(得分:1)

您首先需要注册您的接收器,例如..

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

在这里注册以收听要更改的手机状态。

接下来,您需要扩展phonestateListener以符合您的规范。

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;

在这里创建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);

}

编辑:

要计算,您可以创建一个方法,例如

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

 }