2010-11-01 118 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>