WifiManager.WifiInfo getSSID & getBSSID not working if targetSdkVersion >= 26 - android

I'm getting correctly those values if I compile with API 27 but target API 25. If I set targetSdkVersion to 27, then, those values are not correctly retrieved.
Targeting SDK 25 or less, values are correct, but targeting 26 or more, I get those values:
SSID gives <unknown ssid>
BBSSID gives 02:00:00:00:00:00
These are my manifest permissions, all are normal permissions and don't require user grant:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
And this is the sample code:
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
connectionInfo.getSSID();
connectionInfo.getBSSID();
What has changed when targeting SDK 26 or more? what more should we do to get those values?

You need location permission starting with API 27

Google has changed their policy with WifiManager since you can track user's location tracking wifi location from SDK>=27. Therefore you need to access location permission in order to use getSSID() for your appliaction.
In order to use this code follow below.
As #Samuel Eminet answer above, you need either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION. I used ACCESS_FINE_LOCATION because
ACCESS_COARSE_LOCATION request permission for only NETWORK_PROVIDE whereas
ACCESS_FINE_LOCATION request permissions for NETWORK_PROVIDE AND GPS_PROVIDER.
Refer here for more information
In your Manifest file
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
and where your wifi manager is, add this code to request permissions.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
//Permission Not Granted
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_FINE_LOCATION);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_FINE_LOCATION:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission granted keep going status
Log.d(TAG, "PERMISSION GRANTED");
}
else {
Log.d(TAG, "PERMISSION DENIED");
}
}
}

One more thing to remember (I learned the hard way). If you are side-loading your app onto a device, don't forget to "allow" location permission in the app permissions (long press app, app info). It is off by default, thus you will get

I met with same problem. This problem happens higher android versions than API 23.
Solution:
You need to add below to build.gradle file.
implementation 'com.google.android.gms:play-services-location:15.0.0'
And you permission below rules.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
Below code provides automatically permission for location in application.
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
LocationRequest request = new LocationRequest()
.setFastestInterval(1500)
.setInterval(3000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
builder = new LocationSettingsRequest.Builder()
.addLocationRequest(request);
try {
Task<LocationSettingsResponse> result =
LocationServices.getSettingsClient(this.getApplicationContext()).checkLocationSettings(builder.build());
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
#Override
public void onComplete(#NonNull Task<LocationSettingsResponse> task) {
try {
task.getResult(ApiException.class);
} catch (ApiException e) {
switch (e.getStatusCode()) {
case LocationSettingsStatusCodes
.RESOLUTION_REQUIRED:
ResolvableApiException resolveApiException = (ResolvableApiException) e;
try {
resolveApiException.startResolutionForResult(WifiConnectionActivity.this, REQUEST_CHECK_CODE);
} catch (IntentSender.SendIntentException ex) {
ex.printStackTrace();
} catch (ClassCastException ex) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
}
});
} catch (Exception x) {
}
checkPermissions();
} catch (Exception a) {
}
}
void checkPermissions() {
if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(WifiConnectionActivity.this, new String[]
{
Manifest.permission.ACCESS_COARSE_LOCATION
}, REQUEST_LOCATION);
}
else{
//Toast.makeText(getActivity(), "GPS is already permissioned", Toast.LENGTH_SHORT).show();
}
}

The below is the code .
public String getMac() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//Permission Not Granted
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}else{
WifiManager wifimanage = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiinfo = wifimanage.getConnectionInfo();
macAddress = wifiinfo.getBSSID();//Get the mac address of the currently connected network;
}
return macAddress;
}
#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) {
//permission granted keep going status
Log.d("mac", "PERMISSION GRANTED");
WifiManager wifimanage = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiinfo = wifimanage.getConnectionInfo();
macAddress = wifiinfo.getBSSID();//Get the mac address of the currently connected network;
} else {
Log.d("mac", "PERMISSION DENIED");
}
}
}
Then call getMac() to solve the problem.

Related

Android : unable to list /storage/emulated/0 files despite permissions

I have the following code to handle storage permissions :
private void getGalleryPermission() {
Log.d(TAG, "getGalleryPermission: checking permissions.");
String[] permissions = { WRITE_STORAGE, READ_STORAGE };
if (ContextCompat.checkSelfPermission(requireActivity(), WRITE_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "getGalleryPermission: write storage is granted.");
if (ContextCompat.checkSelfPermission(requireActivity(), READ_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "getGalleryPermission: read storage is granted.");
setupRecyclerView();
}
else {
Log.d(TAG, "getGalleryPermission: asking to read the storage.");
requestPermissions(permissions, REQUEST_STORAGE_PERMISSION_RESULT);
}
}
else {
Log.d(TAG, "getGalleryPermission: asking to write the storage.");
requestPermissions(permissions, REQUEST_STORAGE_PERMISSION_RESULT);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.d(TAG, "onRequestPermissionsResult: request code " + requestCode);
if (requestCode == REQUEST_STORAGE_PERMISSION_RESULT) {
Log.d(TAG, "onRequestPermissionsResult: request code is correct.");
if (grantResults.length > 0) {
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(requireActivity(), "Don't have permission to access the Gallery !", Toast.LENGTH_SHORT).show();
break;
}
}
// Setup the gallery once the permissions are granted
setupRecyclerView();
}
}
}
I am testing this code on several of my phones, and it's working perfectly. However, on my Huawei, the listFiles() method returns null on the /storage/emulated/0 directory.
I have checked in the phone Settings + in logs, and the storage permissions are granted (like on other working phones).
What can prevent the files from being read despite granted permissions ?
On some phones, is it possible that the /storage/emulated/0 is not the correct directory to target to get all gallery files ?
PS : i have checked the directory from the Files application in my phone and it's not empty (DCIM, etc.... are inside).
Thanks
is the Huawei device which returns null running Android 10? It seems that for that path Android returns null since API 29. You might want to check this question here.

SMS Permissions

I am building an Android app via android studio. One major feature of the app is sending SMS but I cannot seem to get it to run correctly on different devices due to permission errors.I tried to eliminate this issue by prompting for permissions when the user logs in but this worked on API version 13 and failed on 26,I tried it with build.versioncode.o. but his also failed.
What is the correct way to do this and to check permissions each time a SMS is sent?(note sms are sent from many different functions throughout the app.
I also just got this error hen sending message
java.lang.SecurityException: Neither user 10205 nor current process has android.permission.READ_PHONE_STATE.
Request permission on log in
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void getPermissionToReadSMS() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(
android.Manifest.permission.READ_SMS)) {
Toast.makeText(this, "Please allow permission!", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{android.Manifest.permission.READ_SMS},
REQUEST_READ_SMS);
}
}
Method snippet that sends method
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(testContact.getNumber(), null, "SENT MESSAGE" + message, sentPending, deliveredPending);
App Manifest
<!-- grant permission to uses in build sms service -->
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission-group.SMS" />
For checkPermission to send SMS, you will need to add
<uses-permission android:name="android.permission.SEND_SMS" />
permission and the below code for >=23 api
protected void sendSMS() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
PERMISSION_REQUEST_SEND_SMS);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_SEND_SMS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent successfully.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"SMS faild", Toast.LENGTH_LONG).show();
return;
}
}
}
}
//You should declare this in your project
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_SMS,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.SEND_SMS,
};
//and call verifymethod where you want
verifyStoragePermissions(Activity_home.this);
//and the verifyStoragePermissions methode is
public static void verifyStoragePermissions(Activity activity) {
int readPermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE);
int smspermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_SMS);
int smspermission1 = ActivityCompat.checkSelfPermission(activity, Manifest.permission.RECEIVE_SMS);
int smssendpermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.SEND_SMS);
if (readPermission != PackageManager.PERMISSION_GRANTED || readPermission != PackageManager.PERMISSION_GRANTED || smspermission != PackageManager.PERMISSION_GRANTED || smspermission1 != PackageManager.PERMISSION_GRANTED || smssendpermission != PackageManager.PERMISSION_GRANTED) {userActivityCompat.requestPermissions(activity,PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE );
}
}
I'm sending it from a fragment. The solution given by #Android Team above works for me for the first time only. Apparently if the app has required permission it doesnt call onRequestPermissionsResult.
Using the following code (else portion with log msg "Have permission.." in the onlclick handler is required for my app to work). I'm using Android 11 device.
if (ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
//if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
if (shouldShowRequestPermissionRationale(
Manifest.permission.SEND_SMS)) {
} else {
//ActivityCompat.requestPermissions(getActivity(),
requestPermissions(
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
}
} else {
Log.i("smsB", "Have permission... send the sms now");
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
}

On Oreo (8.1.0) not getting the correct Wifi SSID. It's showing <unknown ssid> though it is connected to a wifi with SSID

I need to check the current connected wifi SSID on android. I checked with Nokia 6 and OnePlus 5 with respectively Oreo 8.1.0 and Oreo 8.0. Other phones with different OS version is working fine with this code. Is there anything wrong with my code?
private WifiInfo wifiInfo;
private String ssid = "";
private WifiManager wifiManager;
private boolean getWifiStatus() {
wifiManager= (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiInfo = wifiManager.getConnectionInfo();
ssid = "";
ssid = wifiInfo.getSSID();
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isWiFi = false;
if(activeNetwork != null){
isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
}
Log.d(TAG, "getWifiStatus: " + ssid);
if(ssid.contains("TripleMZim") && wifiManager.isWifiEnabled() && isWiFi ){
return true;
}
else{
return false;
}
}
permission in Manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
There are different approaches how you can get the WIFI information (such as SSID) in android.
Some of them already listed here, for example WifiManager service.
But for OP question:
Is there anything wrong with my code?
No, the problem is not in your code.
The main reason for the unexpected behavior, is because of the security patch of the last android releases.
Before the patch (or any device that didn't get an update), the hackers could steal sensitive information that was leaked through the wifi info.
For example the hackers could obtain the device geolocation, that should be accessible only with the Dangerous Permission LOCATION, that should be requested at runtime and can be revoked at any moment. But in the other hand - the WifiInfo is accessible only with the Normal Permission android.permission.ACCESS_WIFI_STATE which is granted at install time and cannot be revoked.
That way, hackers could bypass the permission mechanism and access the data from dangerous permission using only normal permission.
The issue was reported and tracked by CVE-2018-9489, you can read more about it here: Sensitive information expose via WIFI
So the reason why you having those problems is because now android blocks the sensitive wifi info, unless the app holds the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.
So, if you request those permissions explicitly (and allowed by the user), your code should work fine.
For example:
Manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Activity
private static final int LOCATION = 1;
protected void onStart() {
super.onStart();
//Assume you want to read the SSID when the activity is started
tryToReadSSID();
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if(grantResults[0] == PackageManager.PERMISSION_GRANTED && requestCode == LOCATION){
//User allowed the location and you can read it now
tryToReadSSID();
}
}
private void tryToReadSSID() {
//If requested permission isn't Granted yet
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//Request permission from user
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION);
}else{//Permission already granted
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if(wifiInfo.getSupplicantState() == SupplicantState.COMPLETED){
String ssid = wifiInfo.getSSID();//Here you can access your SSID
System.out.println(ssid);
}
}
}
Oreo 8.1+ devices require the coarse location runtime permission as well as location services to be turned on before being able to retrieve the connected SSID as it can infer the users location.
Further information is available here
Which relates to this
In Android Oreo you could use ConnectivityManager to know your wifi SSID.
Permission
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Code to get SSID
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
String ssid = info.getExtraInfo();
Log.d(TAG, "WiFi SSID: " + ssid);
}
This is tested in Android Oreo 8.1.0. You will get SSID enclosed with double quotes.
You need to request permission in manifest :
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
and request it in your activity :
private void grantPermm()
{
try
{
if (ContextCompat.checkSelfPermission(MainScreenActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
}
else
{
ActivityCompat.requestPermissions(MainScreenActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
101);
}
}
catch (Exception xx){}
}
then use this function :
public static String getWifiName(Context context)
{
String result="";
try {
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (manager.isWifiEnabled())
{
WifiInfo wifiInfo = manager.getConnectionInfo();
if (wifiInfo != null)
{
NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR)
{
result = wifiInfo.getSSID();
if(result.equalsIgnoreCase("<unknown ssid>"))
{
Toast.makeText(context,"You are connected to mobile data", Toast.LENGTH_SHORT).show();
result="";
}
return result;
}
else
{
Toast.makeText(context,"WIFI not connected", Toast.LENGTH_SHORT).show();
return "";
}
}
else
{
Toast.makeText(context,"No WIFI Information", Toast.LENGTH_SHORT).show();
return "";
}
}
else
{
Toast.makeText(context,"WIFI not enabled", Toast.LENGTH_SHORT).show();
return "";
}
}
catch (Exception xx)
{
Toast.makeText(context, xx.getMessage().toString(), Toast.LENGTH_SHORT).show();
return "";
}
}
If you're targeting API 29 (Android 10) then the location permission requested must now be for ACCESS_FINE_LOCATION, not just ACCESS_COARSE_LOCATION. Most of the above sample code is fine, however some answers could be misleading in the text.
https://developer.android.com/about/versions/10/privacy/changes
If you face issues even after trying the Yaswant Narayan and behelit answers, make sure you have enabled the Location/GPS in the device while trying to get the SSID. Even though, you have acquired the relevant permissions in Android Oreo and above devices, if the Location/GPS is turned off by the user, then you will get the from those devices.
You need to ask for runtime permission of read phone state. This is included for better security.
code is as follows:-
TelephonyManager telephonyManager = (TelephonyManager) getContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String IMEI ="";
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.READ_PHONE_STATE},
123);
}
int permission = ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.READ_PHONE_STATE);
if(permission == PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
IMEI = telephonyManager.getImei();
}
else{
IMEI = telephonyManager.getDeviceId();
}
}

internet permission not granted

I am writing an app to send msg over TCP Socket. I am struggling to get access to the internet permission. (API 26 is the target)
socket EACCESS permission denied
I have setup in my Manfest :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.seb.sebastien.opengardenirc">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.persmission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
As I am using the API26, I have to add a permission check in the code to make sure it works fine:
if (shouldAskPermission()) {
Log.d(TAG, "Permission is required");
String[] PERMISSIONS_STORAGE = {
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_WIFI_STATE
};
int permission_internet = ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET);
int permission_network = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE);
int permission_wifi = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE);
Log.d(TAG, "Package Manager: " + PackageManager.PERMISSION_GRANTED);
Log.d(TAG, "INTERNET : " + permission_internet);
Log.d(TAG, "NETWORK : " + permission_network);
Log.d(TAG, "WIFI : " + permission_wifi);
if ((permission_internet != PackageManager.PERMISSION_GRANTED)
|| (permission_network != PackageManager.PERMISSION_GRANTED)
|| (permission_wifi != PackageManager.PERMISSION_GRANTED)){
Log.d(TAG, "Asking for permission");
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
PERMISSIONS_STORAGE,
REQUEST_CONNECTION_PERMISSION
);
} else {
Toast.makeText(MainActivity.this, "Permission (already) Granted!", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Permission already granted");
startTcpInAThread("str");
}
}
And I have also the method to catch the onRequestPermissionsResult.
private boolean shouldAskPermission(){
return(Build.VERSION.SDK_INT> Build.VERSION_CODES.LOLLIPOP_MR1);
}
#Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
switch(permsRequestCode){
case REQUEST_CONNECTION_PERMISSION:
Log.d(TAG, "Permission granted result : " + grantResults[0]);
Log.d(TAG, "Permission granted result : " + grantResults[1]);
Log.d(TAG, "Permission granted result : " + grantResults[2]);
boolean accepted = (grantResults[0]== PackageManager.PERMISSION_GRANTED)
&& (grantResults[1]== PackageManager.PERMISSION_GRANTED)
&& (grantResults[2]== PackageManager.PERMISSION_GRANTED);
if(accepted) {
startTcpInAThread("str");
} else {
Toast.makeText(getApplicationContext(), "Permissions issues!!!!", Toast.LENGTH_SHORT).show();
}
break;
}
}
First things is that when checking the permission, I can see that android.persmission.INTERNETis not granted. Good thing is that it's going to call the request permission method ActivityCompat.requestPermissions
I was expecting a Dialog box showing up but my understanding is that the dlgbox only show up for some on the permission requests some other do not need to ask.
My concerns is that when I got onRequestPermissionsResultthe permission is still not granted.
Anybody know how to grant the permission to internet ?
I have also pay attention to not run the network access in the same thread.
Regards

Files from remote source do not get copied to external SDCard and prompt an error like "No Write Permission"

I am trying to copy some files from remotely connected pc to my Android Device's SDCard but it is showing an error "No write permission". while i transfer same files to internal storage, it transferred successfully.
I have already included these permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
My Android Device is running on Android Version 6.0.1
Use Run time permission for Android Version 6.0.0 and more
Click here for more information
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v("======>", "Permission is granted");
return true;
} else {
Log.v("======>", "Permission is revoked");
//1 is request code
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
Log.v("======>", "Permission is granted");
return true;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
//1 is request code
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.v("======>", "Permission: " + permissions[0] + "was " + grantResults[0]);
openFileAttachDialog();
}
return;
}
}

Categories

Resources