如何从Android浏览器启动我的活动?
我有一个链接说http://a.b.com。当用户在android浏览器中输入该URL时,我需要打开活动。我的android清单中有以下intent过滤器:
<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>
答案 0 :(得分:3)
(不要陷入问题的标题。答案是相关的。)
答案 1 :(得分:0)
此示例从Android浏览器启动我的活动,并从URL
显示前2个GET参数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){
}
}
}
您需要在清单中添加此内容并将android主机替换为您的主机:
<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>