简体   繁体   English

要求用户在启动Android应用程序时启动WiFi或3G,如果没有连接到Internet

[英]Ask user to start WiFi or 3G on launching an android app if not connected to Internet

An android app that accesses some online pages using WebView is what I'm working with. 使用WebView访问某些在线页面的Android应用程序就是我正在使用的。 I want to incorporate an option where, if the user is not connected to internet, the application will prompt to switch on WiFi or 3G. 我想合并一个选项,如果用户没有连接到互联网,应用程序将提示开启WiFi或3G。

What's the best way to achieve this user convenience point of view? 实现这种用户便利性观点的最佳方法是什么?

The code is given below: 代码如下:

package com.url.appname;


import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.Window;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebChromeClient;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

 private WebView wv;  


 private ValueCallback<Uri> mUploadMessage;  
 private final static int FILECHOOSER_RESULTCODE=1;

 @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;        
  }  
 }  

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    wv = new WebView(this);
//    wv = (WebView) findViewById(R.id.webview);
    wv.loadUrl("http://www.url.com");



    // Let's display the progress in the activity title bar, like the
    // browser app does.
    getWindow().requestFeature(Window.FEATURE_PROGRESS);

    wv.getSettings().setJavaScriptEnabled(true);

    final Activity activity = this;

    wv.setWebViewClient(new WebViewClient(){    

       public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
             Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
           }
         });

    wv.setWebChromeClient(new WebChromeClient()  {  

           public void onProgressChanged(WebView view, int progress) {
                 // Activities and WebViews measure progress with different scales.
                 // The progress meter will automatically disappear when we reach 100%
                 activity.setProgress(progress * 100);
               }

        // For Android 3.0+
        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("image/*");  
            MainActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainActivity.FILECHOOSER_RESULTCODE ); 
            }

        // For Android < 3.0
        public void openFileChooser( ValueCallback<Uri> uploadMsg ) {
            openFileChooser( uploadMsg, "" );
            }

        // For Android > 4.1
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
            openFileChooser( uploadMsg, "" );
            }

                                });
      setContentView(wv);      

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}e

Create an AlertDialog if there isn't a network connection to tell the user and prompt them. 如果没有网络连接告诉用户并提示他们,请创建一个AlertDialog

Use this code as an example... 使用此代码作为示例...

protected void createNetErrorDialog() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("You need a network connection to use this application. Please turn on mobile network or Wi-Fi in Settings.")
        .setTitle("Unable to connect")
        .setCancelable(false)
        .setPositiveButton("Settings",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                startActivity(i);
            }
        }
    )
    .setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                MyActivity.this.finish();
            }
        }
    );
    AlertDialog alert = builder.create();
    alert.show();
}

Creating an Intent with Settings.ACTION_WIRELESS_SETTINGS as an action will start the built-in activity for the network settings. 使用Settings.ACTION_WIRELESS_SETTINGS创建一个Intent作为操作将启动网络设置的内置活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM