请,任何人都可以查看我的代码吗?
这是我的MainActivity
public class MenuActivity extends Activity{
CekKoneksi cekkoneksi;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_layout);
cekkoneksi = new CekKoneksi(getApplicationContext());
new cekinternet().execute();
}
private class cekinternet extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
//cekkoneksi = new CekKoneksi(getApplicationContext());
if (cekkoneksi.isConnectingToNetwork()) {
if (cekkoneksi.hasActiveInternetConnection())
{
Toast.makeText(getApplicationContext(), "Terkoneksi dengan Jaringan Internet", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG).show();
final AlertDialog.Builder builder = new AlertDialog.Builder(MenuActivity.this);
builder.setMessage("Terkoneksi dengan Jaringan tetapi tidak dengan Internet,\n Buka Setting dan Ubah Jaringan?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
})
.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
Intent exit = new Intent(Intent.ACTION_MAIN);
exit.addCategory(Intent.CATEGORY_HOME);
exit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MenuActivity.this.finish();
startActivity(exit);
}
});
final AlertDialog alert = builder.create();
alert.show();
}
}
else
{
final AlertDialog.Builder builder = new AlertDialog.Builder(MenuActivity.this);
builder.setMessage("Tidak Tersambung dengan Jaringan Internet, Buka Setting dan Aktifkan Jaringan?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
})
.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
Intent exit = new Intent(Intent.ACTION_MAIN);
exit.addCategory(Intent.CATEGORY_HOME);
exit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MenuActivity.this.finish();
startActivity(exit);
}
});
final AlertDialog alert = builder.create();
alert.show();
}
return null;
}
}
}
这里的课程“CekKoneksi”
public class CekKoneksi {
private Context cont;
public CekKoneksi (Context context){
this.cont = context;
}
public boolean isConnectingToNetwork(){
ConnectivityManager connectivity = (ConnectivityManager) cont.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 boolean hasActiveInternetConnection() {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
Toast.makeText(cont, "Error checking internet connection", Toast.LENGTH_LONG).show();
}
return false;
}
}
这是logcat
09-14 03:58:25.799: E/AndroidRuntime(26427): FATAL EXCEPTION: AsyncTask #1
09-14 03:58:25.799: E/AndroidRuntime(26427): java.lang.RuntimeException: An error occured while executing doInBackground()
09-14 03:58:25.799: E/AndroidRuntime(26427): at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-14 03:58:25.799: E/AndroidRuntime(26427): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-14 03:58:25.799: E/AndroidRuntime(26427): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-14 03:58:25.799: E/AndroidRuntime(26427): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-14 03:58:25.799: E/AndroidRuntime(26427): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-14 03:58:25.799: E/AndroidRuntime(26427): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-14 03:58:25.799: E/AndroidRuntime(26427): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-14 03:58:25.799: E/AndroidRuntime(26427): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-14 03:58:25.799: E/AndroidRuntime(26427): at java.lang.Thread.run(Thread.java:856)
09-14 03:58:25.799: E/AndroidRuntime(26427): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-14 03:58:25.799: E/AndroidRuntime(26427): at android.os.Handler.<init>(Handler.java:121)
09-14 03:58:25.799: E/AndroidRuntime(26427): at android.widget.Toast$TN.<init>(Toast.java:326)
09-14 03:58:25.799: E/AndroidRuntime(26427): at android.widget.Toast.<init>(Toast.java:91)
09-14 03:58:25.799: E/AndroidRuntime(26427): at android.widget.Toast.makeText(Toast.java:242)
09-14 03:58:25.799: E/AndroidRuntime(26427): at app.source.ensiklopedaitempatwisataindonesia.MenuActivity$cekinternet.doInBackground(MenuActivity.java:109)
09-14 03:58:25.799: E/AndroidRuntime(26427): at app.source.ensiklopedaitempatwisataindonesia.MenuActivity$cekinternet.doInBackground(MenuActivity.java:1)
09-14 03:58:25.799: E/AndroidRuntime(26427): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-14 03:58:25.799: E/AndroidRuntime(26427): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-14 03:58:25.799: E/AndroidRuntime(26427): ... 5 more
在Manifest中,我添加了此权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
答案 0 :(得分:0)
将您的Toast
和AlertDialog
工作移至onPostExecute()
的{{1}}。您无法从后台线程修改UI。