在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