سلام
استاد تو گروه گفتید که کدهامو بزارم برای چک کردن اینترنت گوشی که اگر خاموش بود اکتیویتی که ساختم به اسم khata رو نشون بده
این کدهای کامل مین اکتیویتی من :
package ir.payamdev.student;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import java.io.IOException;
import static android.content.ClipData.newIntent;
import static ir.payamdev.student.R.id.activity_khata;
public class MainActivity extends AppCompatActivity implements
BottomNavigationView.OnNavigationItemSelectedListener {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
public boolean isOnline() {
ConnectivityManager connectivityManager;
connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
return false;
}
try {
Process ipProcess = Runtime.getRuntime().exec("/system/bin/ping -c 1 8.8.8.8");
if (ipProcess.waitFor() != 0) {
return false;
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return true;
}
private WebView webView;
private ColorStateList myColorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked}, // checked
new int[]{-android.R.attr.state_checkable}// unchecked
}, new int[]{
Color.parseColor("#FFFF4081"),
Color.parseColor("#FF880E4F")
});
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// READ_PHONE_STATE permission is granted
} else {
// READ_PHONE_STATE permission is denied
}
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
if (!isOnline()) {
Intent intent = new Intent(MainActivity.this,khata.class);
startActivity(intent);
finish();
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
} else {
if (!isOnline()) {
Intent intent = new Intent(MainActivity.this, khata.class);
startActivity(intent);
finish();
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.loadUrl(getString(R.string.link_about));
BottomNavigationView navigation = (BottomNavigationView)
findViewById(R.id.navigation);
navigation.setItemIconTintList(myColorStateList);
navigation.setItemTextColor(myColorStateList);
navigation.setOnNavigationItemSelectedListener(this);
navigation.setSelectedItemId(R.id.navigation_about);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// Code to run when page loading starts
}
@Override
public void onPageFinished(WebView view, String url) {
// Code to run when page loading finished
}
});
webView.getSettings().setJavaScriptEnabled(true);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_profile:
webView.loadUrl(getString(R.string.link_profile));
return true;
case R.id.navigation_lesson:
webView.loadUrl(getString(R.string.link_Lesson));
return true;
case R.id.navigation_description:
webView.loadUrl(getString(R.string.link_description));
return true;
case R.id.navigation_download:
webView.loadUrl(getString(R.string.link_download));
return true;
case R.id.navigation_about:
webView.loadUrl(getString(R.string.link_about));
return true;
}
return false;
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Main Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
}
الان تو نسخه اندروید 6 به پایین کار میکنه ولی روی 6 هیچ اتفاقی نمیفته
میخوام اگر اینترنت گوشی کاربر متصل نبود اکتیویتی که ساختم به اسم khata نشون بده و بقیه کدها اجرا نشن
چون برنامه طوری هست که صفحه اول برنامه یک آدرس سایت رو لود میکنه و اگر اینترنت وصل نباشه خطاهای داخل سایت رو نشون میده که زشته
کلا میخوام وقتی اینترنت گوشی وصل نباشه برنامه کار نکنه و خطا بده