安卓 - 意图过滤器?

13

我正在尝试注册我的Activity,以便可以通过Activity选择器/选取器使用,从而允许用户选择是否选择我的应用程序/Activity来完成他们想要完成的操作。

我想为用户提供在发送短信和拨打电话时选择我的应用程序的选项。为了实现这一点,我在清单文件中的Activity标签中添加了以下代码:

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

然而,Activity选择器从未出现,原生应用程序被使用而没有提供给用户选择的选项。有人能看出我哪里做错了吗?

编辑:

我已经发现需要添加

<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

因此,我希望在单击“呼叫手机”时能得到相同的结果。

4个回答

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上,我的活动从未被呈现为完成意图的选择。 - E-Riz

3
这是因为活动和服务不能自行获取外部意图。您需要使用一个BroadcastReceiver。要实现此目的,请执行以下操作:
扩展Android的BroadcastReceiver类,如下所示:
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>

只有在清单文件中声明的意图才会被您的接收器捕获,因此如果您想要捕获系统或其他程序抛出的多个意图,并且在捕获它们的意图时要执行完全相同的操作,则应在清单文件中指定这些意图,并跳过onReceive方法中的if/else代码块。


1
我认为那不是我实现想要的方式,我想要注册系统发送的SEND和DIAL意图,当我点击发送文本时,上述的SEND已经起作用了,“使用完整操作”对话框出现了,这正是我想要的,我只需要在点击呼叫联系人时也能做到同样的事情。 - Donal Rafferty
2
那么也许你需要为你的程序指定某些权限,以允许使用特定的意图。请查看http://developer.android.com/reference/android/Manifest.permission.html。我相信你需要使用CALL_PHONE权限。 - AndrewKS
是的,我也已经加上了这个 - <uses-permission android:name="android.permission.CALL_PHONE"/> 无法理解为什么它根本不起作用。 - Donal Rafferty

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>

1
您需要为意图过滤器添加优先级,以便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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接