android 通过类名跳转activity,Activity跳转方式总结
一、顯式調用方法
方法一:
Intent intent=new Intent(本類,將要跳轉的類); //Intent intent=new Intent(MainActivity.this,JumpToActivity.class);
startActivity(intent);
方法二:
Intent intent2=new Intent();
intent2.setClass(本類,將要跳轉的類); // intent2.setClass(MainActivity.this,JumpToActivity.class);
startActivity(intent2);
方法三:(此方式可用于打開其它的應用)
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"));
二:隱式調用方法
通過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協議格式:
liujc://goods:8080/goodsDetail?goodsId=20170112
上面的路徑 Scheme、Host、port、path、query全部包含:
liujc代表該Scheme 協議名稱
goods代表Scheme作用于哪個地址域
goodsDetail代表Scheme指定的頁面
goodsId代表傳遞的參數
8080代表該路徑的端口號
URL Scheme如何使用:
在AndroidManifest.xml中對標簽增加設置Scheme:
android:name=".GoodsDetailActivity"
android:theme="@style/AppTheme">
獲取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);
}
總結
以上是生活随笔為你收集整理的android 通过类名跳转activity,Activity跳转方式总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android新年祝福代码,讯飞输入法发
- 下一篇: html转换成keynote,keyno