ورود

View Full Version : فراخوانی یک فایل کلاس



kavous_kiani
سه شنبه 29 بهمن 1392, 10:18 صبح
دوستان!

من برای بررسی وجود ارتباط وای فای یک فایل کلاس ایجاد کردم.

حالا در اکتیویتی اصلی خود با چه کدی این فایل را برای بررسی وجود ارتباط اجرا کنم؟

در تمرین اصلی، یک دکمه در صفحه وجود داشت، که با زدن آن، فایل اجرا می شد.

حال من می خواهم که این کار بدون زدن دکمه و به طور خودکار انجام شود.

آموزش را در این لینک ببینید!

http://android.programmerguru.com/android-check-wifi-internet-connection/

با تشکر

#root#
سه شنبه 29 بهمن 1392, 10:22 صبح
خب شما هرچی تو متد onClick نوشته شده بیار تو onCreate .

kavous_kiani
سه شنبه 29 بهمن 1392, 11:24 صبح
با سپاس از شما، این کار برای مبتدیانی چون من دشوار است.

تلاش کردم، اما دستورات زیادی دارد که کار من نیست.

من هنوز در مرحله آموزش هستم.

kavous_kiani
سه شنبه 29 بهمن 1392, 11:53 صبح
سرانجام موفق شدم:

این هم کد :



import android.app.AlertDialog;
import android.content.Context;


//Internet status flag
Boolean isConnectionExist = false;

// Connection detector class
WIFIInternetConnectionDetector cd;

// creating connection detector class instance
cd = new WIFIInternetConnectionDetector(getApplicationConte xt());

// get Internet status
isConnectionExist = cd.checkMobileInternetConn();

// check for Internet status
if (isConnectionExist) {
// Internet Connection exists
showAlertDialog(this, "Internet Connection",
"Your device has WIFI internet access", true);
} else {
// Internet connection doesn't exist
showAlertDialog(this, "No Internet Connection",
"Your device doesn't have WIFI internet access", false);
}

public void showAlertDialog(Context context, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();

// Setting Dialog Title
alertDialog.setTitle(title);

// Setting Dialog Message
alertDialog.setMessage(message);

// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});

// Showing Alert Message
alertDialog.show();
}



یک فایل کلاس هم جدا ساخته بودم طبق دستور تمرین آن سایت:

نام : WIFIInternetConnectionDetector.java




import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class WIFIInternetConnectionDetector {

private Context _context;

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

public boolean checkMobileInternetConn() {
//Create object for ConnectivityManager class which returns network related info
ConnectivityManager connectivity = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
//If connectivity object is not null
if (connectivity != null) {
//Get network info - WIFI Internet access
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TY PE_WIFI);

if (info != null) {
//Look for whether device is currently connected to WIFI network
if (info.isConnected()) {
return true;
}
}
}
return false;
}
}



البته باید تغییراتی در دیالوگ باکس داد تا در زمانی که ارتباط برقرار است دیگر هشداری ظاهر نشود.

ممنون

mohsen22
سه شنبه 29 بهمن 1392, 22:33 عصر
ممنون از اینکه تحربیاتتونو به ما هم دادین