2011-09-21 154 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循環 –