I am having problem where it seems my Android Studio is not recognizing the uses-permission in my AndroidManifest.xml file. In my main activity, when I try to call for some function that requires INTERNET and ACCESS_NETWORK_STATE, the activity seems to not recognize that I have already put the permission in the AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
The following is the method that calls for the ACCESS_NETWORK_STATE. The part when calling
public boolean isNetworkAvailable(Context context) {
boolean isOnline = false;
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
NetworkCapabilities capabilities = manager.getNetworkCapabilities(manager.getActiveNetwork());
isOnline = capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
} else {
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
isOnline = activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}
} catch (Exception e) {
e.printStackTrace();
}
return isOnline;
}
private void showConnectDialog() {
Log.d(TAG, "showConnectDialog: trying to show the connect dialog");
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.MyAlertDialogStyle);
builder.setMessage("Please connect to the internet to proceed further")
.setCancelable(false)
.setPositiveButton("Connect", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
});
builder.show();
}
In my main activity oncreate:
if (!isNetworkAvailable(MainActivity.this)){
showConnectDialog();
}
if (isServicesOK()) {
init();
}
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
AdView mAdView = findViewById(R.id.adViewMain);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
You can see in the images (attached below) show that I have an error in my isNetworkAvailable method, and also my Google AdMob. If I hover my mouse on the error, Android Studio mentioned that I need to add ACCESS_NETWORK_STATE permission for the isNetworkAvailable method, and INTERNET permission for the Google AdMob. But I already have them in my AndroidManifest.xml. Am I missing anything else? Thank you very much for your help.