私はアンドロイド開発の初心者です。以下に、メインの activity.java コードを示します。モバイルデバイスでインターネット接続またはwifiが機能しない場合の私の要件は、次のことを実行することです
- インターネット接続が機能していない場合は、アプリケーションの開始時にスプラッシュ スクリーン バックグラウンドを表示し、トリップアドバイザーのこのスクリーンショットのようなダイアログ ボックスまたはアラートを表示します。この画像とスクリーンショットを確認してください。キャンセル ボタンを押すと、ダイアログ ボックスが消え、スプラッシュ スクリーンのみが表示されます。もう一度押すと、インターネットへの接続を実行します。
私のコードでは、コード購入ダイアログボックスを使用していますが、購入機能は必要ありません。これは私の要件ではありません。私はトリップアドバイザーとまったく同じように欲しいです。誰かが私のコードを編集して、どこで変更を加えるかを示してくれれば、私にとっては簡単です。
package com.example.edarabia;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity{
WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = (WebView) findViewById(R.id.webview);
if(isNetworkConnected() == true){
mywebview.getSettings().setJavaScriptEnabled(true);
mywebview.setWebViewClient(new myWebClient());
mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");
mywebview.getSettings().setBuiltInZoomControls(true);
mywebview.getSettings().setLoadWithOverviewMode(false);
mywebview.getSettings().setUseWideViewPort(false);
}else{
showBuyDialog();
}
}
// @Override
// public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
// if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
// mywebview.goBack();
// return true;
// }
// return super.onKeyDown(keyCode, event);
// }
// To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
mywebview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
finish();
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
//// This method will retun boolean value if net conect then value will be true otherwise false
private boolean isNetworkConnected() {
ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
public void showBuyDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("just for testing");
builder.setMessage("Check you net conectivity....");
builder.setCancelable(false);
builder.setPositiveButton("Buy", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent browserIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://www.google.com"));
startActivity(browserIntent);
}
});
builder.show();
}
}