출처 - http://journae.springnote.com/pages/6730933
주의 :
<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);
}
}
출처 : http://stackoverflow.com/questions/2448213/how-to-implement-my-very-own-uri-schema-on-android/2448531#2448531
This is very possible; you define the URI schema in your AndroidManifest.xml, using the <data>
element. You setup an intent filter with the <data>
element filled out, and you'll be able to create your own schema. (More on intent filters and intent resolution here.)
Here's a short example:
<activityandroid:name=".MyUriActivity"><intent-filter><actionandroid:name="android.intent.action.VIEW"/><categoryandroid:name="android.intent.category.DEFAULT"/><categoryandroid:name="android.intent.category.BROWSABLE"/><dataandroid:scheme="myapp"android:host="path"/></intent-filter></activity>
As per how implicit intents work, you need to define at least one action and one category as well; here I picked VIEW as the action (though it could be anything), and made sure to add the DEFAULT category (as this is required for all implicit intents). Also notice how I added the category BROWSABLE - this is not necessary, but it will allow your URIs to be openable from the browser (a nifty feature).
You need to follow the standard rules for URIs via the W3C and such, which basically means: do not do this.
Android defines a Uri syntax for describing a generic Intent. There are methods on Intent for converting to and from this representation, such as: http://developer.android.com/reference/android/content/Intent.html#toUri(int)
So the way to do this is to use the normal facilities to describe an in your manifest for the kinds of intents you are going to handle with a particular component, especially defining an action name in your own namespace (com.mycompany.myapp.action.DO_SOMETHING or whatever). You can then make an Intent that matches your component, and use Intent.toUri() to get the URI representation of this. This can be placed in your link, and will then when pressed look for something that handles and and thus find your app. Note to be launched from the browser like this, the component's must handle the BROWSABLE category. (You don't need to have this in the Intent you put in the link, the browser will automatically add this in for you.)
Finally, you may want to set the package of the intent to your app with this: http://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String)
This is a newer feature in the platform, which allows you to direct link intents to only your app so that other applications can not intercept and handle them.
In summary: read the regular documentation on intents and intent filters (such as the NotePad tutorial, though you won't be using content: URIs here, probably just custom actions) and get your app working that way. Then you can make a browser link to launch your app in the same way, provided your intent-filter handles the BROWSABLE category.
First, to be able to start your app from link with custom scheme 'myapp' in browser / mail, set intent filter as follows.
<intent-filter>
<== AndroidMenifest.xml 의 내용
<actionandroid:name="android.intent.action.VIEW"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
<categoryandroid:name="android.intent.category.BROWSABLE"/>
<dataandroid:scheme="myapp"/>
</intent-filter>
and to parse queries in your link myapp://someaction/?var=str&varr=string
(the code is over simplified and has no error checking.)
// 소스코드 내용.
Intent intent = getIntent();// check if this intent is started via custom scheme link
if(Intent.ACTION_VIEW.equals(intent.getAction())){
Uri uri = intent.getData();
// may be some test here with your custom uriString
var= uri.getQueryParameter("var");// "str" is setString
varr = uri.getQueryParameter("varr");// "string" is set
}
'개발 > 안드로이드' 카테고리의 다른 글
SQLite 현재 날짜 얻기 (0) | 2012.07.04 |
---|---|
리스트뷰 섹션과 인덱스 만들기 (3) | 2012.07.02 |
안드로이드에서 강제로 터치 이벤트 발생시키기 (1) | 2012.06.14 |
스피너(Spinner) 사용 - 크기 및 모양 조작하기 (0) | 2012.05.25 |
Android USB Accessory (0) | 2012.05.23 |
댓글