Android: Setting the location - java

I want the place picker from the google places API to basically start in a fixed location in the AVD. If I use google maps or place picker it tells me "unable to determine current location", both my wifi and gps are turned on. I know that in simulation you need to give a location.
I accessed the extended controls for the AVD and put in lat and long coordinates, I then click send, I believed this was how to send 1 location to a device. Doing this makes no changes to my devices location, what do I have to do to just send 1 location that google maps and place picker will always start from?
My extended controls:
And this is my place picker:
select this location is not the values I sent and it starts me at an incorrect location.

Six months ago when I was developing an app that used a score of Map related google services, the emulator was unable to utilize google-services. The consequence was that while I could add services, they would not function without the google-services library functioning on the AVD. In short 6 months ago there were no functional AVD images that allowed any google map/location services to work.
The solution (unfortunately) was to utilize physical android devices instead of the emulator. I don't know if there are still no AVD images that work with google-services - but you might want to start by checking if google-services functions with whatever AVD image you are using. If not - you may be out of luck.
Specifically - com.google.gms:google-services:3.0.0 (and any earlier version) did not work through the Android Studio emulator (AVD) 6 months ago.

Related

get a location once in the background at any time Android Studio Java

I want to get the location of the device once when the service is running in the background (the tracker monitors the change in the database and then executes the code that should get the geolocation of the phone). I encountered a problem: the program receives location data when the application is running, but when it goes into the background, the location data stops being received in a few seconds. I tried all the codes and options that I could find, but everything stops working when the program goes into the background. For this I use AndroidStudio Java. So how do I implement this and is it even possible? Thanks.
On Android there are two types of Services- foreground and background. Background (the default) on modern Android are killed 2 minutes after your app is no longer in the foreground. Foreground services are kept for longer, require you to have a notification so the user knows you're tacking him (think of Uber and the notification you can't swipe away you get while its running), but can still be killed for resources if other apps need it. You cannot rely on any Service running permanently.
So the answer is going to be either Foreground Service, or its going to be a completely different architecture for your program. The second really depends on exactly when and why you want to get the location.
There are many limitations on getting on getting location in background, refer to this
Do you target API level 29 or up? if yes, have you add ACCESS_BACKGROUND_LOCATION permission in manifest? If this is not declared, app can only access location while in foreground.
Even after declaring ACCESS_BACKGROUND_LOCATION permission, app can only get location data a few time in an hour due to limitations. Maybe you can consider using foreground service instead to avoid such limitations.

Camera intent open the camera app (only the first time)

Here is the context:
In my app, I need to capture images, that's why I use camera intent.
This feature works perfectly, but only if the camera app was already been opened once before
Indeed, this issue appears only on a new device, or a new emulator
Here is the problem:
When I want to capture an image, the native camera app just open, giving to me some hints to how take pictures. That's not the camera app IN my app, but the camera app alone. I need to close this camera app, back to my app, and trying again
When I visit the camera app at least one time in the device's life, there is no issue
Here is what I'm looking for:
I want this camera app open in my app, even if it's the first time
I use the classic and official way to captures images
https://developer.android.com/training/camera/photobasics
EDIT
I think the problem is because at the first time, the camera app asked for the location permission. So we need to open the intent without asking this permission, to keep the focus
When I want to capture an image, the native camera app just open, giving to me some hints to how take pictures.
There are ~26,000 Android device models. These ship with dozens, if not hundreds, of different pre-installed camera apps. The behavior of any of those apps on first run of that app will vary by app. Moreover, the decision of how those apps behave when they are first run is up to the developers of those apps, not you or me.
That's not the camera app IN my app, but the camera app alone
It is never "IN [your] app" if you are using ACTION_IMAGE_CAPTURE, as your question suggests that you are. It is always a third-party app, one of many pre-installed ones, or one of the user ones. And, again, the behavior of those apps are up to their respective developers.
I want this camera app open in my app, even if it's the first time
Sorry, but that is not under your control.

What is the use of a service when using the Location API Android?

I managed to get a location object as per the documentation available in the Android Developers website (using the Location API offered by Google Play Services). Although I have specified service as the documentation instructs, I am never binding it/ starting it/ using it anywhere. I am doing something wrong?
No, you are not doing anything wrong. It is a requirement for devices with Android 10 (API level 29).
As Google Docs Says
When a foreground service is running, the system raises user awareness
by showing a persistent notification. Your app retains access when
it's placed in the background, such as when the user presses the Home
button on their device or turns their device's display off.
So basically if you are using location it will just show the notification to user if they have android 10 i.e API level 29

How can I disable and enable a USB OTG connection to power a hardware device on and off?

I am working on an android project where I use a phone to power an external device. Through a USB-OTG connection, I can power the external device.
I want to know if I can write a program on android studio where I can press a button in the app to toggle the power to this external device (turn it on and off).
I've seen it done before on an older Samsung Galaxy 3 phone (on a much older version of android) by updating data in a file; specifically this file:
/sys/devices/virtual/host_notify/usb_otg/mode
But I'm trying to write the software so it is more generic and can work on any new android device.
Is there a way to do what I need?
I've tried finding a generic way to do this but it doesn't seem possible because the only way I've seen it possible is by using shell commands to toggle exposed kernel bits through root files.
I have concluded it's likely impossible to make generic code for this as most android phones root files are organised differently. So you need to discover the specific file each time you want to use a different phone for the software.
I don't think it would be feasible for myself to create a database of different brands of phones root file structures because new phones come out all the time with new structures which would require too much maintenance on my behalf.

How to make an app unclosable?

After a user opens my application I don't want them to be able to get out. So when they press the home or back button it doesn't let them exit the app. I know there are ways using a service to keep the app always running in the background, but I don't even want them to even exit.
Is this even possible, if so how? Without having to hack the kernel.
I know this sounds odd, but it is not for a commercial app, but for my lab, it will never be on GooglePlay. The tablets will be given to little kids and they will take a test on it, so I can't have them using youtube or anything else.
Related post: Android, How to make the task of the app unclosable? Only closable by task killing
The answer by CommonWare seems to indicate that kernel hacking is the only way, but is it?
Also I am using Android L.
there is no official support yet - there will be support with android L via the Task locking API
The L Developer Preview introduces a new task locking API that lets
you temporarily restrict users from leaving your app or being
interrupted by notifications. This could be used, for example, if you
are developing an education app to support high stakes assessment
requirements on Android. Once your app activates this mode, users will
not be able to see notifications, access other apps, or return to the
Home screen, until your app exits the mode.
https://developer.android.com/preview/api-overview.html
EDIT: as you said you use L this got obsolete - but I leave it here as a hint for others
said that - you are also able to get this kind of work without root today if you have to ( e.g. kiosk mode app on a certain hardware ). It is not simple - and no solution that works for all devices - but if you need it you can get it work with tricks like:
- reacting on home-screen intent ( and setting it as default )
- when you go to background - bring your self foreground again
- ..
It seems google has some thing called COSU for setting up single-purpose devices
Android 5.0 Lollipop introduced two new ways to configure Android
devices for a single purpose:
With app pinning, the device user can temporarily pin specific apps to the screen.
With lock task mode, a user can’t escape the app and the Home and Recents buttons are hidden. Additionally, lock task mode gives the IT
administrator a more robust way to manage COSU devices, as discussed
below.
and
As an IT administrator, you can configure Android 6.0 Marshmallow and
later devices as corporate-owned, single-use (COSU) devices. These are
Android devices used for a single purpose, such as digital signage,
ticket printing, point of sale, or inventory management. To use
Android devices as COSU devices, you need to develop Android apps that
your customers can manage.
from here
and there is a code lab for it here
for earlier versions of android here i founded How-To Create a Working Kiosk Mode in Android which shows some hacks for disabling buttons and restarting the application after boot and lock. note that most of these hacks, don't work on android 6 and later

Categories

Resources