Android - 意图过滤器?

时间:2010-11-01 12:38:16

标签: android android-intent intentfilter

我正在尝试注册我的活动,以便活动选择器/选择器可以使用它,允许用户选择是否选择我的应用程序/活动来完成他们想要做的事情。

我想为用户提供选项,以便能够在他们想要发送短信时选择我的应用程序,当他们想要拨打电话时,为了尝试实现此目的,我在其中添加了以下代码:我的清单中的活动标签:

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

当我点击Text Mobile:

时会发生这种情况

alt text

所以当我点击Call mobile

时,我想要同样的事情

4 个答案:

答案 0 :(得分: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 :(得分: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>

只有清单中声明的​​意图才会被您的Receiver捕获,因此如果您想捕获系统或其他程序抛出的几个意图,并希望在捕获它们的意图时执行完全相同的操作,那么请在此处指定这些意图清单并跳过onReceive方法中的if / else块。

答案 2 :(得分: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>

答案 3 :(得分: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>