Android:面对java.lang.SecurityException只是检查互联网是否存在

时间:2013-12-18 05:40:38

标签: java android

我不知道为什么会出现这种异常,我只是在检查互联网是否存在。 如果互联网存在,我打开一个链接就可以了。

奇怪的是,我在其他项目中使用了相同的代码,它没有任何问题。

此外,* 我已经获得了互联网许可 *

这是我的代码

package com.siddharth.empyrealrealty;



import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class Main extends Activity {

WebView wv;
ProgressDialog pg;
Background bg;
boolean isInternetPresent = false;
AlertDialog.Builder alert;
MediaPlayer exceptionNotifier;
ConnectionDetector cd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//  overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    setContentView(R.layout.activity_main);

    alert = new AlertDialog.Builder(this);
    cd = new ConnectionDetector(this);

    try {
        isInternetPresent = cd.isConnectingToInternet();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), e.toString(),
                Toast.LENGTH_LONG).show();
    }
    if(isInternetPresent){
     Toast.makeText(getApplicationContext(), "Loading... Please Wait..", Toast.LENGTH_LONG).show();

        wv=(WebView)findViewById(R.id.webView1);

        bg = new Background();
        bg.execute();
    }else{
        exceptionNotifier = MediaPlayer.create(getApplicationContext(), R.raw.notify);
        exceptionNotifier.start();
        alert.setTitle("Alert!"); 

        alert.setMessage("Internet Not Present..! ");
        alert.setCancelable(true);
        alert.setPositiveButton("Ok!",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog,
                            int which) {
                        // TODO Auto-generated method stub
                        exceptionNotifier.release();

                    }
                }).show();

    }

    }

public void progress(){
    pg = new ProgressDialog(this);
    pg.setTitle("");
    pg.setMessage("Please Wait.........");
    pg.setCancelable(false);
    pg.setIndeterminate(true);
    pg.show();
}

class Background extends AsyncTask<Void, Void, Void>
{

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub


            try{
                wv.loadUrl("http://www.siddharth.uphero.com");
                }catch(Exception e){
                    e.printStackTrace();
                }

                wv.getSettings().setJavaScriptEnabled(true);
                wv.getSettings().setLoadWithOverviewMode(true);
                wv.getSettings().setUseWideViewPort(true);
                wv.getSettings().setBuiltInZoomControls(true);
                wv.setWebViewClient(new MyWebClient()); 


        return null;
    }
    @Override
    protected void onPostExecute(Void result) {




    }

    @Override
    protected void onPreExecute() {

        progress();
    }
}
 public class MyWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
        super.onPageFinished(view, url);

                     pg.dismiss();

        }
    }



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





}

继承了ConnectionDetector类的代码

 class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context){
    this._context = context;
}

public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) _context.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;
}
 }

2 个答案:

答案 0 :(得分:2)

在清单文件中检查此权限,如果尚未添加,请添加此权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

答案 1 :(得分:0)

首先查看Manifest权限......

<!-- Checks Internet Services. -->
    <uses-permission android:name="android.permission.INTERNET" />

<!-- Network State Permissions to detect Internet status -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

否则,如果您的手机包含双SIM卡属性,请尝试检测此类连接..

public boolean isConnectingToInternet(){

             boolean outcome = false;


     if (getApplicationContext() != null) {
         ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
                 .getSystemService(Context.CONNECTIVITY_SERVICE);

         NetworkInfo[] networkInfos = cm.getAllNetworkInfo();

         for (NetworkInfo tempNetworkInfo : networkInfos) {

             if (tempNetworkInfo.isConnected()) {
                 outcome = true;
                 break;
             }
         }
     }

     return outcome;

     }