웹뷰에서 tel: sms: 가 안됩니다
본문
웹뷰에서 tel: sms: 링크가 적용이 안되요
앱은 초보라 모르겠네요...전문가 분들 조언 부탁드려요
package com.hybrid.sample;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Process;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
private String mCurrentUrl;
private final static String MAIN_URL = "http://sir.kr";
// BACK 2번 클릭 시 종료 핸들러. 플래그
private Handler mHandler = new Handler();
private boolean mFlag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//스플래시
startActivity(new Intent(this, SplashActivity.class));
//웹뷰 설정
mWebView = (WebView) findViewById(R.id.web);
mWebView.getSettings().setJavaScriptEnabled(true);
//mWebView.setInitialScale(100);
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.setWebViewClient(new WebViewClientClass());
mWebView.loadUrl(MAIN_URL);
}
private class WebViewClientClass extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String overrideUrl) {
view.loadUrl(overrideUrl);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mWebView.getOriginalUrl().equalsIgnoreCase(MAIN_URL)) {
if (!mFlag) {
Toast.makeText(this, "뒤로 버튼을 한번 더 누르시면 종료됩니다.", Toast.LENGTH_SHORT).show(); // 종료안내 toast 를 출력
mFlag = true;
mHandler.sendEmptyMessageDelayed(0, 2000); // 2000ms 만큼 딜레이
return false;
} else {
// 앱 종료 code
moveTaskToBack(true);
finish();
android.os.Process.killProcess(Process.myPid());
}
} else {
// 뒤로 가기 실행
if (mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
}
}
return true;
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Otherwise allow the OS to handle it
else if (url.startsWith("tel:")) {
Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(tel);
return true;
}
else if (url.startsWith("mailto:")) {
String body = "Enter your Question, Enquiry or Feedback below:\n\n";
Intent mail = new Intent(Intent.ACTION_SEND);
mail.setType("application/octet-stream");
mail.putExtra(Intent.EXTRA_EMAIL, new String[]{"email address"});
mail.putExtra(Intent.EXTRA_SUBJECT, "Subject");
mail.putExtra(Intent.EXTRA_TEXT, body);
startActivity(mail);
return true;
}
return true;
}
}
답변 2
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
정리해서
if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:") || url.startsWith("mms:") || url.startsWith("mmsto:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent);
return true;
}
필요하신 분이 있을 것 같아...소스 올립니다.
package com.hybrid.sample;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Process;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
private String mCurrentUrl;
private final static String MAIN_URL = "http://food.wehappy.net/";
// BACK 2번 클릭 시 종료 핸들러. 플래그
private Handler mHandler = new Handler();
private boolean mFlag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//스플래시
startActivity(new Intent(this, SplashActivity.class));
//웹뷰 설정
mWebView = (WebView) findViewById(R.id.web);
mWebView.getSettings().setJavaScriptEnabled(true);
//mWebView.setInitialScale(100);
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.setWebViewClient(new WebViewClientClass());
mWebView.loadUrl(MAIN_URL);
}
private class WebViewClientClass extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String overrideUrl) {
if (overrideUrl.startsWith("tel:") || overrideUrl.startsWith("sms:") || overrideUrl.startsWith("smsto:") || overrideUrl.startsWith("mms:") || overrideUrl.startsWith("mmsto:") || overrideUrl.startsWith("mailto:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(overrideUrl));
startActivity(intent);
return true;
}
view.loadUrl(overrideUrl);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mWebView.getOriginalUrl().equalsIgnoreCase(MAIN_URL)) {
if (!mFlag) {
Toast.makeText(this, "뒤로 버튼을 한번 더 누르시면 종료됩니다.", Toast.LENGTH_SHORT).show(); // 종료안내 toast 를 출력
mFlag = true;
mHandler.sendEmptyMessageDelayed(0, 2000); // 2000ms 만큼 딜레이
return false;
} else {
// 앱 종료 code
moveTaskToBack(true);
finish();
android.os.Process.killProcess(Process.myPid());
}
} else {
// 뒤로 가기 실행
if (mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
}
}
return true;
}
}
답변을 작성하시기 전에 로그인 해주세요.