브라우저에서 링크를 가로 채서 안드로이드 앱을 엽니 다
-
05-07-2019 - |
문제
브라우저가 열 수 있도록 주어진 패턴의 URL을 클릭 할 때 앱에 링크를 열도록 프롬프트 할 수 있기를 원합니다. 사용자가 브라우저 또는 이메일 클라이언트의 웹 페이지 또는 새로 채굴 된 앱의 웹 뷰에있을 때일 수 있습니다.
예를 들어, 전화의 어느 곳에서나 YouTube 링크를 클릭하면 YouTube 앱을 열 수 있습니다.
내 앱에 대해 어떻게 달성합니까?
해결책
Android.intent.action.view의 범주를 사용하십시오 Android.intent.category.browsable.
Romain Guy 's에서 Photostream 앱 AndroidManifest.xml,
<activity
android:name=".PhotostreamActivity"
android:label="@string/application_name">
<!-- ... -->
<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="http"
android:host="flickr.com"
android:pathPrefix="/photos/" />
<data android:scheme="http"
android:host="www.flickr.com"
android:pathPrefix="/photos/" />
</intent-filter>
</activity>
일단 안으로 들어가면 활동, 당신은 행동을 찾아야하고, 당신이 건네 준 URL로 무언가를해야합니다. 그만큼 Intent.getData()
메소드는 URI를 제공합니다.
final Intent intent = getIntent();
final String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action)) {
final List<String> segments = intent.getData().getPathSegments();
if (segments.size() > 1) {
mUsername = segments.get(1);
}
}
그러나이 앱이 약간의 날짜가 조금 떨어지고 있다는 점에 유의해야하므로이를 달성하는 더 나은 방법이 있음을 알 수 있습니다.
다른 팁
URL의 일부 라이브러리가 자동으로 구문 분석 매개 변수가 있습니다.
~와 같은
https://github.com/airbnb/deeplinkdispatch
&&
https://github.com/mzule/activityrouter
나중에는 나에 의해 썼다. 항상 문자열이 아니라 주어진 유형으로 매개 변수를 구문 분석 할 수 있습니다.
예시
@Router(value = "main/:id" intExtra = "id")
...
int id = getIntent().getInt("id", 0);
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
setUrlparams(url);
if (url.indexOf("pattern") != -1) {
// do something
return false;
} else {
view.loadUrl(url);
}
return true;
}
}
제휴하지 않습니다 StackOverflow