Google Apps can access your location, but this functionality may be either enabled or disabled:
How can I programmatically check if a Google App can access your location?
I am use this code but nothing is changing.
boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
this, AtlastActivity.GPS_ERRORDIALOG_REQUEST);
dialog.show();
} else {
}
Related
I want to send SMS by android-app, but the permission dialog show before the send a message every time, I supposed to send a message without dialog show after the first permission asked.
My android app is run on mi-8, target SDK-version is 29.
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, 3);
} else if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, 4);
} else {
sendMsg();
}
I expect the permission dialog did not show when I send a message after the first asked permission, but actual every time show dialog when I send a message .
use this function to check whether you have given the permission if say so you have given as required persmission it will return true flag
check by this way
private boolean checkAndRequestPermissions(){
//add as much as permission you need
int readSMS = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS);
int sendSms = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS);
List<String> listPermissionsNeeded = new ArrayList<>();
if (readSMS != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_SMS);
}
if (receiveSms != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.RECEIVE_SMS);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
check by this way
if (checkAndRequestPermissions()){
sendMsg();
}else{
//do what you want
}
no need to do this by hardcode way
So I'm new to Android development and have a basic application that writes to a text file on it's first time loading. But because it is the application's first time loading it must first ask the user for read/write permissions. The problem I have is that the application tries to write the file before it has these permissions. I have the following MainActivity.java class that contains the section of code that requests for permissions:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Stes fullscreen and removes title
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
setContentView(new Environment(this));
}
}
The constructor for Environment.jar then calls a function that writes the file if it does not yet exist.
But it seems that while the application does request permissions, it doesn't wait to receive these permissions before starting again. Is there any way to get it to wait to recieve the permissions, and if the permissions are not received to close the application?
You should call your Environment constructor only if the Permission is granted. Otherwise you can show an error dialog asking user to first grant permission before proceeding with the app. To check for permission You should request for permission as below -
Bool isPermissionGiven = false;
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission. READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
isPermissionGiven = true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
isPermissionGiven = false;
}
if (isPermissionGiven){
new Environment(this)
}
Then implement onRequestPermissionsResult
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 0:
boolean isPerpermissionForAllGranted = false;
if (grantResults.length > 0 && permissions.length==grantResults.length) {
for (int i = 0; i < permissions.length; i++){
if (grantResults[i] == PackageManager.PERMISSION_GRANTED){
isPerpermissionForAllGranted=true;
}else{
isPerpermissionForAllGranted=false;
}
}
Log.e("value", "Permission Granted, Now you can use local drive .");
} else {
isPerpermissionForAllGranted=true;
Log.e("value", "Permission Denied, You cannot use local drive .");
}
if(isPerpermissionForAllGranted){
// Do your work here
new Environment(this);
}
break;
}
}
you should implment onRequestPermissionsResult() method
more here https://developer.android.com/training/permissions/requesting.html
Also its a violation of the android rules to terminate the app like that. You should just print something like this app requires this permission to run. Or whatever you want to say and do nothing more if you want but don't terminate the app.
Also be prepared to launch the app no matter what. You can stick in place holder text while you wait like "loading..." and on a successful result update the text.
I'm following a tutuorial to use google location services. So, I'm trying to get location using Google API service. But I got isGooglePlayServicesAvailable(this) is deprecated error. Can anyone help. Thanks
Below is my code for checking if Play services exist on device:
private boolean checkPlayServices(){
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if(resultCode != ConnectionResult.SUCCESS){
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)){
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
}else {
Toast.makeText(getApplicationContext(),
"This device is not supported", Toast.LENGTH_LONG)
.show();
finish();
}
return false;
}
return true;
}
Use isGooglePlayServicesAvailable instead
GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context)
The correct way of checking if play services are available is:
private boolean isGooglePlayServicesAvailable(Context context) {
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(context);
return resultCode == ConnectionResult.SUCCESS;
}
I have heard in many places that if my app uses a permission not applicable to a certain device, it will not show up in the play store for that device. Now, in my code, I am playing audio. I mute that audio whenever there is a phone call by doing this:
private PhoneStateListener phoneStateListener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
onPhoneCallInterrupt(); //Method I made that mutes audio for phone call
} else if (state == TelephonyManager.CALL_STATE_IDLE) {
} else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
onPhoneCallInterrupt(); //Method I made that mutes audio for phone call
}
}
};
Now, this uses the following permission in the manifest:
<uses-feature android:name="android.permission.READ_PHONE_STATE" android:required="false" />
Will I get any exceptions because I have made the permission optional by doing android:required = "false" on devices that don't have phone compatibility (tablets)?
The reason I am so confused on this, is because I am checking if the phone is being used, but I am not using it. So, will my app work on tablets, let alone show up in the play store for them?
Thanks for helping me clear up this confusion,
Ruchir
You have to use this way
As your permission READ_PHONE_STATE request the
<uses-feature android:name="android.hardware.telephony" />
So you need to use this
<uses-feature android:name="android.hardware.telephony"
android:required="false" />
So play store wont filter your apps for the tablets
but make sure you have to do check manually where you are using the functionality of the telephony that device is phone or tablets and have a telephony aceess or not
To check the telephony access device has or not use this check this code
static public boolean hasTelephony()
{
TelephonyManager tm = (TelephonyManager) Hub.MainContext.getSystemService(Context.TELEPHONY_SERVICE);
if (tm == null)
return false;
//devices below are phones only
if (Utils.getSDKVersion() < 5)
return true;
PackageManager pm = MainContext.getPackageManager();
if (pm == null)
return false;
boolean retval = false;
try
{
Class<?> [] parameters = new Class[1];
parameters[0] = String.class;
Method method = pm.getClass().getMethod("hasSystemFeature", parameters);
Object [] parm = new Object[1];
parm[0] = "android.hardware.telephony";
Object retValue = method.invoke(pm, parm);
if (retValue instanceof Boolean)
retval = ((Boolean) retValue).booleanValue();
else
retval = false;
}
catch (Exception e)
{
retval = false;
}
return retval;
}
For more you can check this blog
http://commonsware.com/blog/2011/02/25/xoom-permissions-android-market.html
I have the following code in my app to initiate Map View
private boolean servicesOk() {
int isAvailable = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Connect Connect to Maps", Toast.LENGTH_SHORT)
.show();
}
return false;
}
But on the check the isAvailable has the value 9, moreover in LogCat its continously repeating message: Google Play services is invalid. Cannot recover.
Can anyone please tell me, what mistake Am I making?
From the documentation 9 is an invalid/inauthentic install of google play services.
Have you made sure you downloaded and updated gps from the play store?