2011-09-21 129 views
0

大家好,我想创建一个应用程序,其中有一个活动说我的活动。现在,如果在20秒内未得到答复,我希望通过来电启动它。请帮助我。关于广播接收器

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

在这里,你创建广播接收器...

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

} 
+0

感谢回复我已经完成了。如何让它等待20秒。 – Suyash

+0

你可以使用for循环来查看,而CALL_STATE_RINGER只是每次创建一个int并增加一个int。 “int i = 0;增加它i ++ –

+0

或者你可以使用一个while循环 –