This is weird. I mean, I have strong network, and GPS is turned On, still it asks for Wifi !!
I'm using new Location Api with class FusedLocationProviderClient.
This is how I am checking if Location Settings are enabled or not:
private void checkIfLocationSettingsAreEnabled() {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
SettingsClient client = LocationServices.getSettingsClient(context);
Task<LocationSettingsResponse> locationSettingsResponseTask = client.checkLocationSettings(builder.build());
locationSettingsResponseTask.addOnSuccessListener(locationSettingsResponse -> {
getLastKnownLocation();
});
locationSettingsResponseTask.addOnFailureListener(e -> {
if (e instanceof ResolvableApiException) {
myLocationListener.onResolutionNeeded(e);
} else {
myLocationListener.onLocationFailure(e);
}
});
}
With the above code, I am able to get below image:
But then, after clicking OK, again I make call to checkIfSettingsAreEnabled() this shows another popup as below:
I wonder why enabling Wifi is mandatory, even if I am on desert safari, where there is no Wifi to connect to !!
Is there any way to Skip this Wifi option, and work as normal as Google Maps does ?
In fact, Google Maps uses priority PRIORITY_HIGH_ACCURACY and still once, the first settings dialog has been shown, it doesn't ask second time to turn on Wifi.
FusedLocationProvider uses a combo of GPS and WIFI to get your location. Especially when asking for balanced mode, it needs both or won't work (GPS takes power, so it won't rely just on it for balanced). Try max accuracy, its less likely to use it then. Or just use the GPS provider if you actually want GPS.
PRIORITY_BALANCED_POWER_ACCURACY
This priority will go with coarse level accuracy which fetch location only from wi-fi , cellular network or bluetooth and NOT from GPS.
and if device don't have SIM it needs to have wi-fi to get location or it will not call onLocationChanged till won't get location.
With the below solution, I am able to skip this Wifi Settings Dialog. This is not a perfect solution, but just a workaround till I find perfect solution.
Problem was, even after clicking "Ok" on Settings Dialog, it returned result as Activity.RESULT_CANCELLED. Reason was, Wifi was still turned off. So, as a quick fix, made a check in result Activity.RESULT_CANCELLED, that if I could get Location by checking LocationSettingStates.
You can use my MyLocationUtils.java and Base classes LocationActivity.java or LocationFragment.java. Happy Coding..!!
for getting location
MyLocationUtils.java:
public class MyLocationUtils {
public static final int REQUEST_CODE_LOCATION_SETTINGS = 123;
private static final int INTERVAL_IN_MS = 10000;
private static final int FASTEST_INTERVAL_IN_MS = 5000;
private static final int MAX_WAIT_TIME_IN_MS = 5000;
private static final int NUMBER_OF_UPDATES = 1;
private final MyLocationListener myLocationListener;
private final Context context;
private FusedLocationProviderClient mFusedLocationProviderClient;
private LocationCallback mLocationCallback = new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Location location = null;
if (locationResult.getLocations().size() > 0) {
location = locationResult.getLocations().get(0);
}
myLocationListener.onLocationSuccess(location);
stopContinuousLocation();
}
};
public MyLocationUtils(Context context, MyLocationListener myLocationListener) {
this.context = context;
this.myLocationListener = myLocationListener;
initializeFusedLocationProviderClient();
}
private boolean checkIfRequiredLocationSettingsAreEnabled(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
private void initializeFusedLocationProviderClient() {
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
}
public void stopContinuousLocation() {
mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback);
}
public void getLocation() {
checkIfLocationSettingsAreEnabled();
}
private void checkIfLocationSettingsAreEnabled() {
if (checkIfRequiredLocationSettingsAreEnabled(context)) {
getLastKnownLocation();
} else {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(getLocationRequest());
builder.setAlwaysShow(true);
SettingsClient client = LocationServices.getSettingsClient(context);
Task<LocationSettingsResponse> locationSettingsResponseTask = client.checkLocationSettings(builder.build());
locationSettingsResponseTask.addOnSuccessListener(locationSettingsResponse -> {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
getLastKnownLocation();
});
locationSettingsResponseTask.addOnFailureListener(e -> {
if (e instanceof ResolvableApiException) {
myLocationListener.onResolutionNeeded(e);
} else {
myLocationListener.onLocationFailure(e);
}
});
}
}
#SuppressLint("MissingPermission")
private void getLastKnownLocation() {
Task<Location> locationTask = mFusedLocationProviderClient.getLastLocation();
locationTask.addOnSuccessListener(location -> {
// Got last known location. In some rare situations this can be null.
if (location != null) {
myLocationListener.onLocationSuccess(location);
} else {
startContinuousLocation();
}
});
locationTask.addOnFailureListener(myLocationListener::onLocationFailure);
}
#SuppressLint("MissingPermission")
private void startContinuousLocation() {
mFusedLocationProviderClient.requestLocationUpdates(getLocationRequest(), mLocationCallback, Looper.getMainLooper())
.addOnFailureListener(myLocationListener::onLocationFailure);
}
private LocationRequest getLocationRequest() {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(INTERVAL_IN_MS);
locationRequest.setFastestInterval(FASTEST_INTERVAL_IN_MS);
locationRequest.setNumUpdates(NUMBER_OF_UPDATES);
locationRequest.setMaxWaitTime(MAX_WAIT_TIME_IN_MS);
return locationRequest;
}
public void resolveLocationSettings(FragmentActivity fragmentActivity, Exception exception) {
ResolvableApiException resolvable = (ResolvableApiException) exception;
try {
resolvable.startResolutionForResult(fragmentActivity, MyLocationUtils.REQUEST_CODE_LOCATION_SETTINGS);
} catch (IntentSender.SendIntentException e1) {
e1.printStackTrace();
}
}
public interface MyLocationListener {
void onLocationSuccess(Location location);
void onResolutionNeeded(Exception exception);
void onLocationFailure(Exception exception);
}
}
BaseLocationActivity.java:
public abstract class LocationActivity extends AppCompatActivity {
private MyLocationUtils myLocationUtils;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myLocationUtils = new MyLocationUtils(this, new MyLocationUtils.MyLocationListener() {
#Override
public void onLocationSuccess(Location location) {
if (shouldBeAllowedToProceed())
LocationActivity.this.onLocationSuccess(location);
}
#Override
public void onResolutionNeeded(Exception exception) {
exception.printStackTrace();
if (shouldBeAllowedToProceed())
LocationActivity.this.onResolutionNeeded(exception);
}
#Override
public void onLocationFailure(Exception exception) {
exception.printStackTrace();
if (shouldBeAllowedToProceed())
LocationActivity.this.onLocationFailure(exception);
}
});
}
protected void getLocation() {
myLocationUtils.getLocation();
}
protected void resolveLocationSettings(Exception exception) {
myLocationUtils.resolveLocationSettings(this, exception);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MyLocationUtils.REQUEST_CODE_LOCATION_SETTINGS) {
final LocationSettingsStates locationSettingsStates = LocationSettingsStates.fromIntent(data);
switch (resultCode) {
case Activity.RESULT_OK:
onResolveLocationSettingOk();
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to,
// or on Android Oreo 8.1 Wifi Settings were not satisfied inspite of clicking OK, that results on Activity.RESULT_CANCELED
onResolveLocationSettingCancelled(locationSettingsStates);
break;
default:
break;
}
}
}
protected abstract void onResolveLocationSettingOk();
protected void onResolveLocationSettingCancelled(LocationSettingsStates locationSettingsStates) {
if (locationSettingsStates.isLocationPresent() && locationSettingsStates.isLocationUsable()) {
onResolveLocationSettingOk();
}
}
private boolean shouldBeAllowedToProceed() {
return getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED);
}
public abstract void onLocationSuccess(Location location);
public abstract void onResolutionNeeded(Exception exception);
public abstract void onLocationFailure(Exception exception);
#Override
public void onDestroy() {
super.onDestroy();
myLocationUtils.stopContinuousLocation();
}
}
LocationFragment.java:
public abstract class LocationFragment extends Fragment {
private MyLocationUtils myLocationUtils;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myLocationUtils = new MyLocationUtils(context, new MyLocationUtils.MyLocationListener() {
#Override
public void onLocationSuccess(Location location) {
if (shouldBeAllowedToProceed())
LocationFragment.this.onLocationSuccess(location);
}
#Override
public void onResolutionNeeded(Exception exception) {
exception.printStackTrace();
if (shouldBeAllowedToProceed())
LocationFragment.this.onResolutionNeeded(exception);
}
#Override
public void onLocationFailure(Exception exception) {
exception.printStackTrace();
if (shouldBeAllowedToProceed())
LocationFragment.this.onLocationFailure(exception);
}
});
}
protected void resolveLocationSettings(FragmentActivity appCompatActivity, Exception exception) {
myLocationUtils.resolveLocationSettings(appCompatActivity, exception);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MyLocationUtils.REQUEST_CODE_LOCATION_SETTINGS) {
final LocationSettingsStates locationSettingsStates = LocationSettingsStates.fromIntent(data);
switch (resultCode) {
case Activity.RESULT_OK:
onResolveLocationSettingOk();
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
onResolveLocationSettingCancelled(locationSettingsStates);
break;
default:
break;
}
}
}
protected abstract void onResolveLocationSettingOk();
protected void onResolveLocationSettingCancelled(LocationSettingsStates locationSettingsStates) {
if (locationSettingsStates.isLocationPresent() && locationSettingsStates.isLocationUsable()) {
onResolveLocationSettingOk();
}
}
public void getLocation() {
myLocationUtils.getLocation();
}
private boolean shouldBeAllowedToProceed() {
return getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED);
}
public abstract void onLocationSuccess(Location location);
public abstract void onResolutionNeeded(Exception exception);
public abstract void onLocationFailure(Exception exception);
#Override
public void onDestroy() {
super.onDestroy();
myLocationUtils.stopContinuousLocation();
}
}
Related
I have simple activity with function that when called displays ble pairing request. After user confirms, startObservingDevicePresence("Some Mac address") is called. That successfully triggers CompanionDeviceService and I see logs that device appeared in range, but right after that onDestroy is called. App continues to run and there are no errors in logs. Has anybody used successfully these new android 12 api's (https://developer.android.com/guide/topics/connectivity/companion-device-pairing#keep-awake)?
MainActivity:
public class MainActivity extends ReactActivity {
private static final int SELECT_DEVICE_REQUEST_CODE = 42;
private static CompanionDeviceManager deviceManager;
private static AssociationRequest pairingRequest;
private static BluetoothDeviceFilter deviceFilter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
deviceManager = getSystemService(CompanionDeviceManager.class);
}
}
public void start() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
deviceFilter = new BluetoothDeviceFilter.Builder()
.build();
pairingRequest = new AssociationRequest.Builder()
.addDeviceFilter(deviceFilter)
.build();
deviceManager.associate(pairingRequest,
new CompanionDeviceManager.Callback() {
#Override
public void onDeviceFound(IntentSender chooserLauncher) {
try {
startIntentSenderForResult(chooserLauncher,
SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
#Override
public void onFailure(CharSequence charSequence) {
}
},
null);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
try {
deviceManager.startObservingDevicePresence("Some MAC address");
} catch(DeviceNotAssociatedException e) {}
}
}
}
}
CompanionService:
#RequiresApi(VERSION_CODES.S)
public class BleCompanionDeviceService extends CompanionDeviceService {
private static final String TAG = "BleReceiver";
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDeviceAppeared(#NonNull String s) {
Log.d(TAG, "DEVICE APPEARED INTO RANGE");
}
#Override
public void onDeviceDisappeared(#NonNull String s) {
Log.d(TAG, "DEVICE DISAPPEARED");
}
#Override
public void onDestroy() {
Log.d(TAG, "SERVICE DESTROYED");
}
}
This seems to be a race condition happening in CompanionDeviceManager, Android 13 should have fixed it.
I'm trying to get location information in ViewModel.
So I called requestLocationUpdates() by getting LocationManager at View.
I'm not sure requestLocationUpdates() works in viewmodel because callback method onLocationChanged() is not working.
I'm new on android so I don't know what's wrong in here.
Because there was no error on this code.
I guess the error is occuring because callback method onLocationChanged() is not getting called at
ViewModel.
How can make this work??
This is my code.
ViewModel Class
public class MAgencyViewModel extends ViewModel implements LocationListener {
private final String TAG = "MAgencyViewModel";
//이 클래스에서는 Model과 통신하여서 날씨 정보를 받아온다.
private MutableLiveData<ShortWeather> sw;
private MAgencyRepo maRepo;
private GeoInfo gi;
private GpsTransfer gpt;
// public void init(GeoInfo gi) {
// if (sw != null) {
// return;
// }
//
// maRepo = MAgencyRepo.getInStance();
// sw = maRepo.getWeather(gi); // this part is calling a weather api
// Log.i(TAG, "API Connection finish");
// }
public LiveData<ShortWeather> getWeather() {
return sw;
}
#SuppressLint("MissingPermission")
//이부분은 권한 재확인 안하게 해주는 부분이다. 따로 재확인을 안하는 이유는 Activity단에서 이미 확인을 거쳤기 때문이다.
public void locationUpdate(LocationManager lm) {
Log.i(TAG, "locationUpdate()");
// lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,this); //위치정보를 update하는 함수 이건 실제 기기용
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); //After this nothing is happening
}
//여기서는 이제 위치 정보가 변경 되었을때 진입하는 부분
#Override
public void onLocationChanged(Location location) { //This CallBack method is not working
Log.i(TAG, "onLocationChanged()");
}
//위치 정보 이용 권한 허가를 받지 못했을떄 호출 하는 부분
public void defaultLocation() {
//GpsTransfer 객체 생성
gpt = new GpsTransfer();
//GpsTransfer 위도와 경도를 원주로 설정
gpt.setxLat(76);
gpt.setyLon(122);
gpt.transfer(gpt, 1);
gi = new GeoInfo();
gi.setLon(gpt.getyLon());
gi.setLat(gpt.getxLat());
getTime();
if (sw != null) {
return;
}
//해당 정보를 API를 호출
maRepo = MAgencyRepo.getInStance();
sw = maRepo.getWeather(gi); // this part is calling a weather api
Log.i(TAG, "API Connection finish");
}
public void getTime() {
SimpleDateFormat dateSdf = new SimpleDateFormat("yyyyMMdd"); //년월일 받아오는 부분
SimpleDateFormat timeSdf = new SimpleDateFormat("HH"); //현재시간 받아오는 부분
Calendar cal = Calendar.getInstance(); //현재시간을 받아온다.
gi.setNowDate(dateSdf.format(cal.getTime())); //날짜 세팅
gi.setNowTime(timeSdf.format(cal.getTime())); //시간 세팅
/*
* 하루 전체의 기상예보를 받아오려면 전날 23시에 266개의 날씨정보를 호출해와야 한다.
* 따라서 호출 값은 현재 날짜보다 1일전으로 세팅해줘야 한다.
* */
cal.add(Calendar.DATE, -1); //1일전 날짜를 구하기 위해 현재 날짜에서 -1 시켜주는 부분
gi.setCallDate(dateSdf.format(cal.getTime())); //1일전 값으로 호출값 생성
Log.i(TAG, "DATE : " + gi.getNowDate());
Log.i(TAG, "TIME : " + gi.getNowTime());
Log.i(TAG, "CALL DATE : " + gi.getCallDate());
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}
In you view model
public class MAgencyViewModel extends AndroidViewModel {
private LocationCallback locationCallback;
public boolean requestingLocationUpdates;
public MAgencyViewModel(Application app) {
super(app);
locationCallback = new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
for (Location location : locationResult.getLocations()) {
// Update UI with location data
// ...
}
}
};
}
public void startLocationUpdates() {
requestingLocationUpdates = true
LocationServices.getFusedLocationProviderClient(getApplication())
.requestLocationUpdates(locationRequest,
locationCallback,
Looper.getMainLooper());
}
public void stopLocationUpdates() {
requestingLocationUpdates = false
LocationServices.getFusedLocationProviderClient(getApplication())
.removeLocationUpdates(locationCallback);
}
}
Now in your Fragment/Activity override on pause and onresume
Then call the startlocation and stoplction in your viewmodel
#Override
protected void onPause() {
super.onPause();
viewModel.stopLocationUpdates();
}
#Override
protected void onResume() {
super.onResume();
if (viewModel.requestingLocationUpdates) {
startLocationUpdates();
}
Notice setting and checking boolean requestingLocationUpdates if locationRequest is active
Hello I am building an app I would like to access the location of the mobile phone. However it seems that I can not grant the access fine location permission so my app doesn't function in Android M cellphone which I am testing.
Here is a part of my code:
public class LocationMainActivity extends AppCompatActivity {
private ListView listView;
private static final String EXTERNAL_PERMISSIONS[] = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET};
private static final int EXTERNAL_CODE=1;
private boolean perm;
private Cursor cursor;
private DatabaseAdapt dbAdapt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar4);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
listView = (ListView)findViewById(R.id.locationContainer);
dbAdapt = new DatabaseAdapt(this);
dbAdapt.open();
cursor =dbAdapt.loadLocationTitles();
String [] from = new String[]{DatabaseAdapt.LOCATION_TITLE};
int [] to = new int[] {android.R.id.text1};
CursorAdapter cursorAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,cursor,
from,to,0);
listView.setAdapter(cursorAdapter);
}
public void onDestroy(){
super.onDestroy();
cursor.close();
dbAdapt.close();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id==R.id.add_button);{
askForPerm();
if(perm){
Intent intent = new Intent(LocationMainActivity.this,LocationSecondActivity.class);
startActivity(intent);
}
else {
askForPerm();
}
}
return super.onOptionsItemSelected(item);
}
private void askForPerm () {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int w = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
int r = ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET);
if (w != PackageManager.PERMISSION_GRANTED && r != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, EXTERNAL_PERMISSIONS, EXTERNAL_CODE);
} else {
Toast.makeText(this, "Permissions already granted", Toast.LENGTH_SHORT).show();
perm=true;
}
}
#Override
public void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case EXTERNAL_CODE:
if(ActivityCompat.checkSelfPermission(this,permissions[0])==PackageManager.PERMISSION_GRANTED)
{
perm=true;
}
else {
Toast.makeText(this,"Permissions denied",Toast.LENGTH_LONG).show();
perm=false;
}
break;
}
}
}
In this activity I ask for the permissions and only then I can add a new location. The app moves to the next activity regularly, the permissions granted toast is shown even I install the app for the first time which seems a little weird. I use the same code to request the Write External and camera permissions in another feature of the app and it is working fine.
I have also included the code of my second activity where I would like to obtain the location coordinates
private EditText editTitleLocation, addressText, latText,longText;
private Button saveLocationButton, deleteLocationButton, navigateButton,findLocationButton;
private static final int UPDATE_TIME = 15000;
private static final int FASTER_UPDATE_TIME = 10000;
private GoogleApiClient myGoogleApi;
private Location location;
private LocationRequest myLocationRequest;
private static final int CHECK_CODE=1;
private double longitude,latitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_second);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
initializeViews();
buildGoogleApi();
}
private void initializeViews(){
editTitleLocation = (EditText)findViewById(R.id.editTitleLocation);
addressText = (EditText)findViewById(R.id.addressText);
latText = (EditText)findViewById(R.id.latitudeText);
longText=(EditText)findViewById(R.id.longitudeText);
saveLocationButton = (Button)findViewById(R.id.saveLocation);
deleteLocationButton = (Button)findViewById(R.id.deleteLocation);
findLocationButton = (Button)findViewById(R.id.findLocation);
navigateButton= (Button)findViewById(R.id.navigateLocation);
saveLocationButton.setOnClickListener(this);
deleteLocationButton.setOnClickListener(this);
findLocationButton.setOnClickListener(this);
navigateButton.setOnClickListener(this);
}
public void onStart(){
super.onStart();
if(myGoogleApi!=null){
myGoogleApi.connect();
}
}
public void onPause(){
super.onPause();
if(myGoogleApi!=null)
LocationServices.FusedLocationApi.removeLocationUpdates(myGoogleApi,LocationSecondActivity.this);
}
public void onStop(){
super.onStop();
myGoogleApi.disconnect();
}
private synchronized void buildGoogleApi(){
myGoogleApi = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
private void createRequest (){
myLocationRequest = new LocationRequest();
myLocationRequest.setInterval(UPDATE_TIME)
.setFastestInterval(FASTER_UPDATE_TIME)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(myLocationRequest);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(myGoogleApi,builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
#Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()){
case LocationSettingsStatusCodes.SUCCESS:
retrieveLocation();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try{
status.startResolutionForResult(LocationSecondActivity.this,CHECK_CODE);
}catch (IntentSender.SendIntentException e){
e.printStackTrace();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Toast.makeText(LocationSecondActivity.this,"Settings Change Unavailable",Toast.LENGTH_SHORT).show();
break;
}
}
});
}
private void retrieveLocation(){
try {
location = LocationServices.FusedLocationApi.getLastLocation(myGoogleApi);
}catch (SecurityException ex){
ex.printStackTrace();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case CHECK_CODE:
switch (requestCode){
case Activity.RESULT_OK:
retrieveLocation();
break;
case Activity.RESULT_CANCELED:
break;
}
break;
}
}
#Override
public void onConnected(Bundle bundle) {
createRequest();
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this,"Google Api can not connect",Toast.LENGTH_LONG).show();
}
#Override
public void onLocationChanged(Location location) {
this.location=location;
longitude=location.getLongitude();
latitude=location.getLatitude();
}
#Override
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.saveLocation:
break;
case R.id.deleteLocation:
break;
case R.id.navigateLocation:
break;
case R.id.findLocation:
if(location!=null){
latitude = location.getLatitude();
Log.e("MALAKIS",String.valueOf(latitude));
longitude=location.getLongitude();
latText.setText(String.valueOf(latitude));
longText.setText(String.valueOf(longitude));
}else {
if(!myGoogleApi.isConnected()){
myGoogleApi.connect();
Log.e("ELSE","fdsfddsd");
}
try {
LocationServices.FusedLocationApi.requestLocationUpdates(myGoogleApi, myLocationRequest, this);
}catch (SecurityException ex){
ex.printStackTrace();
}
}
break;
}
}
Here the location is object is null and the security location is triggered pointing that I dont have the fine location permission. So please can someone point me to the right direction? Do I have do ask again for permissions in the second activity?
The log is showing this below:
W/System.err: java.lang.SecurityException: Client must have
ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY
locations.
05-30 10:50:40.516 32177-32177/com.example.notepad.isidorosioannou.notepad
W/System.err: at android.os.Parcel.readException(Parcel.java:1602)
05-30 10:50:40.516 32177-32177/com.example.notepad.isidorosioannou.notepad
W/System.err: at android.os.Parcel.readException(Parcel.java:1555)
05-30 10:50:40.516 32177-32177/com.example.notepad.isidorosioannou.notepad
W/System.err: at
com.google.android.gms.internal.zzase$zza$zza.zza(Unknown Source)
05-30 10:50:40.516 32177-32177/com.example.notepad.isidorosioannou.notepad
W/System.err: at com.google.android.gms.internal.zzasg.zza(Unknown
Source)
05-30 10:50:40.516 32177-32177/com.example.notepad.isidorosioannou.notepad
W/System.err: at com.google.android.gms.internal.zzash.zza(Unknown
Source)
05-30 10:50:40.516 32177-32177/com.example.notepad.isidorosioannou.notepad
W/System.err: at com.google.android.gms.internal.zzary$1.zza(Unknown
Source)
05-30 10:50:40.516 32177-32177/com.example.notepad.isidorosioannou.notepad
W/System.err: at com.google.android.gms.internal.zzary$1.zza(Unknown
Source)
05-30 10:50:40.516 32177-32177/com.example.notepad.isidorosioannou.notepad
W/System.err: at com.google.android.gms.internal.zzaad$zza.zzb(Unknown
Source)
put following code in your askForPerm () method:-
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true); //this is the key ingredient
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
#Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
});
Try this!!It works in my application.
Does anyone know of an example of using the LocationServices.GeofencingApi?
All the android geofencing examples I find are using the deprecated LocationClient class.
From what I can see, the LocationServices class is the one to use, but there doesn't seem to be any working examples on how to use it.
The closest I've found is this post highlighting location update requests
UPDATE: The closest answer I've found is this git example project - but it still uses the deprecated LocationClient to get triggered fences.
I just migrated my code to the new API. Here is a working example:
A working project on GitHub based on this answer: https://github.com/androidfu/GeofenceExample
This helper class registers the geofences using the API. I use a callback interface to communicate with the calling activity/fragment. You can build a callback that fits your needs.
public class GeofencingRegisterer implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private List<Geofence> geofencesToAdd;
private PendingIntent mGeofencePendingIntent;
private GeofencingRegistererCallbacks mCallback;
public final String TAG = this.getClass().getName();
public GeofencingRegisterer(Context context){
mContext =context;
}
public void setGeofencingCallback(GeofencingRegistererCallbacks callback){
mCallback = callback;
}
public void registerGeofences(List<Geofence> geofences){
geofencesToAdd = geofences;
mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
#Override
public void onConnected(Bundle bundle) {
if(mCallback != null){
mCallback.onApiClientConnected();
}
mGeofencePendingIntent = createRequestPendingIntent();
PendingResult<Status> result = LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, geofencesToAdd, mGeofencePendingIntent);
result.setResultCallback(new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
if (status.isSuccess()) {
// Successfully registered
if(mCallback != null){
mCallback.onGeofencesRegisteredSuccessful();
}
} else if (status.hasResolution()) {
// Google provides a way to fix the issue
/*
status.startResolutionForResult(
mContext, // your current activity used to receive the result
RESULT_CODE); // the result code you'll look for in your
// onActivityResult method to retry registering
*/
} else {
// No recovery. Weep softly or inform the user.
Log.e(TAG, "Registering failed: " + status.getStatusMessage());
}
}
});
}
#Override
public void onConnectionSuspended(int i) {
if(mCallback != null){
mCallback.onApiClientSuspended();
}
Log.e(TAG, "onConnectionSuspended: " + i);
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if(mCallback != null){
mCallback.onApiClientConnectionFailed(connectionResult);
}
Log.e(TAG, "onConnectionFailed: " + connectionResult.getErrorCode());
}
/**
* Returns the current PendingIntent to the caller.
*
* #return The PendingIntent used to create the current set of geofences
*/
public PendingIntent getRequestPendingIntent() {
return createRequestPendingIntent();
}
/**
* Get a PendingIntent to send with the request to add Geofences. Location
* Services issues the Intent inside this PendingIntent whenever a geofence
* transition occurs for the current list of geofences.
*
* #return A PendingIntent for the IntentService that handles geofence
* transitions.
*/
private PendingIntent createRequestPendingIntent() {
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
} else {
Intent intent = new Intent(mContext, GeofencingReceiver.class);
return PendingIntent.getService(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
}
This class is the base class for your geofence transition receiver.
public abstract class ReceiveGeofenceTransitionIntentService extends IntentService {
/**
* Sets an identifier for this class' background thread
*/
public ReceiveGeofenceTransitionIntentService() {
super("ReceiveGeofenceTransitionIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent event = GeofencingEvent.fromIntent(intent);
if(event != null){
if(event.hasError()){
onError(event.getErrorCode());
} else {
int transition = event.getGeofenceTransition();
if(transition == Geofence.GEOFENCE_TRANSITION_ENTER || transition == Geofence.GEOFENCE_TRANSITION_DWELL || transition == Geofence.GEOFENCE_TRANSITION_EXIT){
String[] geofenceIds = new String[event.getTriggeringGeofences().size()];
for (int index = 0; index < event.getTriggeringGeofences().size(); index++) {
geofenceIds[index] = event.getTriggeringGeofences().get(index).getRequestId();
}
if (transition == Geofence.GEOFENCE_TRANSITION_ENTER || transition == Geofence.GEOFENCE_TRANSITION_DWELL) {
onEnteredGeofences(geofenceIds);
} else if (transition == Geofence.GEOFENCE_TRANSITION_EXIT) {
onExitedGeofences(geofenceIds);
}
}
}
}
}
protected abstract void onEnteredGeofences(String[] geofenceIds);
protected abstract void onExitedGeofences(String[] geofenceIds);
protected abstract void onError(int errorCode);
}
This class implements the abstract class and does all the handling of geofence transitions
public class GeofencingReceiver extends ReceiveGeofenceTransitionIntentService {
#Override
protected void onEnteredGeofences(String[] geofenceIds) {
Log.d(GeofencingReceiver.class.getName(), "onEnter");
}
#Override
protected void onExitedGeofences(String[] geofenceIds) {
Log.d(GeofencingReceiver.class.getName(), "onExit");
}
#Override
protected void onError(int errorCode) {
Log.e(GeofencingReceiver.class.getName(), "Error: " + i);
}
}
And in your manifest add:
<service
android:name="**xxxxxxx**.GeofencingReceiver"
android:exported="true"
android:label="#string/app_name" >
</service>
Callback Interface
public interface GeofencingRegistererCallbacks {
public void onApiClientConnected();
public void onApiClientSuspended();
public void onApiClientConnectionFailed(ConnectionResult connectionResult);
public void onGeofencesRegisteredSuccessful();
}
I want to get File Or Folder ID when that file is changed from the web. I gone through Documentation Listening for Change Events Git Hub Demo But i am not able to implement it.
In my app i am trying to listen to changes and modify those changes to local files also (sync).
In this test code i am just creating a folder and attaching a listner to the folder.
but when i rename folder from web Listner in not listening why?
1>I tried with this code
DriveId ddid = sky.getDriveFolder().getDriveId();
DriveFolder df = Drive.DriveApi.getFolder(mGoogleApiClient, ddid);
df.addChangeListener(getGoogleApiClient(), changeListener2);
2> And also with this line
DriveFolderResult sky = Drive.DriveApi.getRootFolder(getGoogleApiClient())
.createFolder(getGoogleApiClient(), changeSet).await();
sky.getDriveFolder().addChangeListener(getGoogleApiClient(), changeListener2);
class file.
public class MainActivity extends Activity implements ConnectionCallbacks,
OnConnectionFailedListener {
private static final int REQUEST_CODE_CREATOR = 2;
private static final int REQUEST_CODE_RESOLUTION = 3;
private static final int PICKFILE_RESULT_CODE = 1;
private ContentsResult contentsresult;
private GoogleApiClient mGoogleApiClient;
String EXISTING_FILE_ID = "";
int folderCreated = 0;
SharedPreferences prefs;
ArrayList<String> dbfileid = new ArrayList<String>();
ArrayList<String> dbfilename = new ArrayList<String>();
String fdd="";
DriveFolderResult sky;
private DriveId mFolderDriveId;
String isfolder;
SharedPreferences sp;
String Shared="Shared";
String folderid="";
int j=0;
String songfileid="";
String realid ="";
#Override
protected void onResume() {
super.onResume();
initDrive();
}
private void initDrive() {
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(com.google.android.gms.drive.Drive.API)
.addScope(com.google.android.gms.drive.Drive.SCOPE_FILE).setAccountName("shivrajp130#gmail.com")
.addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
}
mGoogleApiClient.connect();
}
#Override
public void onConnectionFailed(ConnectionResult result) {
// Called whenever the API client fails to connect.
if (!result.hasResolution()) {
// show the localized error dialog.
showToast("Error in on connection failed");
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (SendIntentException e) {
showToast("error" + e.toString());
}
}
#Override
public void onConnected(Bundle connectionHint) {
showToast("Inside Connected");
sp = getSharedPreferences(Shared, Context.MODE_PRIVATE);
Thread t = new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
showToast("Thread Started");
createSkyFolder();
}
});
t.start();
}
private void createSkyFolder()
{
// TODO Auto-generated method stub
try
{
showToast("creating Folder");
if(!sp.getString(isfolder, "false").contains("created"))
{
MetadataChangeSet changeSet = new MetadataChangeSet.Builder().
setTitle("Sky folder").build();
sky = Drive.DriveApi.getRootFolder(getGoogleApiClient())
.createFolder(getGoogleApiClient(), changeSet).await();
sky.getDriveFolder().addChangeListener(getGoogleApiClient(), changeListener2);
showToast("folder created");
sp.edit().putString(isfolder, "created").commit();
// To store secret ID string of file or folder so that we can later get a DriveId object.
realid = sky.getDriveFolder().getDriveId().encodeToString();
sp.edit().putString(folderid, realid).commit();
showToast("Real== "+realid);
}
Status s = Drive.DriveApi.requestSync(mGoogleApiClient).await();
if(s.isSuccess())
{
showToast("Success");
}
}
#Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent data) {
if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
mGoogleApiClient.connect();
showToast("Connected");
}
}
#Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
public void showToast(final String toast) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), toast,
Toast.LENGTH_SHORT).show();
}
});
}
public GoogleApiClient getGoogleApiClient() {
return mGoogleApiClient;
}
final private Listener<ChangeEvent> changeListener2 = new Listener<ChangeEvent>() {
#Override
public void onEvent(ChangeEvent event) {
showToast("Listening now");
showToast(String.format("The New Id Is %s", event.hasMetadataChanged()));
}
};
#Override
public void onConnectionSuspended(int cause) {
showToast("GoogleApiClient connection suspended");
}
}
Please provide any code snippet for the same.