I have a countdown timer that runs when a user presses a button, at the conclusion of the timer, the location of the device is sent to another device for which the user enters a phone number for. Here is my timer code::
private void initTimer() {
if(timer==null)
{
long totalTime= (PersistData.getIntData(con, AppConstant.selectedMinute)*60*1000)+(PersistData.getIntData(con, AppConstant.selectedseconds)*1000);
timer=new CountDownTimer(totalTime, 1000) {
public void onTick(long millisUntilFinished) {
timerView.setText(getFormatterTimerText(millisUntilFinished));
if(millisUntilFinished<60*1000)
{
continueFlashAndAudio();
/*
refresh GPS Location again
*/
updateGPSLocation (millisUntilFinished);
}
}
public void onFinish() {
showFinishView();
}
};
}
}
When the timer runs, it is supposed to run the "updategpslocation" function:
private void updateGPSLocation(long millisUntilFinished) {
Log.e("current time remaining", " time: "+millisUntilFinished/1000);
if(millisUntilFinished/1000==30)
{
Log.e("30 seconds remaining", "get locaiton");
getLocation();
}
if(millisUntilFinished/1000==10)
{
Log.e("10 seconds remaining", "get locaiton");
getLocation();
}
}
The issue I am having is that about half the time, the location will not be updated and it will send a location that was previously fetched to the other phone.
Here is my code to get the location:
private void getLocation()
{
GPSTracker tracker=new GPSTracker(con);
if(tracker.canGetLocation())
{
Location lastLocation=tracker.getLocation();
PersistObject.apply(con,AppConstant.LOCATION,lastLocation);
Log.e("Location is refreshed", "5 seconds remaining");
}
}
I am extremely confused as to why this is happening, I have spent hours on this and have hired a couple freelancers to help and nothing they have done has been able to remedy the issue. Half the time the code will work fine and it will send the new location of the device, and half the time it will send the location that the device returned previously. I move the phone around to test new spots and I am running this on three different phones and it happens on all of them intermittently. Any ideas would be GREATLY appreciated.
Related
I am building an app for video editing...
I want to give some free minutes for 1st time using of app . User can edit the videos until the completion free minutes .
These minutes count based on duration of video which is editing..
After that they have to purchase for further any duration of videos..?
How can I limit user to use some until free minutes?
Till now I didn't used any online service in my app. Everything worked on device.
Any way to implement it on offline.?
If not possible..
What can I do to tackle it..?
I not a native English speaker..
Try to understand... :)
This is a timer that will trigger when no time is left.
It works completely offline .
This goes in the Class outside any other method where you usually declare your variables: (Note that you can set the time you want in millis. I chose 10000ms so 10 seconds as an example)
private CountDownTimer timer;
private long secondsLeft = 10000; // how much milliseconds of
// free time you want to have
//
And this goes where you want to start the time.
timer = new CountDownTimer(secondsLeft, 1000) {
public void onTick(long millisUntilFinished) {
secondsLeft = millisUntilFinished/1000;
}
public void onFinish() {
// The time is up! User has spent the time
}
}.start();
Then add these (or if you already have these methods then add the code in them to your ones.
#Override
protected void onPause() {
super.onPause();
timer.cancel();
}
#Override
protected void onResume() {
super.onResume();
timer = new CountDownTimer(secondsLeft, 1000) {
public void onTick(long millisUntilFinished) {
secondsLeft = millisUntilFinished/1000;
}
public void onFinish() {
// The time is up! The user has used the app for your amount of time
}
}.start();
}
Note: To make it survive an app restart or a phone restart you have to save "secondsLeft" to your device and load it when the app starts.
I've a TextView where I want to set a message according to time ( hour of day). To achieve this I'm using-
Thread t2 = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(3600000);
handler.post(new Runnable() {
#Override
public void run() {
int hour=Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
update(hour);
}
});
}
} catch (InterruptedException e) {
}
}
};
t2.start();
where update(hour) is used to update the message.
This works fine, however, the updating is done depending on the launching time. For example, the message should update at time 20:00. But if the app is launched at 19:59, the updating takes place at 20:59.
If I use Thread.sleep(1000) it works just as expected. But I feel like wasting resource by running the thread every second just to look for a 1 hour event. Is there any better way to do this?
Surely you need to check the current time, get the minutes past the hour, then work out from there when the next hour 00 will come. Then, just sleep time for those minutes, then for all the hours after that sleep for the full hour.
Take a look at AlarmManager.
The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.
i build app which give Location update to server in background using service and broadcastreceiver.i put timer(Run every X Minute) which run method of location.it include fatch lattitude,longitude and update to server with API Call.
I have a problem and quastions . there is a list of this.
1.my timer goes sleep when device is in standby mode or sleep mode
2.i have to update location if user last location and current location distance 500m or greater.
3.I want to know how much minute or second tack device for going sleep mode?
4.my timer run perfactly in running devoce mode than why stop or sleep in device sleep mode?
my timertask
class TimeDisplayTimerTask extends TimerTask {
#Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
#Override
public void run() {
location();
notifyUser2();
Log.d("timeeeeeeee22222222", "time22222222");
// display toast
}
});
}
//in oncreate
if (mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
Is the currect Way to timer run? if yes than Why it Sleep while device in standby mode or sleep mode?
Help me i searched lots of sites but can't get it.
Thanks in advance
I'm new on android application and i'm creating an app that shows a video using a VideoView, I use a CountDownTimer to search each 15 seconds for video updates and play another video. My app works very well during 2 or 3 hours but after this my app just stop, the application doesn't close but is stopped and I must close and open again. What should I do? Thanks.
It literally stops on the screen, it stops the timer and video view.
Here is the code that i'm using to count time to call functions to show video and time.
new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
time++;
downloadTime++;
if(downloadTime == 2){ // Should be executed each 2 seconds
DoDownloads();
downloadTime = 0; //
}
showPlayer();
showTime();
}
public void onFinish() {
this.start();
}
}.start();
I can't show any log because this happens after some hours and can be 2 hours or 10 hours, and i'm testing on tablet.
We would need the code of the rest of your functions, like DoDownloads(); showPlayer(); showTime();
anyways, i wouldnt restart the countdown that way. Id try with a handler and postdelayed(Runnable), like:
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
#Override
public void run() {
time++;
downloadTime++;
if(downloadTime == 2){ // Should be executed each 2 seconds
DoDownloads();
downloadTime = 0; //
}
showPlayer();
showTime();
handler.postDelayed(this, tiempo);
}
};
handler.postDelayed(runnable, tiempo);
And also, you should debug the app and copy/past the log cast in the moment it fails, and give us some information about in what point the execution wsa
Details:
I have a service that needs to do the following:
- listen constantly for GPS locations, with 50 meters as parameter (works fine) and send it to a server
- each 60 seconds, if no GPS location was read, start listening for a Network location and send it to the server
This may sound weird, but this is the project requirement. So the user is constantly being tracked using GPS. When he stops, or GPS is not locked, or is inside a building, every 60 seconds start a quick Network location read, and send this location to the server. As the service uses the same thread as the main app, each server update is done in its own thread. And another important thing: each location read should be sent one after another, so for instance if the user is driving, and multiple reads are done, each should be send to the server, after the previous one has been sent. That's why I decided to use ScheduledExecutorService as I can submit threads and they will be execute one after another.
Here is the code:
private ScheduledExecutorService scheduleTaskExecutor;
Handler locationHandler = new Handler();
private Location lastNetworkLocation;
#Override
public void onStartCommand() {
scheduleTaskExecutor = Executors.newSingleThreadScheduledExecutor();
//prepare to execute Network reading every 60 seconds
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
initNetworkReadings();
//usually a network location read is done almost instantly
//however after 5 seconds I check if a location has been read by the Network listener inside the locationRunnable
locationHandler.postDelayed(locationRunnable, 5000);
}
}
}, 60, 60, TimeUnit.SECONDS);
locationRunnable = new Runnable() {
#Override
public void run() {
if (lastNetworkLocation !=null){
//send location to the server if valid
}
lastNetworkLocation = null;
}
}
}
private void initNetworkReadings() {
locationManager.removeUpdates(locationListenerNetwork);
try {
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (isGpsEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
}
}
LocationListener locationListenerNetwork = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
lastNetworkLocation = location;
// stop listening for Network locations
locationManager.removeUpdates(locationListenerNetwork);
}
...
}
Each time I read a GPS location I add it to the threads queue as:
scheduleTaskExecutor.submit(new Runnable() {
#Override
public void run() {
updateLocationOnServer(readLocation);
}
});
The problem I have is that the Network location listener never gets onLocationChanged() called,when I use it like in the code above, in the Runnable. BUT if I add on service start, the initNetworkReadings(), I get onLocationChanged() fired right away. So I believe it has something to do with being used in scheduleAtFixedRate.
What do you think it could be the problem ? Anything bad in the way I thought of the workflow ?
What if you try to set up your repitition with a Handler and a Runnable instead of scheduleAtFixedRate()?
Handler h = new Handler();
Runnable run = new Runnable(){
//Do your repeititive work here!
h.postDelayed(run, 60 * 1000);
};
h.post(run);
Same problem,
I have a background service that detect locations.
If network is avaible, it uses networks, otherwise it uses gps.
It works well with many smartphones
( nexus s 4.1, galaxy nexus 4.2, galaxy note) , but with Galaxy s3 jb (4.1) network location never rises any location.
Same code with gps locations, works just fine.