2011-04-28 83 views
4

我的意圖是讓廣播接收機在接收呼叫時執行操作。有沒有可能比自動呼叫接收SO有更高的優先級?接收呼叫的廣播接收機的優先級

我試過分配2147483647的優先級,我認爲它是最好的,但仍然在我的接收器結束之前跳過我嘗試呼叫。

<!-- Receiver de llamadas --> 
<receiver android:name=".PhoneCall"> 
    <intent-filter android:priority="2147483647"> 
     <action android:name="android.intent.action.PHONE_STATE"/> 
    </intent-filter> 
</receiver> 

回答

6

此鏈接回答我:

http://developer.android.com/reference/android/content/BroadcastReceiver.html

有可以接收廣播兩大類:

  • 正常播放(帶Context.sendBroadcast發)完全異步。廣播的所有接收者通常在同一時間以未定義的 的順序運行。這樣更有效率,但意味着接收者不能使用此處包含的結果或中止API。

  • 有序廣播(使用Context.sendOrderedBroadcast發送)一次傳遞給一個接收者。由於每個接收器在 轉中執行,它可以將結果傳播到下一個接收器,或者它可以完全中止廣播,使其不會傳遞到其他接收器。接受訂單的接收者可以通過匹配意圖過濾器的android:priority屬性來控制;具有相同優先級的接收器 將以任意順序運行。

廣播像PHONE_STATE是 「正常播出」。據我所知,我不能優先考慮我的廣播。有沒有人想到任何方式?

2

其實,我不認爲2147483647是最好的使用價值,因爲Android不理解它,並會忽略這個值。你要做的就是設置優先級爲999,因爲我猜1000是最大值。

+0

參考此文檔谷歌更多信息[鏈接](HTTP: //developer.android.com/reference/android/content/IntentFilter.html#SYSTEM_HIGH_PRIORITY) – JeffE 2011-10-18 23:12:36

0

我的溶液創建兩個廣播接收機。 第一個接收器用於接收系統通過Action:android.intent.action.PHONE_STATE發送的廣播。 第二個接收器由第一個接收器調用。 (第一個接收器將發送廣播,我發現在所有接收器接收到android.intent.action.PHONE_STATE之後,這個廣播將被第二個接收器接收。)

詳細代碼如下所示: 第一個接收器CallReceiver。JAVA):

public class CallReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent newintent = new Intent(intent); 
     newintent.setAction(""); 
     newintent.setClass(context, SecondReceiver.class); 
     context.sendBroadcast(newintent); 
    } 
} 

第二接收器(SecondReceiver.java):

public class SecondReceiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String number = intent.getStringExtra(
     TelephonyManager.EXTRA_INCOMING_NUMBER); 
     String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 
    } 
} 

的AndroidManifest.xml:

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

<receiver android:name=".SecondReceiver" />