I want to test if a service will still run after lets say, 5 days has passed. I do not want to just set the phone's time, I want the phone to think that the time has passed so that it will get rid of background apps, etc.
How do I do that in adb?
I want the phone to think that the time has passed so that it will
get rid of background apps, etc.
Android can actually finish your application only and not other app.
To get rid of background apps, you can use Task Manager but that's what manual process.
If you have background service running over 5 days, you can actually stop your service, use AlarmManager for it.
As your question is not properly clarified, there could be multiple solution to your problem, of which i have tried to address.
I use the following code in my app to get the current date:
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
AndroidDate = df.format(c.getTime());
Now with that said, all you can do is set the AndroidExpiryDate (string) and set your own date of expiry:
AndroidExpiryDate = "2016-02-28"
Then you can just compare current date (AndroidDate) with expiry date (AndroidExpiryDate ) using if statement:
if ("AndroidDate".equals("AndroidExpiryDate") )
{
//code to get rid of background app
}
Related
I tried:
TimeZone.getDefault();
I tried:
Calendar mCalendar = new GregorianCalendar();
TimeZone mTimeZone = mCalendar.getTimeZone();
I tried:
Calendar c = Calendar.getInstance();
mTimeZone = c.getTimeZone();
No matter what I try... it gives me a default of 0 and GMT. My app just cannot seem to get the time zone (it should read -4 or ET, i.e. eastern USA time zones)
The strange thing is... my clock displays the correct time on my phone.
the "use automatic time zone" is checked in settings of my android device.
So why can't I get the local time in my app? how does the Android clock able to achieve this but not me?
I have checked online and cant seem to find anything. Is there at least a way to sync with the clock app and receive it's time to display on my app as well? Is there ANY way to get the correct time on my app?
System.getMilliseconds() return the time since epoch, which would only function as the current time in areas that use GMT. (As long as what you are using doesn't make it's own conversion)
To get local time in milliseconds since epoch, you can use this function:
// Kotlin
fun getLocalTime(): Long
{
val currentDate = Calendar.getInstance()
return currentDate.timeInMillis + TimeZone.getDefault().getOffset(currentDate.timeInMillis)
}
Which in Java would probably look like this:
// Java
static long getLocalTime()
{
Calendar currentDate = Calendar.getInstance();
return currentDate.getTimeInMillis() + TimeZone.getDefault().getOffset(currentDate.getTimeInMillis());
}
The function takes the time since epoch and adds to it the timezone offset of the phone's local timezone.
I am designing a birthday based Android app. This app check current system date, if date is birth date, app will start a music and allow other activities. Else, the app will give an error message to wait until birth date and exit.
Below is the sample code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateobj = new Date();
Date date1 = sdf.parse(sdf.format(dateobj));
Date date2 = sdf.parse("2016-05-30");
if (date1.compareTo(date2) >= 0) {
// Start a music
//Other task
}
else
{
// You need to wait message
}
Now, I want this condition to be checked after every 60 seconds (1 minute) automatically, even if app is not open [Probably alarmManager might help, but I am not sure how] , something like a background task. This task should be repeated until condition is true.
If user opens the app before the birth date, an error alert is displayed and app is closed.
You should use the AlarmManager setRepeating function to achieve that. Check this link for details - https://developer.android.com/training/scheduling/alarms.html
As for your requirement of running the code even when it is not open, you should make use of BroadcastReceiver. You need to pass your BroadcastReceiver as a PendingIntent to the AlarmManager.
hello my bro & sis i am new in android, currently i am working in android database site. my application user used app from the multiple country, So my problem is how can i insert current time of every user from different locale wise when user perform any database transaction, even if user's android mobile device current time is not proper set yet i want to insert current time in database of his region.
Note: currently i am using MySQL database
Now i am insert current time of user i this way
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).concat(" 00:00:00");
but this is working for only one locale which is default time zone is set. So, Please somebody help me how to resolve my problem.
For saving time inside a database, the best and most country compatible way is to use the time as Long. For example, create a calendar instance:
Calendar cal = Calendar.getInstance();
Long timeToSave = cal.getTimeInMillis();
Then You have to save this Long value inside the database. With this value, You can set all Dateformats for all time zones, also AM/PM values and so on.
I'd recommend to keep the DateTime type in SQL as Long.
Please try this code:
Long now = System.currentTimeMillis();
If you ever need to, you will be able to convert the Long value to string for the current locale:
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.LONG, Session.currentLocale);
String date = dateFormat.format(yourStoredDateAsLong);
or
String date = dateFormat.format(System.currentTimeMillis());
in my application i need to get current Date and time, every time the user inputs data with it.
I know i can use System.currentTimeMillis(), but it can give me wrong time(because it gives system time, witch can be edited by user)
So i see the only way is to call server for current time, when the user makes data input. but i am not sure that internet connection is always awailable.
Is there any way to get current time (not system time) in android, without using internet connection?
If you don't want system time you need some other source then.
There are a few possibilities that I know:
Get it from web - Internet needed
Get it from router - enabled wifi needed (NTP)
Get it from GPS - GPS fix needed
All of these aren't very helpful I believe. I don't think you can find a way of getting current time without connecting so something externally.
In my opinion you should use system time and assume it's set correctly. When it's not your application shouldn't crash and should gently know user that he has wrong content because of wrong dates ...
I believe there's no way to get the current system time without the timezone.
A good approach would be getting the current system time first
long time= System.currentTimeMillis();
And then getting the correct TimeZone to handle it
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
long dateInMillis = System.currentTimeMillis();
String format = "yyyy-MM-dd HH:mm:ss";
final SimpleDateFormat sdf = new SimpleDateFormat(format);
String dateString = sdf.format(new Date(dateInMillis));
Use dateString as your variable which contains current date time as timestamp.
Well, I googled about your topic, and i got logic solution but not tested:
" Use The network-provided values ", in the android phone settings, it;a as shown in the picture bellow:
The screen I show is DateTimeSettings. The checkbox "Use network-provided values" is associated to the shared preference String KEY_AUTO_TIME = "auto_time"; and also to Settings.System.AUTO_TIME
This settings is observed by an observed called mAutoTimeObserver in the 2 network ServiceStateTrackers: GsmServiceStateTracker and CdmaServiceStateTracker.
Both implementations call a method called revertToNitz() when the settings becomes true. Apparently NITZ is the equivalent of NTP in the carrier world.
Bottom line: You can set the time to the value provided by the carrier thanks to revertToNitz(). Unfortunately, I haven't found a mechanism to get the network time. If you really need to do this, I'm afraid, you'll have to copy these ServiceStateTrackers implementations, catch the intent raised by the framework (I suppose), and add a getter to mSavedTime.
For more informations, i suggest you to check this link here
Use the ScheduledExecutorService with scheduleAtFixedRate to send you "clock ticks". If the user initiates an event and the number of accumulated "clock ticks" since the last event doesn't match the time change on the system clock, you're being lied to.
You don't need to know the correct time. You need to know the correct interval. This can be done with any periodic source, even a local one. (Timekeeping is two jobs: a metronome and a labeler for the intervals of the metronome. You don't want the system's labels because they can be made to lie, but the metronome ticks on even if the labels are changed.)
I'd recommend a relatively slow tick rate (<= 1 tick per minute) and rather sloppy comparisons (within 2%, maybe) since the various clocks may not be all that accurate.
i am developing an android app where in i am displaying the date and time for the user on the main screen of my app. I am using the below code for it.
SimpleDateFormat sdf = new SimpleDateFormat("EE, dd MMM yyyy");
String strtDates = sdf.format(cal.getTime());
textviewfordate.setText(strtDates);
clk = (DigitalClock)findViewById(R.id.digitalClock);
However, when the time is changed, the date doesnot get updated without refreshing the screen. For example when the time is 11:59:00 PM and when the time changes to 12:00:00 AM, the date will not be updated. It gets updated only when the screen is refreshed. I want the date to be changed automatically without refreshing the screen.
Not getting how to do it! Please Help! Thanks!
If you set a text to textview in onCreate(), it will run only once when that method is called and won't get updated everytime the time changes.
For your need, you have to use handler or executor or any other thread which runs for every user defined time, and you have to set the current time text to textview inside them. So, that the text will be updated.
And also refer the following links, they will help you:
Android run thread in service every X seconds
How to execute a function every two seconds in Java on Android?
display data after every 10 seconds in Android