2013-12-18 74 views
0

我不知道爲什麼這個異常來了,我只是檢查互聯網是否存在。 如果互聯網目前即時通訊打開鏈接多數民衆贊成它。Android:面對java.lang.SecurityException只是檢查互聯網是否存在

奇怪的是,我在其他項目中使用相同的代碼,它沒有任何問題的工作。

此外,* 我已經給網絡許可 *

這裏是我的代碼

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; 
} 
} 
+1

後logcat的輸出。 –

+3

添加此權限並閱讀您的日誌...它會幫助你。 '<使用權限android:name =「android.permission.ACCESS_NETWORK_STATE」/>' – MAC

+0

MAC解決方案解決了您的問題.. – AndroidHacker

回答

2

入住此權限的代碼你的清單文件,並添加這個,如果你還沒有添加它

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

非常感謝我的愚蠢..感謝點解決方案 –

0

首先檢查出的清單許可...

<!-- 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" /> 

否則,如果您的手機包括雙卡屬性,則嘗試檢測您的連接這樣的..

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; 

    } 
+0

謝謝你也是broda,但作爲我在urs之前看到上面的答案,所以選中它作爲解決方案 –

+0

沒關係.. :)但主要答案是由Mac提出的..檢查評論您的帖子..我問你,如果Mac的解決方案解決了你的問題。確保每次如果您正在使用雙卡智能手機,因爲它阻礙了您的連接檢查屬性。 CHEERS! – AndroidHacker

+0

是的,謝謝你的雙SIM卡的東西它很酷:))Bdw我是新來的,所以知道如何標記該評論作爲解決方案,因爲沒有刻度選項即將到來.. –