Below android 6.0 its working perfect, but in android marshmallow HDMI status is not coming
private boolean isHdmiSwitchSet() {
File switchFile = new File("/sys/devices/virtual/switch/hdmi/state");
if (!switchFile.exists()) {
switchFile = new File("/sys/class/switch/hdmi/state");
}
try {
Scanner switchFileScanner = new Scanner(switchFile);
Toast.makeText(MainActivity.this,"HDMI Status"+switchFileScanner.nextInt(),Toast.LENGTH_LONG).show();
int switchValue = switchFileScanner.nextInt();
switchFileScanner.close();
return switchValue > 0;
} catch (Exception e) {
return false;
}
}
Follow below steps,
First, Put storage permission in AndroidManifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Second, Before access file check permission allowed by user or not:
public boolean checkPermissionForWRITE_STORAGE(){
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
Third, do your task on true condition.
I found solution,If we will compile like android system application with android:sharedUserId="android.uid.system" its working perfect...
Thank U.
Related
Even with all the permissions granted the startScan() function still returns false. So my question is: is there a way to scan access points on android 29 or higher?
The problem was in asking the user for permission. My working code
-Manifest:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
-And for asking user for permissions I used EasyPermissions:
boolean scanWifi(WifiManager wifiManagerToScan) {
String[] perm = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE};
if (EasyPermissions.hasPermissions(this, perm)) {
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
boolean scanStarted = wifiManagerToScan.startScan();
if (scanStarted) {
Toast.makeText(this, "Scanning...", Toast.LENGTH_SHORT).show();
return true;
} else {
Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show();
return false;
}
}
else {
EasyPermissions.requestPermissions(this, "WE REQUIRE THESE PERMISSIONS FOR THE APP TO FUNCTION", 123, perm);
return false;
}
}'
I was using below method for checking weather user is connected to internet or not.
public boolean internetIsConnected() {
try {
String command = "ping -c 1 google.com";
return (Runtime.getRuntime().exec(command).waitFor() == 0);
} catch (Exception e) {
return false;
}
}
Manifest is as below
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
but after I switched to latest API 30. It just freezes the app and returns false. any suggestion to get this working.
Thank you.
Edit :
As per comment's suggestions I tried below similar method but still Not working.
public boolean internetIsConnected() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
System.out.println(" mExitValue "+exitValue);
return (exitValue == 0);
} catch (IOException e){
System.out.println(" IO Error ");
e.printStackTrace(); }
catch (InterruptedException e) {
System.out.println(" Interrupted Error ");
e.printStackTrace(); }
return false;
}
I got :
I/System.out: mExitValue 1
So it is failing in Try block itself. I tried finding what Exit value "1" means but it only shows for "0" Here This Happens in both Emulator as well as physical device. Any suggestion is appreciated.
Thank you
You can check network availability using the following command.
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
You have to add access_network_state permission in Manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
If I ask for several permissions at one time on Android, run time permissions works great and asks the user for all the permissions I am requesting. That is if I do them all in one call.
If I instead choose to separate run time permission requests into groups, so that I first request access to contacts, then afterwards request access to SMS, my program will ignore the request to SMS and will ultimately treat the SMS request as rejected. Why is this? does the program proceed through code while waiting for the user to respond? How can I keep my permission requests separate and still be able to request permissions right after each other?
The reason why I don't want to do all permissions at once is because I only want ask for permissions when I need them. That is why I have separated permissions into groups.
Any help is greatly appreciated!
EDIT:
Sample code: checkPermissions is called
public boolean checkPermissions(int permissionsToCheck)
{
// Use constants above to specify permissions check to do
if (Build.VERSION.SDK_INT < 23) {
//Do not need to check the permission
return true;
} else {
switch (permissionsToCheck) {
case PERMISSIONS_FOR_MESSAGING:
return ( checkAndRequestContactPermissions() && checkAndRequestSMSPermissions() );
case PERMISSIONS_FOR_BLUETOOTH:
return checkAndRequestBluetoothPermissions();
default:
return false;
}
}
}
Calls checkAndRequestContactPermissions() and checkAndRequestSMSPermissions()
private boolean checkAndRequestContactPermissions() {
int permissionReadContacts = ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_CONTACTS);
int permissionWriteContacts = ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_CONTACTS);
List<String> listPermissionsNeeded = new ArrayList<>();
if (permissionReadContacts != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS);
}
if (permissionWriteContacts != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.WRITE_CONTACTS);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(mActivity, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), MY_PERMISSIONS_REQUEST_CONTACTS);
return false;
}
return true;
}
private boolean checkAndRequestSMSPermissions() {
int permissionReadSMS = ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_SMS);
int permissionSendSMS = ContextCompat.checkSelfPermission(mContext, Manifest.permission.SEND_SMS);
List<String> listPermissionsNeeded = new ArrayList<>();
if (permissionReadSMS != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.SEND_SMS);
}
if (permissionSendSMS != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_SMS);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(mActivity, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), MY_PERMISSIONS_REQUEST_SMS);
return false;
}
return true;
}
I try to check if I have permissions to Contacts and SMS but it only requests permission to Contacts instead of both.
I use Android API lvl 23 in my application. When I check Camera permission, the ContextCompat.checkSelfPermission always return 0 (== PackageManager.PERMISSION_GRANTED)
I managed to change it from ContextCompat to ActivityCompat.
Here is my code:
public static boolean verify(Activity activity, final String[] PERMISSIONS, final int PERMISSIONS_REQUEST_ID) {
if (underAPI23())
return true;
String[] denyPermission = new String[PERMISSIONS.length];
int denyPermissionLength = 0;
boolean shouldShowRequest = false;
for (int i = 0; i < PERMISSIONS.length; i++) {
int check = ContextCompat.checkSelfPermission(activity, PERMISSIONS[i]);
LogUtils.e(TAG, PERMISSIONS[i] + ": " + (check != PackageManager.PERMISSION_GRANTED));
// ===== ===== =====
// This always return true. :'(
// ===== ===== =====
if (check != PackageManager.PERMISSION_GRANTED) {
denyPermission[denyPermissionLength++] = PERMISSIONS[i];
if (shouldShowRequest == false) {
boolean should = ActivityCompat.shouldShowRequestPermissionRationale(activity, PERMISSIONS[i]);
if (should)
shouldShowRequest = true;
}
}
}
if (denyPermissionLength > 0) {
if (shouldShowRequest) {
ActivityCompat.requestPermissions(activity, denyPermission, PERMISSIONS_REQUEST_ID);
} else {
ActivityCompat.requestPermissions(activity, denyPermission, PERMISSIONS_REQUEST_ID);
}
return false;
} else {
return true;
}
}
My dependencies in Gradle build
dependencies {
//...
compile 'com.android.support:support-v4:23.2.0'
compile 'com.android.support:appcompat-v7:23.2.0'
//...
}
Updated: Permission call
if (PermissionGrant.verify(getActivity(), new String[]{Manifest.permission.CAMERA}, GRANT_TAKE_PHOTO)) {
// Do my jobs
}
Your permission should be Manifest.permission.<Android permission>
What is your Android target version? shouldShowRequestPermissionRationale always return false that mean ContextCompat.checkSelfPermission(activity, permission) always return false at Android API lvl under 23 too.
Document here. Please focus on:
Note: If the user turned down the permission request in the past and chose the Don't ask again option in the permission request system dialog, this method returns false. The method also returns false if a device policy prohibits the app from having that permission.
I want to read the debug state in the Android manifest file and then fire off a method or not based on that state. I see you can read the XML file and parse it but that way seems not that elegant. Is there another way, is that information of whats in the Manifest stored in a Java object somewhere?
<application android:name=".MyActivity" android:icon="#drawable/myicon"
android:label="#string/app_name" android:debuggable="true">
boolean DEBUGGABLE = (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
i use the ApplicationInfo.FLAG_DEBUGGABLE for checking if the android:debuggable=true is set.
The following code is copied from this thread
private static Boolean isSignedWithDebugKey = null;
protected boolean signedWithDebug() {
if(isSignedWithDebugKey == null) {
PackageManager pm = getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
isSignedWithDebugKey = (pi.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
}
catch(NameNotFoundException nnfe) {
nnfe.printStackTrace();
isSignedWithDebugKey = false;
}
}
return isSignedWithDebugKey;
}