简体   繁体   中英

open my application on link click instead of browser - Android

I want to open my application on link click instead of the browser if the application is installed on the device , and open Google play otherwise. so after searching .. This is my manifest

    <activity
        android:name="example.com.MyActivity"
        android:label="@string/title_activity_open_event_details" >
        <intent-filter>
            <data
                android:host="play.google.com"
                android:scheme="http"/>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />


        </intent-filter>
    </activity>

and this is my activity

    public class MyActivity extends Activity {

private int EventId;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_open_event_details);

    tv = (TextView) findViewById(R.id.testtv);

    Uri data = getIntent().getData();
    if ( data != null )
    {
        String scheme = data.getScheme();
        String host = data.getHost(); 
        List<String> params = data.getPathSegments();
        String first = params.get(0); 
        String second = params.get(1); 

        int size = params.size();
    //  EventId  = params.get(size-1);
        String third = params.get(size-1);
        tv.setText(data.toString());
    }


  }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my_activity, menu);
    return true;
}

}

when I click on this link https://play.google.com/store/apps/details?id=example.com&evid=117 MyActivity opens successfully but the link that appears in the textview is only https://play.google.com/store/apps/details?id=example.com without &evid=117 which I need to get some data and display it in my activity

could you please help me to know what I am missing ? Thanks in advance

To get that id try this code:

    Uri data = getIntent().getData();
    if (data != null) {
        String url = data.toString();
            try {
                List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
                for (NameValuePair para : params)
                    if (para.getName().equals("evid")) {
                        String id = para.getValue(); //my id (117)
                        Log.v("myId",id);
                    }
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }

        }

Also you are not including the https protocol. Add

<data android:host="play.google.com" android:scheme="https"/>

EDIT

I've tested this code and it's working. I've created a simple activity with the code I provided above and for the intent filter:

<intent-filter>
       <action android:name="android.intent.action.MAIN" />

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

The output result is, as expected, 117

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM