在Android中scheme是指Uri連接 :以前的部分如 test://login scheme 即爲test。 可能不少人要爲URI是怎麼定義的呢?具體看URI的定義html
String url="test://video:8080/event?title="scheme"&url="www.xiaomi.com" " Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url)); startActivity(intent);
<activity android:name=".UriActivity"> <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="test" android:host="video" /> </intent-filter> </activity>
這樣就能夠在UriActivity 中接收url 跳轉相應的動做以及數據。android
> Uri data=getIntent().getData(); > data.getScheme(); > data.getHost(); > data.getPort(); > data.getPath(); > data.getQueryParameter("title");
若是咱們在每一個activity 中配置scheme ,host,path 顯得特別麻煩。這個時候咱們須要一箇中轉的activity來處理全部的scheme的跳轉。根據相應的host,path 來跳轉到相應的activity作進一步處理。ide
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); Uri data = intent.getData(); String host = data.getHost(); if(host.equals("list")){ if ("/near".equals(data.getPath())) {//yhouse://list/near intentTransfer = new Intent(this, NearbyActivity.class); } else if ("/new".equals(data.getPath())) {//yhouse://list/new intentTransfer = new Intent(this, NewSkuActivity.class); } else if ("/live".equals(data.getPath())) { intentTransfer = new Intent(this, LiveActivity.class); } else if ("/neighbor".equals(data.getPath())) { intentTransfer = new Intent(this, NeighborActivity.class); } else if ("/talent".equals(data.getPath())) { intentTransfer = new Intent(this, TalentListActivity.class); } else if ("/guide".equals(data.getPath())) { intentTransfer = new Intent(this, GuideActivity.class); } else if ("/experience".equals(data.getPath())) { intentTransfer = new Intent(this, PlayerActivity.class); }else if ("/latest".equals(data.getPath())) { intentTransfer = new Intent(this, CikeActivity.class); } else if ("/message".equals(data.getPath())) { intentTransfer = new Intent(this, HomeActivity.class); } else if ("/message/like".equals(data.getPath())) { intentTransfer = new Intent(this, MessageDetailActivity.class); } else if ("/message/comment".equals(data.getPath())) { intentTransfer = new Intent(this, MessageDetailActivity.class); } else if ("/message/follow".equals(data.getPath())) { intentTransfer = new Intent(this, MessageDetailActivity.class); } else if ("/message/system".equals(data.getPath())) { intentTransfer = new Intent(this, SystemNoticeActivity.class); } else { intentTransfer = new Intent(this, HomeActivity.class); } }else if ("my-member".equals(host)) { intentTransfer = new Intent(this, MemberInfoActivity.class); } else if ("event".equals(host)) { intentTransfer = new Intent(this, HuodongDetailActivity.class); } else if ("booking-list".equals(host)) { intentTransfer = new Intent(this, OrderManagerActivity.class); } else if ("my-party".equals(host)) { intentTransfer = new Intent(this, OrderManagerActivity.class); intentTransfer.putExtra("type", OrderManagerActivity.PARTY); } else if ("order".equals(host)) { intentTransfer = new Intent(this, BookingManageDetailActivity.class); } else if ("meal".equals(host)) { intentTransfer = new Intent(this, MealDetailActivity.class); } }
你可能發現大量的if else 語句不是很是好的選擇。你能夠採用swith case 看不起來更清爽一些。可是使用HashMap 將是咱們比較好的選擇。ui
HashMap處理 首發定義一個枚舉 SchemeEnum和類SchemeManagerthis
>``` public enum ShemeEnum { > Nearby("/near", NearbyActivity.class), NewSku("/new", NewSkuActivity.class),Live("/live", LiveActivity.class), > NeighborActivity("/neighbor", NeighborActivity.class),Talent("/talent", NeighborActivity.class),Guide("/guide", GuideActivity.class), > Player("/experience", PlayerActivity.class),News("/news", NewsActivity.class),Latest("latest",CikeActivity.class), > Message("/message",NeighborActivity.class),Like("/message/like",NeighborActivity.class), > Comment("/message/comment",NeighborActivity.class),Follow("/message/follow",NeighborActivity.class), > System("/message/system",NeighborActivity.class),event("event",HomeActivity.class); > private String key; > private Class<?> value; > > private ShemeEnum(String key, Class<?> value) { > this.value = value; > this.key = key; > } > > public String getKey() { > return key; > } > > public Class<?> getValue() { > return value; > } > } >``` > public class SchemeManager { > private static HashMap<String, Class> map = new HashMap(); > private static SchemeManager scheme; > > public static HashMap initScheme() { > ShemeEnum[] values = ShemeEnum.values(); > int len=values.length; > for (int i = 0; i <len ; i++) { > map.put(values[i].getKey(), values[i].getValue()); > } > return map; > } > > public static HashMap<String, Class> getMap() { > return map; > } > }
scheme 轉化的Activity 將整理成url
>``` > public class UriActivity extends Activity { > private SApplication sApplication; > > @Override > protected void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.activity_test); > Log.e("---UriActivity---", getClass().getName()); > Uri data = getIntent().getData(); > sApplication = (SApplication) getApplication(); > Intent intent = getIntent(); > if (intent == null || data == null) finish(); > Intent intentTransfer = null; > String host = data.getHost(); > if (host.equals("list")) { > intentTransfer = getIntentTransfer(data.getPath()); > } else { > intentTransfer = getIntentTransfer(host); > } > if (intentTransfer != null) { > intentTransfer.setData(data); > startActivity(intentTransfer); > } > finish(); > } > > private Intent getIntentTransfer(String path) { > Object clazz = sApplication.getMap().get(path); > Intent intentTransfer; > if (clazz != null) { > intentTransfer = new Intent(this, (Class<?>) clazz); > } else { > intentTransfer = new Intent(this, HomeActivity.class); > } > return intentTransfer; > } > } > ``
感受UriActivity 變的更簡潔。每次新加scheme 只須要在對應的枚舉中添加相應的scheme 和Activity名稱既能夠正常的跳轉。翻譯
若是你想在跳轉的途中或者我直接操做相應的某個操做(顯示彈框,去分享等等) 你首先要重寫 WebViewClient shouldOverrideUrlLoading(WebView view, String url); 來實現攔截的目的。rest
this.setWebViewClient(new WebViewClient() { String failedUrl; @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //TODO 判斷是否要攔截。若是須要作你的攔截以後的動做。 //不須要直接跳轉(看第3條)。 return true; } }); }
這樣經過H5 來打開相應的activity而且傳遞參數的相關內容介紹完。下一節。咱們將講解Activity 傳遞給html 數據,以及調用相關的方法。code