I'm using Android Studio to create an app that utilizes google maps api. I am having trouble validating if the user input is an actual location.
i.e if the user enters "fdfdfaef", program crashes.
i have the following code to store the user "location" input:
address = geocoder.getFromLocationName(location, 1)
Any help on how to check if the input is valid or not or at least to prevent a crash. Thanks and appreciate it.
Since you haven't posted any code, I will go out on a limb and suggest that you might be doing this on your main UI thread.
From the doc,
The query will block and returned values will be obtained by means of
a network lookup. The results are a best guess and are not guaranteed
to be meaningful or correct. It may be useful to call this method from
a thread separate from your primary UI thread.
Try doing a async task that requests information using the Geocoding API. On getting a result, update the UI. Offloading to an AsyncTask will prevent the crash, at the very least.
Does that help you?
Related
I'm trying to write a discord bot using JDA, and while I can get the presence of the bot fairly easily, I cannot get the presence of a selected user. An example use case for this might be the user typing in a command like !game and then the bot sends a message telling the user what game they're playing. As far as I know, the bot cannot get the user's game activity without getting the user's presence, and I do not see a way to do so. If I missed it in the documentation, please link the method/class.
After a bunch of digging and documentation reading, I have come to a solution. There is no way to get a presence like I was thinking, but since my original intent was to get the game activity, this solution works. First, the bot must have guild presence permissions granted through the developer portal. Then we need to include the line jdaBuilder.enableIntents(GatewayIntent.GUILD_PRESENCES); before we call the build method in our main method. Finally, to get member activities, we need to include jdaBuilder.enableCache(CacheFlag.ACTIVITY);. This lets us use the member.getActivities() method. Generally speaking, getting data associated with specific users requires enabling guild presences.
I'm curious if it's possible to trigger a specific piece of code when a REST API its values changes. Actually a sort of realtime update a mechanism.
If it's not possible, what's a better to do it?
The idea is when I push on a button (on my android device) a text will appear in a game (Java Desktop).
Thank you!
Your question is not clear please rephrase it.
As you said you want that has to be done realtime.
Possible three approaches:
In android there are connection APIs with which you can connect your desktop(must have WiFi enabled) with phone. Then you can send or receive files and messages directly.
The other way is to have Push notifications but it's not guarantees realtime fast some time gets delayed otherwise performs good always.
Other way is to keep the connected socket open for listening changes from phone side to desktop side once connected but it's more costly.
Hope this gives you a direction, rest you can search over internet.
Why not just make a simple setter?
When you get a rest command to update something (for example some int value) you can do:
public void setValue(int newValue)
{
value = newValue;
doAction(newValue);
}
define the doAction as you want
If you need asynchronous update, use the thread. Learn multithreading
Check the link to see if your requirement could be completed with the java thread concepts. http://www.javatpoint.com/multithreading-in-java
I am creating an app that checks for user locations every half an hour and updates the location of the user in the local database and then runs CRUD queries based on the user's location even when the app is not running. How do i do it ?
I have referred to this http://techtej.blogspot.com.es/2011/03/android-thread-constructspart-4.html article and i am still confused about which is the correct approach for my result ?
There are 4 options according to the article for what i intend to achieve according to me
1) Service : But since i feel it would be a long operation with the local database, i feel i should ignore this one.
2) IntentService : This cannot perform multiple tasks, so i feel this one also should be avoided for me as i have to get the location of the user and scan the database , update the database (3 tasks)
3)Thread : I am not sure how to call this when the app is not open
4) AsyncTask : I am not sure how to call this when the app is not open.
Basically i looking for something like a CRON JOB that runs on a local database while working on the location data.
It would be great if you could link me up to some tutorials and answer with a simple example to make me understand the difference of all 4 methods.
// editted on 16 March :
I have read something about a JobScheduler which is introduced in the API 21, but not sure if it also supports till Gingerbread and is it the right approach for my question
Thanx
When recording the users position use a service with a notification. Just for the sake of creating a morally responsible app that informs the user the app is tracking them. The service by definition runs in the background.
A fused location provider with setinterval(long) 30 minutes gets the interval. Set fastestInterval() to a minute to receive GPS data when other apps are using the GPS.
Have you considered using a SyncAdapter. Its best to schedule jobs at fixed interval and also optimized for battery usage. Also, once started, it can run independently of the app. As per your requirements, I believe this is best suited for your need. You can read about this here. This also removes the corner case of starting the service (generally used) when your device is restarted. Your app will still continue running the scheduled job even if the device gets restarted.
In the SyncAdapter you have to use a ContentProvider so wrap your DB inside a ContentProvider. Also, preferably use a CursorLoader to run longrunning tasks on DB. You should read about CursorLoader. This is a great way to access your resources. Also, you can define an Observer Design Pattern which Observes for changes in a DB and will perform a task when changes are made in DB. This can also be used inside your application itself and also inside SyncAdapter. Cursor Loader is best preferred for background work on DB. You can perform all CRUD Operations using a CursorLoader and ContentProvider.
This cannot perform multiple tasks
Yes, it can. It has only one thread, and so it can only do one simultaneous task.
i have to get the location of the user and scan the database , update the database (3 tasks)
I have no idea why you think that is three tasks. You cannot do them simultaneously.
Your bigger problem with IntentService is that getting location data is asynchronous, and IntentService is not well-suited for calling APIs that themselves are asynchronous.
But since i feel it would be a long operation with the local database, i feel i should ignore this one.
The point behind any service is for "a long operation".
Basically i looking for something like a CRON JOB that runs on a local database while working on the location data
Use AlarmManager to trigger a WakefulBroadcastReceiver, which then triggers a Service. The Service, in onStartCommand(), forks a background thread to (asynchronously) retrieve the location and update the database. The Service can then call completeWakefulIntent() on WakefulBroadcastReceiver, plus stopSelf() with the startId received in onStartCommand() for this work, plus allow the thread to terminate. If no other commands were received in the interim, the service will shut down.
I think you are looking for something similar to WakefulIntentService. This handles all your cases completely.
You can do your location and db related work inside doWakefulWork() of said implementation.
I've done what you are looking for, both with GPS and non-GPS.
The project I took as staring point for the non-GPS solution already does all you need, and is battery-friendly (credits should go to Kenton Price):
https://code.google.com/p/little-fluffy-location-library/
Take a look at it, it works like a charm. Just run it in any device. If you need any help customizing just let me know.
Just edit the "onReceive" method in the "TestBroadcastReceiver" to update your DB.
If you need the GPS solution let me know too, but I dropped it for being a battery killer!
Hope it helps.
1. I think for this requirement, Thread and inside it AsyncTask -- this structure will be useful.
In link provided by you, it is mentioned very nicely here
2. For location related blog, you can check useful materials here :
(1) Difference between Google Map Distance and Directions API
(2) Check this answer also
Hope this will help you
I have an android application and in one of my activities I am making a call to get say "Customers", this call is made to an external API, when I get the response I get it as a JSON object. The problem i am having is that I have a ListView in the activity and when you click on of its items it shows you the details but then when you hit the back button I have to make the call again to populate it. In Samsung Galaxy 4S it seems to keep the data of the list view but in the HTC android incredible it's blank. So what I did is, make it rebind OnResume(), this fixed the issues for both BUT the consequence is making another call to that server. When its 10 or 100 customers it doesnt matter but I know that there are some accounts that have up to 5000 and I am sure it will crash.
What are my options to improve performance on this issue with Android?, I tried a static variable but at some point that object got cleared too.
How do Android applications usually handle this cases where the data is retrieved from API's and they need to be stored through out the application and there is no need to make another call for the same information?, I was thinking on static object but i want to make sure I do this the right way.
You have a couple of options.
1) You can cache the data in memory. For example you can make a static cache or cache the data within the Activity or the App object. If you are doing this in only one view and if it is not a lot of data, this might be an ok solution. However, if you have to do this for many activities and there is a lot of data that has to be cached, you might want to go for option 2. Also storing data in memory in android, does not mean it won't be garbage collected (in some cases, even if you have a reference to it.)
2) You can cache the data in the internal storage and refresh it from time to time.
You can find more info about the internal storage and how to use it here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Basically, you store the response within the internal storage under a specific identifier. The next time you open the activity, you check if the storage has data for that identifier and if yes, you read it and display it. If no, you make the API call.
Keep in mind, that you will have to refresh the cache from time to time.
I had the problem with ListViews on my application too. What I did is that I wrote a custom adapter and that solved the issue..
However the thing you can do is to make a global variable and save the returned results to it. When your application wants to call the server, check the variable, if it's null make the call, if it's not then just draw the ListView with the already fetched data..
Keep in my mind, to implement a refresh button, you need to skip the check.
I have been developing an application to download whole calendars from all users in domain and save them in ICS format. The app is written in Java. I get access using 2L OAuth. So far I'm able to get most of calendar's data, excluding exceptions from recurrent events. Google API docs say that every recurrent event should contain a list of recurrence, including EXRULEs. But when I call the API I got only recurrent rule without exception.
It there any way to get these exceptions?
You get the exceptions as regular item and
event.getOriginalEvent()
will return reference to reccuring event.