How intercept a google maps intent in Android?

主宰稳场 提交于 2019-11-30 15:17:01
Nick Peters

The code example below is just working fine in my project.

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="https" android:host="maps.google.com" />
    <data android:scheme="geo" />
</intent-filter>

So, I'm thinking that unfortunately is not possible.

I am rather sure that it is.

Specially I think that the next intent filter must match with the mentioned intent.

You have the wrong action string. If you read the documentation, the string representation of ACTION_VIEW is:

android.intent.action.VIEW

which is not what you have in your <intent-filter>.

This manifest entry worked for me:

    <activity
        <intent-filter>  
          <!-- Filter to run activity without geo --> 
            <action android:name="com.example.MYACTION" />                
            <category android:name="android.intent.category.DEFAULT" />                     
        </intent-filter>
        <intent-filter>   
            <!-- Filter for geo scheme geo -->                  
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />               
            <data android:scheme="geo" />
        </intent-filter>
    </activity>

in your activity you can check onCreate:

protected void onCreate(Bundle savedInstanceState) {
  Intent intent = getIntent();  
  String action = intent.getAction();
  Uri data      = intent.getData();

  if(      Intent.ACTION_VIEW.equals(action) ) {
    if(data != null && "geo".equals(data.getScheme())) {  

      // do something with geo uri data
    }
  }
  else {
    // Code for activity when it`s started without geo uri
  }
}

I have been trying to get this to work as well. there seems to be couple of way here:

1) Intents with scheme: "http" or "https" with authority "maps.google.com". This seems to function normally as standard intents do. 2) using the "geo" scheme as OP mentioned in the question. Seems like this is not open to be intercepted.

@CommonsWare Even if one uses "android.intent.action.VIEW" in the manifest with the following filter: ,

it seems NOT possible to intercept it.

When you mentioned "I am rather sure that it is.", did you mean it IS possible or it IS NOT possible?

I use multi intent filter and this work with me

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

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data
                    android:host="maps.google.com"
                    android:scheme="https"/>
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data
                    android:host="maps.google.com"
                    android:scheme="http"/>
            </intent-filter>

For this you should add the following <intent-filter> inside the <activity></activity> tag of the respective ACTIVITY in AndroidManifest.xml file.

<intent-filter >
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="geo"/>
    <data android:scheme="google.navigation"/>
</intent-filter>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!