웹뷰 작업시작
마스터욱
0
45
0
0
2017-02-10 16:15:28
헤헤 "랜덤 이성 채팅" 그럴싸한가?
아이콘이 기본이라서 좀 없어 보이긴 하지만... 아이콘은 이걸 쓸 예정이다.
어디서 많이 본거 같다... 그렇다... 어디서 많이 본거에서 색깔만 바꿨다... 나 지급 급해...
아래소스는 웹뷰에서 뒤로가기, 파일업로드 까지 되도록 모두 구현된 것이다.
MainActivity.java
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | package com.example.wook.myapplication; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { private static final int FILECHOOSER_RESULTCODE = 1; private final static int FILECHOOSER_LOLLIPOP_REQ_CODE = 2; private ValueCallback<Uri> mUploadMessage = null; private ValueCallback<Uri[]> filePathCallbackLollipop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //타이틀바 없애기 //requestWindowFeature(Window.FEATURE_NO_TITLE);
//setTheme(android.R.style.Theme_NoTitleBar_Fullscreen); setContentView(R.layout.activity_main); WebView WebView01 = (WebView) findViewById(R.id.webView01); WebView01.setWebChromeClient(new WebChromeClient(){ // For Android 3.0- @SuppressWarnings("unused") public void openFileChooser(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); //i.setType("image/*"); i.setType("*/*"); startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); } // For Android 3.0+ @SuppressWarnings("unused") public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); } // For Android 4.1+ @SuppressWarnings("unused") public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); //i.setType("image/*"); i.setType("*/*"); startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); } // For Android 5.0+ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { Log.d("MainActivity", "5.0+"); if (filePathCallbackLollipop != null) { filePathCallbackLollipop.onReceiveValue(null); filePathCallbackLollipop = null; } filePathCallbackLollipop = filePathCallback; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_LOLLIPOP_REQ_CODE); return true; } }); WebView01.setWebViewClient(new WebViewClient()); WebSettings webSettings = WebView01.getSettings(); webSettings.setJavaScriptEnabled(true); WebView01.loadUrl("http://story84.com/art/chating"); } //파일첨부 @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } else if (requestCode == FILECHOOSER_LOLLIPOP_REQ_CODE) { if (filePathCallbackLollipop == null) return ; filePathCallbackLollipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent)); filePathCallbackLollipop = null; } } public class BackPressCloseHandler {
private long backKeyPressedTime = 0;
private Toast toast;
private Activity activity;
public BackPressCloseHandler(Activity context) {
this.activity = context;
}
public void onBackPressed() {
if (System.currentTimeMillis() > backKeyPressedTime + 2000) {
backKeyPressedTime = System.currentTimeMillis();
showGuide();
return;
}
if (System.currentTimeMillis() <= backKeyPressedTime + 2000) {
activity.finish();
toast.cancel();
}
}
public void showGuide() {
toast = Toast.makeText(activity, "뒤로 버튼을 한번 더 누르시면 종료됩니다.", Toast.LENGTH_SHORT);
toast.show();
}
}
//뒤로가기 기능
BackPressCloseHandler backPressCloseHandler = new BackPressCloseHandler(this);
@Override
public void onBackPressed() {
WebView WebView01 = (WebView) findViewById(R.id.webView01);
//Log.d("wook", WebView01.getOriginalUrl())
if (WebView01.getOriginalUrl().equalsIgnoreCase(WebUrl)) {
backPressCloseHandler.onBackPressed();
}else if(WebView01.canGoBack()){
WebView01.goBack();
}else{
backPressCloseHandler.onBackPressed();
}
} } | cs |
manifasts > AndroidManifast.xml
퍼미션 두줄 추가~
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.jai.wook.jai">
- <uses-permission android:name="android.permission.INTERNET"></uses-permission>
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
res > layout > Activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <WebView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/webView01"
- >
- </WebView>
- </LinearLayout>
res > values > styles.xml
- <resources>
- <!-- Base application theme. -->
- <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
- <!-- Customize your theme here. -->
- <item name="colorPrimary">@color/colorPrimary</item>
- <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
- <item name="colorAccent">@color/colorAccent</item>
- <item name="windowActionBar">false</item>
- <item name="windowNoTitle">true</item>
- </style>
- </resources>