Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- kotlin
- web
- traits
- Android
- sprintf
- sha256
- AES
- coroutines
- go
- ChromeTab
- SHA1
- haskell
- Scala
- c++
- CustomTab
- Observer
- Clojure
- WebView
- ranges
- Functional
- type_traits
- RAII
- Reflect
- design pattern
- SHA512
- program
- stringprintf
- async
- Chrono
- template
Archives
- Today
- Total
프로그래밍 검색 블로그
안드로이드 WebView 대신 Chrome Tab 사용 본문
간단한 웹 브라우징 기능에 대해서는 WebView 대신 Chrome Tab 기능도 사용할 수 있어 소개 한다
최신 안드로이드 폰에서는 크롬이 강제적으로 설치되어 있어서 무난하게 사용할 수 있을것이다
support 라이브러리에 맞는 버젼으로 build.gradle에 추가
1 | implementation 'com.android.support:customtabs:28.0.0' | cs |
여러가지 방법으로 CustomTab을 사용이 가능하지만 몇가지 이벤트를 전달받을 수 있도록
ServiceConnection 부분으로 사용한다
1 2 3 4 5 6 7 8 9 | @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CustomTabsClient.bindCustomTabsService( getApplicationContext() , "com.android.chrome", tabConnection); } | cs |
tabConnection 구현
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | private CustomTabsServiceConnection tabConnection = new CustomTabsServiceConnection() { @Override public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient customTabsClient) { CustomTabsSession session = customTabsClient.newSession(null); CustomTabsIntent.Builder customTabsIntentBuilder = new CustomTabsIntent.Builder(session); CustomTabsIntent customTabsIntent = customTabsIntentBuilder.build(); customTabsIntent.intent.setPackage("com.android.chrome"); customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); customTabsIntent.launchUrl(getApplicationContext(), Uri.parse("https://psbs.tistory.com")); } @Override public void onServiceDisconnected(ComponentName name) { } }; | cs |
새로운 크롬과의 연결 session을 만들어서 launchUrl을 하는 방식으로 웹페이지 호출이 가능하다
로드 시작 이벤트, 로드 끝 이벤트를 받고 싶다면 newSesson으로 넣는 null 대신 CustomTabsCallback을 호출하는것으로 가능한데
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | CustomTabsSession session = customTabsClient.newSession(new CustomTabsCallback(){ @Override public void onNavigationEvent(int navigationEvent, Bundle extras) { super.onNavigationEvent(navigationEvent, extras); switch (navigationEvent) { case CustomTabsCallback.NAVIGATION_STARTED: Log.d("psbs", "NAVIGATION_STARTED"); break; case CustomTabsCallback.NAVIGATION_FINISHED: Log.d("psbs", "NAVIGATION_FINISHED"); break; case CustomTabsCallback.NAVIGATION_FAILED: Log.d("psbs", "NAVIGATION_FAILED"); break; case CustomTabsCallback.NAVIGATION_ABORTED: Log.d("psbs", "NAVIGATION_ABORTED"); break; } } }); |
위의 session 생성을 이것으로 대체하는것으로 이벤트를 확인 가능해진다
CustomTab의 메뉴 등을 추가를 위해서는 다음과 같은 코드로 추가/이벤트 핸들링이 가능하다
1 2 3 4 5 | String menuItemTitle = "my menu"; Intent actionIntent = new Intent( getApplicationContext(), MainActivity.class); PendingIntent menuItemPendingIntent = PendingIntent.getActivity( getApplicationContext(), 1, actionIntent, 0); | cs |
최종 전체 소스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | package org.rudtyz.webview; import android.app.PendingIntent; import android.content.ComponentName; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.support.customtabs.CustomTabsCallback; import android.support.customtabs.CustomTabsClient; import android.support.customtabs.CustomTabsIntent; import android.support.customtabs.CustomTabsServiceConnection; import android.support.customtabs.CustomTabsSession; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private CustomTabsServiceConnection tabConnection = new CustomTabsServiceConnection() { @Override public void onCustomTabsServiceConnected(ComponentName componentName, final CustomTabsClient customTabsClient) { CustomTabsSession session = customTabsClient.newSession(new CustomTabsCallback(){ @Override public void onNavigationEvent(int navigationEvent, Bundle extras) { super.onNavigationEvent(navigationEvent, extras); switch (navigationEvent) { case CustomTabsCallback.NAVIGATION_STARTED: Log.e("psbs", "NAVIGATION_STARTED"); break; case CustomTabsCallback.NAVIGATION_FINISHED: Log.e("psbs", "NAVIGATION_FINISHED"); break; case CustomTabsCallback.NAVIGATION_FAILED: Log.e("psbs", "NAVIGATION_FAILED"); break; case CustomTabsCallback.NAVIGATION_ABORTED: Log.e("psbs", "NAVIGATION_ABORTED"); break; } } }); CustomTabsIntent.Builder customTabsIntentBuilder = new CustomTabsIntent.Builder(session); String menuItemTitle = "my menu"; Intent actionIntent = new Intent( getApplicationContext(), MainActivity.class); PendingIntent menuItemPendingIntent = PendingIntent.getActivity( getApplicationContext(), 1, actionIntent, 0); customTabsIntentBuilder.addMenuItem(menuItemTitle, menuItemPendingIntent); CustomTabsIntent customTabsIntent = customTabsIntentBuilder.build(); customTabsIntent.intent.setPackage("com.android.chrome"); customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); customTabsIntent.launchUrl(getApplicationContext(), Uri.parse("https://psbs.tistory.com")); } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CustomTabsClient.bindCustomTabsService( getApplicationContext() , "com.android.chrome", tabConnection); } } | cs |
'Android' 카테고리의 다른 글
코틀린 파일 MD5 SHA1 SHA256 SHA512 (0) | 2018.05.30 |
---|---|
코틀린 비동기 async (0) | 2018.02.16 |
안드로이드 SQLite 사용 Create, Index (0) | 2018.02.12 |
안드로이드 REST API / JSON 파싱 (0) | 2017.12.31 |
안드로이드 웹뷰 성능 향상 (0) | 2017.12.30 |
Comments