Skip to content

Instantly share code, notes, and snippets.

@ueno-yuhei
Created February 1, 2015 06:14
Show Gist options
  • Select an option

  • Save ueno-yuhei/5e7b0e403afb248fe532 to your computer and use it in GitHub Desktop.

Select an option

Save ueno-yuhei/5e7b0e403afb248fe532 to your computer and use it in GitHub Desktop.
Webview表示中に、mail telリンクを押された時にIntentを投げるようにする
package jp.logiclogic.jikken;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends FragmentActivity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
// 電話発信ダイアログ
TelDialogFragment dialogFragment = TelDialogFragment.newInstance(url);
dialogFragment.show(MainActivity.this.getSupportFragmentManager(), "tel_dialog");
return true;
}else if(url.startsWith("mailto:")){
Intent intent = new Intent(Intent.ACTION_SENDTO,Uri.parse(url));
startActivity(intent);
return true;
}
return true;
}
});
mWebView.loadUrl("http://");;
}
// 電話確認ダイアログ
public static class TelDialogFragment extends DialogFragment {
public static TelDialogFragment newInstance(String telNumber){
TelDialogFragment frag = new TelDialogFragment();
Bundle args = new Bundle();
args.putString("tel_number", telNumber);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("確認");
builder.setMessage("発信しますか?");
builder.setPositiveButton("はい",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String telNumber = (String) getArguments().get("tel_number");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(telNumber));
startActivity(intent);
}
});
builder.setNegativeButton("いいえ",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
});
return builder.create();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment