简体   繁体   English

关于广播接收器

[英]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. 现在,如果要在20秒内仍未接听来电,我想从来电开始。请帮助我。

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. 接下来,您将需要扩展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;

Here you create your BroadCastReceiver... 在这里创建您的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++;

 } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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