Android - Intent Filter?

2019-01-13 17:09发布

问题:

I am trying to register my Activity so that it can be used by the Activity chooser/picker allowing a user to choose whether or not to select my application/Activity to complete what they are trying to do.

I want to provide the option for the user to be able to select my application when they want to send an SMS message and when they want to place an outgoing call, to try to achieve this I added the following pieces of code within my Activity tags in my manifest:

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

However the Activity chooser never appears and the native apps are used without providing a choice to the user. Can anyone see where I am going wrong?

EDIT:

I have figured out I need to add

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

for the SMS message but what do I use for the outgoing call?

EDIT 2:

I have tried the following for the outgoing call:

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

But again with no luck, has this been blocked from 1.6 on?

Edit 3:

This is what happens when I click Text Mobile:

So i want the same thing when I click Call mobile

回答1:

I think it should help you

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

Tested on Android 2.1



回答2:

This is because activities and services can not pickup outside intents on their own. You need to use a BroadcastReceiver. To implement this, do the following:

Extend Android's BroadcastReceiver class like so:

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

}

Then you need to declare the receiver in your manifest like this:

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

Only intents declared in the manifest will be caught by your Receiver, so if you want to catch several intents thrown by the system or other programs and want to do the exact same thing when their intents are caught then specify those intents here in the manifest and skip the if/else blocks in the onReceive method.



回答3:

this worked for me

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


回答4:

You need to add a priority to the intent filter so that Android takes it into account. For example:

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