現如互聯網的發展,單個app或者單個平台很難滿足公司業務的需求,多平台合作或協作是大勢所趨。並且未來IT業務的發展形勢必然要朝着平台化共享化的方向發展。業務對於平台的依賴也會越來越強。平台之間的數據交換和共享是必然會發生的事情。
扯遠了,把視線回歸到APP本身。不同的APP之間也是需要進行數據交互。舉個栗子簡單說下URL Scheme
假如有這么一個場景,公司由於業務的發展,需要app用戶在app內進行實名認證,恰好支付寶平台提供芝麻實名認證功能。那么app就需要進行喚起支付寶app進行實名認證,實名認證完畢之后支付寶返回實名認證結果。這就完成了不同app之間進行了數據交互。
URL Scheme:
android中的scheme是一種頁面內跳轉協議,是一種非常好的實現機制,通過定義自己的scheme協議,可以非常方便跳轉app中的各個頁面;通過scheme協議,服務器可以定制化告訴App跳轉那個頁面,可以通過通知欄消息定制化跳轉頁面,可以通過H5頁面跳轉頁面等。
URL Scheme協議格式:
栗子1: http://baidu:8080/news?system=pc&id=45464 栗子2: alipays://platformapi/startapp?appId=20000067
http 代表Scheme的協議
baidu代表作用於哪個地址域
8080代表路徑的端口號
news代表Scheme指定的頁面
system和id代表要傳遞的參數
URL Scheme使用方法:
首先要在Mainifest文件中對要啟動的activity進行添加過濾器。
<activity android:name="com.example.helloworld.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </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:scheme="http" android:host="baidu" android:path="/news" android:port="8080"/>
</intent-filter> </activity>
在activity中進行接收參數:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = getIntent(); String scheme = intent.getScheme(); String dataString = intent.getDataString(); Uri uri = intent.getData(); System.out.println("scheme:" + scheme); if (uri != null) { //完整的url信息 String url = uri.toString(); //scheme部分 String schemes = uri.getScheme(); //host部分 String host = uri.getHost(); //port部分 int port = uri.getPort(); //訪問路徑 String path = uri.getPath(); //編碼路徑 String path1 = uri.getEncodedPath(); //query部分 String queryString = uri.getQuery(); //獲取參數值 String systemInfo = uri.getQueryParameter("system");
String id=uri.getQueryParameter("id");
System.out.println("host:" + host);
System.out.println("dataString:" + dataString);
System.out.println("id:" + id);
System.out.println("path:" + path);
System.out.println("path1:" + path1);
System.out.println("queryString:" + queryString);
}
}
}
調用方式:
網頁上調用
<a href="http://baidu:8080/news?system=pc&id=45464">test</a>
native調用
if (hasApplication()) { Intent action = new Intent(Intent.ACTION_VIEW); StringBuilder builder = new StringBuilder(); builder.append("http://baidu:8080/news?system=pc&id=45464"); action.setData(Uri.parse(builder.toString())); startActivity(action); } /** * 判斷是否安裝了應用 * @return true 為已經安裝 */ private boolean hasApplication() { PackageManager manager = mContext.getPackageManager(); Intent action = new Intent(Intent.ACTION_VIEW); action.setData(Uri.parse("http://")); List list = manager.queryIntentActivities(action, PackageManager.GET_RESOLVED_FILTER); return list != null && list.size() > 0; }