Android

android url scheme 추가하기(custom scheme 생성)

Sunny's 2011. 7. 12. 08:58

주의 :

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
위와 같은 activity의 경우

<data android:scheme="myapp" />

와 같은 설정을 추가해도 scheme을 인식하지 못한다..(내가 테스트한 바로는..)

따라서 android 단말기의 브라우저에서 해당 scheme( "myapp") 을 인식하고 찾아가게 하기 위해서는

이 페이지의 맨 하단의 내용과 같은 설정과 코딩 부분이 들어가면 된다.

참조 코드 전문 :

[AndroidManifest.xml]

<activity android:name=".Activity.XenoboxCustomDataSchemeActivity">
<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>

[assets 폴더]
[test.xml]

<html>

<head>
</head>

<body>
<a href="myapp://someaction?var=str&varr=string">Foo</a>
</body>

</html>


[XenoboxCodeLabAppCustomScheme extends Activity]

@Override
public void onCreate(Bundle savedInstanceState) {
//.
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/test.html");
mWebView.setWebViewClient(new XenoboxWebViewClient());
}


protected class XenoboxWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("myapp:")) {
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
return true;
}
return false;

}
}

[XenoboxCustomDataSchemeActivity extends Activity]

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//.
Intent intent = getIntent();
if(Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
String var = uri.getQueryParameter("var"); //. "str" is set
String varr = uri.getQueryParameter("varr"); //. "string" is set

Log.i("xenobox", "var=" + var + ", " + "varr=" + varr);
}
}