How can I launch my activity from android browser?
I have a link say,http://a.b.com. I need to open activity when user enters that URL in android browser. I have the following intent filters in my android mani开发者_如何学编程fest:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="http" android:host="a.b.com"/>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
Take a look at How to register some URL namespace (myapp://app.start/) for accessing your program by calling a URL in browser in Android OS?
(Don't get stuck at the title of the question. The answers are relevant.)
This example launch my activity from android browser and display first 2 GET prams form URL
package com.example.openapp;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt1 = (TextView) findViewById(R.id.textView1);
TextView txt2 = (TextView) findViewById(R.id.textView2);
try{
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);
txt1.setText(first);
txt2.setText(second);
}
} catch (Exception e){
}
}
}
You need to add this in manifest and replace android host with your host:
<activity
android:name="com.example.openapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="http" android:host="example.com"/>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
精彩评论