Android支持通过Url打开App,比如下面的Url
scheme://host/datastring
要打开这样的Url,首先在配置文件AndroidManifest.xml里使用<data>添加一种App打开的格式,代码如下
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.sheng00.customuridemo”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk
android:minSdkVersion=”9″
android:targetSdkVersion=”17″ />
<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.sheng00.customuridemo.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
<!– Open links like scheme://host/?… –>
<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=”scheme” android:host=”host” />
</intent-filter>
</activity>
</application>
</manifest>
注意,第二个<intent-filter>中的data绑定了url 在页面中出现scheme://host/类似这样的链接,点击就可以直接打开app app中处理链接地址代码如下
package com.sheng00.customuridemo;
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);
}
@Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
if (intent != null) {
String action = intent.getAction();
String dataString = intent.getDataString();
if(dataString!=null){
TextView tv = (TextView) findViewById(R.id.tv);
tv.append("n" + dataString);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
源代码:https://github.com/shengoo/myandroidcode/tree/master/CustomUriDemo