2010-11-01 201 views
13

我正在嘗試註冊我的Activity,以便Activity選擇器/選取器可以使用它,從而允許用戶選擇是否選擇我的應用程序/活動來完成他們正在嘗試執行的操作。Android - 意圖過濾器?

我想提供選項讓用戶能夠選擇我的應用程序,當他們想發送一條短信,當他們想發出一個呼出,試圖實現這個我添加了下面的代碼片段在我的清單我的活動標籤內:

<intent-filter> 
<action android:name="android.intent.action.SENDTO" /> 
<category android:name="android.intent.category.DEFAULT" /> 
<data android:mimeType="text/plain" /> 
</intent-filter> 

<intent-filter> 
<action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
<category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 

然而,活動選擇器永遠不會出現,沒有提供選擇,讓用戶使用本機應用程序。任何人都可以看到我要去哪裏嗎?

編輯:

我已經想通了,我需要添加

<data android:scheme="sms" /> 
<data android:scheme="smsto" /> 

的短信,但我該怎麼使用的呼出?

編輯2:

我已經試過了呼出如下:

<intent-filter> 
<action android:name="android.intent.action.VIEW" /> 
<action android:name="android.intent.action.DIAL" /> 
<category android:name="android.intent.category.DEFAULT" /> 
<category android:name="android.intent.category.BROWSABLE" /> 
<data android:scheme="tel" /> 
</intent-filter> 

但同樣,沒有運氣,這已經從1.6封鎖?

編輯3:

這是當我單擊文本移動會發生什麼:

alt text

所以我想,當我點擊呼叫移動

回答

9

,我認爲它應該幫助你

<intent-filter > 
    <action android:name="android.intent.action.CALL_PRIVILEGED" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <data android:scheme="tel" /> 
</intent-filter> 

測試在Android 2。1

+1

有沒有人注意到這不適用於HTC手機?我爲我的活動添加了同樣的意圖過濾器,並且它可以在一些手機上使用,但在HTC Incredible上,我的活動永遠不會作爲完成意圖的選擇。 – 2011-09-20 18:15:34

3

這是因爲同樣的事情活動和服務不能單獨採用外部意圖。您需要使用BroadcastReceiver。爲了實現這一點,請執行下列操作:

擴展Android的廣播接收器類,像這樣:

public class MyReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
     Context context = getApplicationContext(); 
    Intent sendIntent = new Intent(); 
    if (intent.getAction().equals(Intent.NEW_OUTGOING_CALL)) { 
    sendIntent.setClass(context, MyActivity.class); 
    context.startActivity(sendIntent); 
    } 
     else if (intent.getAction().equals(Intent.SOME_OTHER_INTENT) { 
      sendIntent.setClass(context, MyService.class); 
    context.startService(sendIntent); 
     } 
     // More else ifs for more intents you want to catch. 
} 

} 

然後,你需要聲明的接收器在您的清單如下:

<receiver android:name="MyReceiver"> 
    <intent-filter> 
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
     // more intents you want to catch here 
    </intent-filter> 
</receiver> 

在宣佈僅意圖該清單將被Receiver捕獲,因此如果您想要捕獲系統或其他程序拋出的多個意圖,並且希望在捕獲其意圖時執行完全相同的操作,請在清單中指定這些意圖,並跳過if /其他塊在onReceive方法中。

+1

我不認爲這是我實現我想要什麼,我要註冊爲SEND和撥號系統發送的意圖的正確方法,當我點擊發送文本時,發送一個按上述方式工作的「完成操作使用」對話框出現,這正是我想要的,當我點擊通話聯繫人時,我只需要能夠做到這一點。 – 2010-11-01 15:22:59

+2

啊,那麼也許你需要指定一些你希望程序被允許使用的意圖。看看http://developer.android.com/reference/android/Manifest.permission.html。我相信你需要使用CALL_PHONE權限。 – AndrewKS 2010-11-01 15:31:58

+0

是的,我也在那裏 - <使用權限android:name =「android.permission.CALL_PHONE」/>不能理解爲什麼它不工作。 – 2010-11-01 15:53:20

1

您需要爲intent過濾器添加優先級,以便Android將其考慮在內。例如:

<activity android:name="YourActivity"> 
    <intent-filter android:priority="100"> 
     <action android:name="android.intent.action.CALL_PRIVILEGED" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <data android:mimeType="text/plain" /> 
    </intent-filter> 
</activity> 
2

這個工作對我來說

<activity 
     android:label="@string/app_name" 
     android:name=".TestIntentActivity" >    
     <intent-filter> 
     <action android:name="android.intent.action.CALL" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <action android:name="android.intent.action.CALL_PRIVILEGED" /> 
     <data android:scheme="tel" /> 
     </intent-filter> 
    </activity>