سلام دوستان خسته نباشید .
من یه اکتیوی دارم که درونش یه webview قرار دادم . سوالم اینه که چجوری میتونم یه کدی فقط به این اکتیویتی بدم که وقتی کاربر روش میزنه ازش برای اتصال به اینترنت سوال کنه اگه اینترنتش رو فعال کرد اکتیوی webview رو باز کنه اگرم وصل نشد برگرده به اکتیویتی اصلی برنامه .
سلام ابتدا با استفاده از این مقاله نت گوشی را چک نماید اگر وصل بود activity که قرار است webview نمایش داده شود صدا زده شود با استفاده از تعریف intent و اگر هم نت وصل نبود activity اول start شود.
ممنون از پاسختون . ولی هرکار میکنم وقتی روی اکتیویتی میزنم فورس کلوز میده .
کدهای زیر مربوط به اموزشی هست که قرار دادید .
الان من اکتیویتی اصلیم main هست و میخوام اگه کاربر روی ( imageButton3 ) زد اگه نتش وصل بود بره به اکتیویتی ssajad در غیر این صورت یه پیامی بده که نت وصل نیس و برگرده به اکتیویتی اصلی (main) . فایلهای main هم ضمیمه کردم . پیشاپیش ممنون از پاسخگوییتون
import android.app.Activity; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; /** * Created by Esmaili-PC on 26/12/2015. */ public class AndroidDetectInternetConnectionActivity extends Activity { Boolean isInternetPresent = false; // Connection detector class ConnectionDetector cd; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnStatus = (Button) findViewById(R.id.btn_check); // creating connection detector class instance cd = new ConnectionDetector(getApplicationContext()); /** * Check Internet status button click event * */ btnStatus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // get Internet status isInternetPresent = cd.isConnectingToInternet(); // check for Internet status if (isInternetPresent) { // Internet Connection is Present // make HTTP requests showAlertDialog(AndroidDetectInternetConnectionActivity.this, "اینترنت فعال است", "اینترنت دستگاه شما فعال می باشد", true); } else { // Internet connection is not present // Ask user to connect to Internet showAlertDialog(AndroidDetectInternetConnectionActivity.this, "اینترنت فعال نیست", "اینترنت دستگاه شما غیر فعال می باشد", false); } } }); } /** * Function to display simple Alert Dialog * @param context - application context * @param title - alert dialog title * @param message - alert message * @param status - success/failure (used to set icon) * */ 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(); } }
شما برای activity مورد نظر یک شرط بزارید برای اتصال به نت که اگر نت وصل بود وارد activity3 شود در غیر این صورت شرطی که می خواهید باشد اجرا شود، اگر بدون شرط activity3 اجرا می شود شاید داخل androidmanifest کلاس مورد نظر به activity3 را تعریف نکرده اید که از actvity خارج می شوید.
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)