自我感覺這里的排版看著更舒服些。?Activity跳轉方式總結
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
android實現頁面跳轉、1、顯式調用方法
方法一:
Intent intent=new Intent(本類,將要跳轉的類); //Intent intent=new Intent(MainActivity.this,JumpToActivity.class);
intent跳轉錯誤、startActivity(intent);
方法二:
Intent intent2=new Intent();
android的activity。intent2.setClass(本類,將要跳轉的類); // intent2.setClass(MainActivity.this,JumpToActivity.class);
startActivity(intent2);
方法三:(此方式可用于打開其它的應用)
android簡單頁面跳轉、Intent intent2=new Intent();
intent2.setComponent(new ComponentName(MainActivity.this, JumpToActivity.class));
startActivity(intent2);
component,目標組件的包或類名稱(完整類名):
在使用component進行匹配時,一般采用以下幾種形式:
intent.setComponent(new?ComponentName(getApplicationContext(),?JumpToActivity.class));
intent.setComponent(new?ComponentName(getApplicationContext(),?"com.liujc.test.JumpToActivity"));
intent.setComponent(new?ComponentName("com.liujc.test",?"com.liujc.test.JumpToActivity"));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2:隱式調用方法
通過action跳轉:
Intent?intent?=?new?Intent();
intent.setAction("con.liujc.test.jump");
startActivity(intent);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
需要將要跳轉到的Activity在AndroidManifest.xml中設置action:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
通過Scheme跳轉協議跳轉:
android中的scheme是一種頁面內跳轉協議,是一種非常好的實現機制,通過定義自己的scheme協議,可以非常方便跳轉app中的各個頁面;通過scheme協議,服務器可以定制化告訴App跳轉那個頁面,可以通過通知欄消息定制化跳轉頁面,可以通過H5頁面跳轉頁面等。
URL Scheme協議格式:
scheme://host:port/path 模式://主機:端口/路徑
完整的URL Scheme協議格式:liujc://goods:8080/goodsDetail?goodsId=20170112
上面的路徑 Scheme、Host、port、path、query全部包含:
liujc代表該Scheme 協議名稱
goods代表Scheme作用于哪個地址域
goodsDetail代表Scheme指定的頁面
goodsId代表傳遞的參數
8080代表該路徑的端口號
URL Scheme如何使用:
在AndroidManifest.xml中對標簽增加設置Scheme:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
獲取Scheme跳轉的參數:
Uri?uri?=?getIntent().getData();
if?(uri?!=?null)?{
//?完整的url信息
String?url?=?uri.toString();
Log.e(TAG,?"url:?"?+?uri);
//?scheme部分
String?scheme?=?uri.getScheme();
Log.e(TAG,?"scheme:?"?+?scheme);
//?host部分
String?host?=?uri.getHost();
Log.e(TAG,?"host:?"?+?host);
//port部分
int?port?=?uri.getPort();
Log.e(TAG,?"host:?"?+?port);
//?訪問路勁
String?path?=?uri.getPath();
Log.e(TAG,?"path:?"?+?path);
List?pathSegments?=?uri.getPathSegments();
//?Query部分
String?query?=?uri.getQuery();
Log.e(TAG,?"query:?"?+?query);
//獲取指定參數值
String?goodsId?=?uri.getQueryParameter("goodsId");
Log.e(TAG,?"goodsId:?"?+?goodsId);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
調用方式:
網頁上:(使用系統自帶瀏覽器或者谷歌瀏覽器)
打開商品詳情
原生調用:
Intent?intent?=?new?Intent(Intent.ACTION_VIEW,Uri.parse("liujc://goods:8080/goodsDetail?goodsId=20170112"));
startActivity(intent);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如何判斷一個Scheme是否有效,有效后再啟動:
PackageManager?packageManager?=?getPackageManager();
Intent?intent?=?new?Intent(Intent.ACTION_VIEW,?Uri.parse("liujc://goods:8080/goodsDetail?goodsId=20170112"));
List?activities?=?packageManager.queryIntentActivities(intent,?0);
boolean?isValid?=?!activities.isEmpty();
if?(isValid)?{
startActivity(intent);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
小奮斗文章
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态