Android瀏覽器打開本地app前端同學的兩種配置方式Url scheme和Android intent

這裏也有個免費的三方sdk

騰訊的AppLink接入方式

百度的AppLink接入方式

Facebook的AppLink接入方式

google官方文檔

更新分割線


首先說一下,在我和前端同學調試時,使用第一種方式配置時,不能夠啓動本地app,第二種方式能夠啓動app。可能是手機不支持第一種方式啓動,具體原因沒找到。


這部分是Android小夥伴要做的事情

喚醒APP

首先兩種方式都要在AndroidManifest 裏配置

<activity               android:name=".ui.detail.SchemeCenterActivity"          android:theme="@android:style/Theme.NoDisplay">
<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="Myapp"/>
 </intent-filter>
</activity>

然後在Activity裏獲取

    
  public class SchemeCenterActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent i_getvalue = getIntent();
        String action = i_getvalue.getAction();

        if (Intent.ACTION_VIEW.equals(action)) {

            Uri data = i_getvalue.getData();
            String host = data.getHost();
            Log.i(this,data+"");
            Log.i(this,host+"");//548,後邊會提到這個參數是那裏傳入的
            }
        }

    }

}
   

我這裏本身做的跳轉到我指定的界面,便於理解簡化了,如果有需要,我在最後貼出代碼


下邊說幾個獲取的方式

假設用戶點擊一個鏈接到http://twitter.com/status/1234

在Activity裏可以這麼獲取

Uri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "twitter.com"
List<String> params = data.getPathSegments();
String first = params.get(0); // "status"
String second = params.get(1); // "1234"

當然了我這裏只配置了scheme,因爲我沒有太多參數要傳,這裏的scheme 是必須配置的。

如果有需求配置host 可以在data標籤下添加這裏還按twitter來舉例 data標籤就變成下邊的樣子了,
<data android:scheme="http" android:host="twitter.com"/>


下邊是前端同學配置的方式

我的問題主要是前端同學使用第一種方式時,我不能啓動app
所以我選擇了第二種方式

這裏說下第一種方式

Url scheme

Url scheme是iOS,Android平臺都支持,只需要原生APP開發時註冊scheme, 那麼用戶點擊到此類鏈接時,會自動跳到APP。比如

<!-- 打開twitter -->
<a href="http://twitter.com">打開APP</a>
<!-- 打開我上邊的應用 -->
<a href="Myapp://548">打開我的應用</a>

上述的鏈接,有些手機不支持Scheme所以有了第二種方式


第二種方式

Android intent

這是Android平臺獨有的,使用方式如下

intent:
   HOST/URI-path // Optional host 
   #Intent; 
      package=[string]; 
      action=[string]; 
      category=[string]; 
      component=[string]; 
      scheme=[string]; 
   end;

這裏的HOST/URI-path, 與普通http URL 的host/path書寫方式相同, package是Android app的包名,其它參數如action、category、component就填你AndroidManifest裏對應的配置 比如打開我的應用,代碼如下

<a href="intent://548#Intent;scheme=CampusLife;package=cn.mytest.app;end">打開APP</a>

最後貼一下我的activity代碼給需要的朋友

    
public class SchemeCenterActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent i_getvalue = getIntent();
        String action = i_getvalue.getAction();

        if (Intent.ACTION_VIEW.equals(action)) {

            Uri data = i_getvalue.getData();

            if (data != null) {

                String host = data.getHost();
                Intent intent = new Intent(this, DetailActivity.class);
                intent.putExtra(DetailActivity.EXTRA_ID, host);
                startActivity(intent);
                finish();
            }
        }

    }

}
   

我這裏是將獲取的548傳給我的下一個界面DetailActivity 就可以了所以沒有配置其他的。


希望這篇能夠給移動端/前端小夥伴在配合時提供幫助。

如果哪裏表述的不清楚,希望大家指出來,大家共同進步。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章