I need some advice to learn more about thread and looper. How the looper start and stop? and how to implement it to Network Check method..
i have tried some code. here they are
public void downloadthread(Handler handler){
handler.post(new Runnable() {
@Override
public void run() {
try{
Looper.prepare();
// cekkoneksi is Network Check Class
cek1 = cekkoneksi.isConnectingToNetwork();
cek2 = cekkoneksi.hasActiveInternetConnection();
cek3 = cekkoneksi.checkServerConnectionn();
//NorNetwork is Condition if when device Lose Network Connection
NorNetwork();
Looper.loop();
Looper.myLooper().quit();
}catch(Exception e){
Toast.makeText(context1, "thread error gan", Toast.LENGTH_LONG).show();
}
}
});
}
Here a part of NorNetwork()
public boolean NorNetwork(){
if (cek1==false) {
final AlertDialog.Builder builders = new AlertDialog.Builder(activity);
builders.setMessage("Tidak Tersambung Ke Jaringan. Aktifkan Paket Data ?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
Intent setting = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
activity.startActivity(setting);
activity.finish();
}
})
.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);
activity.finish();
activity.startActivity(exit);
}
});
final AlertDialog alerts = builders.create();
alerts.show();
}.......
in MainActivity to implement it i write this
handel = new Handler();
cekifkoneksi.downloadthread(handel);
But that not work well. And the question, How to make it work well? is there any wrong code? help me to realize that.
or somebody have another method to doing that?
thank u everyone