android 通过类名跳转activity,Activity跳转方式总结
一、顯式調(diào)用方法
方法一:
Intent intent=new Intent(本類,將要跳轉(zhuǎn)的類); //Intent intent=new Intent(MainActivity.this,JumpToActivity.class);
startActivity(intent);
方法二:
Intent intent2=new Intent();
intent2.setClass(本類,將要跳轉(zhuǎn)的類); // intent2.setClass(MainActivity.this,JumpToActivity.class);
startActivity(intent2);
方法三:(此方式可用于打開其它的應(yīng)用)
Intent intent2=new Intent();
intent2.setComponent(new ComponentName(MainActivity.this, JumpToActivity.class));
startActivity(intent2);
component,目標(biāo)組件的包或類名稱(完整類名):
在使用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"));
二:隱式調(diào)用方法
通過action跳轉(zhuǎn):
Intent intent = new Intent();
intent.setAction("con.liujc.test.jump");
startActivity(intent);
需要將要跳轉(zhuǎn)到的Activity在AndroidManifest.xml中設(shè)置action:
通過Scheme跳轉(zhuǎn)協(xié)議跳轉(zhuǎn):
android中的scheme是一種頁面內(nèi)跳轉(zhuǎn)協(xié)議,是一種非常好的實現(xiàn)機制,通過定義自己的scheme協(xié)議,可以非常方便跳轉(zhuǎn)app中的各個頁面;通過scheme協(xié)議,服務(wù)器可以定制化告訴App跳轉(zhuǎn)那個頁面,可以通過通知欄消息定制化跳轉(zhuǎn)頁面,可以通過H5頁面跳轉(zhuǎn)頁面等。
URL Scheme協(xié)議格式:
liujc://goods:8080/goodsDetail?goodsId=20170112
上面的路徑 Scheme、Host、port、path、query全部包含:
liujc代表該Scheme 協(xié)議名稱
goods代表Scheme作用于哪個地址域
goodsDetail代表Scheme指定的頁面
goodsId代表傳遞的參數(shù)
8080代表該路徑的端口號
URL Scheme如何使用:
在AndroidManifest.xml中對標(biāo)簽增加設(shè)置Scheme:
android:name=".GoodsDetailActivity"
android:theme="@style/AppTheme">
獲取Scheme跳轉(zhuǎn)的參數(shù):
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);
//獲取指定參數(shù)值
String goodsId = uri.getQueryParameter("goodsId");
Log.e(TAG, "goodsId: " + goodsId);
}
調(diào)用方式:
網(wǎng)頁上:(使用系統(tǒng)自帶瀏覽器或者谷歌瀏覽器)
打開商品詳情
原生調(diào)用:
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);
}
總結(jié)
以上是生活随笔為你收集整理的android 通过类名跳转activity,Activity跳转方式总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android新年祝福代码,讯飞输入法发
- 下一篇: html转换成keynote,keyno