레이블이 url scheme인 게시물을 표시합니다. 모든 게시물 표시
레이블이 url scheme인 게시물을 표시합니다. 모든 게시물 표시

2014년 3월 18일 화요일

[android] url scheme 사용하기 (웹에서 앱 호출 및 앱에서 앱 호출)

[호출될 앱]
다른 앱이나 웹에서 호출될 앱의 AndroidManifest.xml 파일을 열어 원하는 activity에 아래와 같이 빨간 부분을 추가 한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<activity
            android:name=".testUrlScheme"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <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:host="호스트이름" android:scheme="스키마이름" />
            </intent-filter>
        </activity>

[testUrlScheme.java]
1
2
3
4
5
6
7
8
if(getIntent()!=null){
        Uri uri = getIntent().getData();
        if(uri != null)
        { 
            String param1 = uri.getQueryParameter("param1");
            String param2 = uri.getQueryParameter("param2");
        }
}


[앱에서 호출]
1
2
3
4
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setdata(Uri.parse("스키마://호스트?param1=test&param2=test2"));
startActivity(intent);


[웹에서 호출]
<a href="스키마://호스트?param1=test&param2=test2"> urlscheme </a>


[앱이 다시실행 될 때 호출]
1
2
3
4
5
6
7
8
@Override
    protected void onNewIntent(Intent intent) {
        // TODO Auto-generated method stub
        super.onNewIntent(intent);
        if(intent!=null){
            Uri uri = intent.getData();
        }
    }