1. 程式人生 > >Activity跳轉方式總結

Activity跳轉方式總結

一、顯式呼叫方法

  1. 方法一:

    Intent intent=new Intent(本類,將要跳轉的類); //Intent intent=new Intent(MainActivity.this,JumpToActivity.class);
    startActivity(intent);
  2. 方法二:

    Intent intent2=new Intent();
    intent2.setClass(本類,將要跳轉的類); // intent2.setClass(MainActivity.this,JumpToActivity.class);
    startActivity(intent2);
  3. 方法三:(此方式可用於開啟其它的應用)

    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"));

二:隱式呼叫方法

  1. 通過action跳轉:
Intent intent = new Intent();  
intent.setAction("con.liujc.test.jump");  
startActivity(intent);

需要將要跳轉到的Activity在AndroidManifest.xml中設定action:

<activity android:name=".JumpToActivity" >  
    <intent-filter>  
        <action android:name="con.liujc.test.jump"/>  
        <category android:name="android.intent.category.DEFAULT" />  
    </intent-filter>  
</activity>
  1. 通過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如何使用:

    1. 在AndroidManifest.xml中對標籤增加設定Scheme:
<activity
            android:name=".GoodsDetailActivity"
            android:theme="@style/AppTheme">
            <!--要想在別的App上能成功調起App,必須新增intent過濾器-->
            <intent-filter>
                <!--協議部分,隨便設定-->
                <data android:scheme="liujc" android:host="goods" android:path="/goodsDetail" android:port="8080"/>
                <!--下面這幾行也必須得設定-->
                <category android:name="android.intent.category.DEFAULT"/>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.BROWSABLE"/>
            </intent-filter>
        </activity>
  1. 獲取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<String> pathSegments = uri.getPathSegments();
    // Query部分
    String query = uri.getQuery();
    Log.e(TAG, "query: " + query);
    //獲取指定引數值
    String goodsId = uri.getQueryParameter("goodsId");
    Log.e(TAG, "goodsId: " + goodsId);
}
  1. 呼叫方式:
    網頁上:(使用系統自帶瀏覽器或者谷歌瀏覽器)
    <a href="liujc://goods:8080/goodsDetail?goodsId=20170112">開啟商品詳情</a>
    原生呼叫:
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("liujc://goods:8080/goodsDetail?goodsId=20170112")); 
startActivity(intent);
  1. 如何判斷一個Scheme是否有效,有效後再啟動:
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("liujc://goods:8080/goodsDetail?goodsId=20170112"));
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isValid = !activities.isEmpty();
if (isValid) {
    startActivity(intent);
}