我不知道爲什麼這個異常來了,我只是檢查互聯網是否存在。 如果互聯網目前即時通訊打開鏈接多數民衆贊成它。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;
}
}
後logcat的輸出。 –
添加此權限並閱讀您的日誌...它會幫助你。 '<使用權限android:name =「android.permission.ACCESS_NETWORK_STATE」/>' – MAC
MAC解決方案解決了您的問題.. – AndroidHacker