This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android:how does application Protector app works?
How can i be notified when new app begin launching? clearly to say, i click on app icon , and app begin launching, i want to set my service to observer of that event (if there is any). Is there any event or some way to know that before app launched ?
If I were you, I would get the ActivityManager with a call to
getSystemService(Context.ACTIVITY_SERVICE)
and then setup my program with the following code:
private final static Handler updateHandler = new Handler();
private static long WAKEUP_INTERVAL = 10000; // 10secs?
private Runnable periodicUpdate = new Runnable() {
#Override
public void run() {
checkRunningProcesses();
updateHandler.postDelayed(this, WAKEUP_INTERVAL);
}
};
to do periodical checks of the following two things:
check getRecentTasks() to find out what user has launched recently (requres GET_TASKS permission)
get from getRunningAppProcesses() and keep somewhere list of running processes just in case periodical check interval has been chosen too long and some processes were added/removed but did not show in getRecentTasks()
This way I could not get an instant notification about program launch, but could find about that soon enough (seconds later, maybe) to do something about it.
One more thing, it might be a wise idea to stop checks when screen goes dark, to save the battery.
android listen for app launch
You can't really do this with intents or anything like that. Check out this guy's brute hack. Not a great solution but the closest you'll get.
Related
Hello I'm creating tracking app and I want to quickly get the location. But this app should be friendly for people. It should works like that:
I turn on GPS and i want to get location by it
If it takes more that 5 seconds I try get location by network provider
If it takes more that 5 seconds i try to get location by wifi
if it takes more I do something else.
I already have this functions! How to do that they work one after another (If one wokrs more than 5 sec we move to another one).
So I'm thinking that I must use some kind of timer, but here I got a problem if I put code like this
Timer timer2 = new Timer();
TimerTask testing = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();
}
});
}
}; timer2.schedule(testing, 1000); ***
*** here syntax errors
not inside onCreate I have syntax errors (and I want to do that after pushing the button). However how should looks this timer for 3 methods following next by next ?
depending on the OS that you are devlopping for, I would recommend you to make use of the OS specific methods available for retrieving location.
In case of Android for example there is a service available that makes is quite easy for you to retrieve the location in an user friendly manner:
Please check this: https://developer.android.com/training/location/index.html
This allows you to create location requests, depending on your situation you can define the request with a certain priority. For example: in case city precision is enough in your situation, you can define it and the framework might decide to only use the currently available WIFI to get the location (and not startup the GPS).
I want this app to function like this
1.it will send the user location along with a message at interval of 2 mins to preselected contacts.
2.Location should be as presice as possible.
3.it should run in backround.
4.it stops only when the user stops it.
I tried many ways but it would work in foreground I want this to waork even when the app is closed if the user has to stop it he would have to open the app and deactivate it.
The IntentService class provides a straightforward structure for running an operation on a single background thread
See https://developer.android.com/training/run-background-service/create-service.html
This question already has an answer here:
Timer in Eclipse for Android Game
(1 answer)
Closed 9 years ago.
I have a 3-level math game. Our professor wants me to put a timer that starts at first level and stops only if the user already finished the 3rd level. I have this 3 activity class since I have 3 levels. I made it like this so that I will not be confused.
My problem is how can I make a timer continuously within each activity? I tried the examples found on the internet but I can able to run it on the first level.
Please help , because I don't have any idea how can I make this possible.
Thankyou :)
You could store the time when the game starts and then just calculate the current gameplay time according to that.
First you need to create the static variable to store the time in, we need it to be static, so it can be shared between classes easily.
public static long time_start = System.currentTimeMillis();
Then if you want to get the current passed time since the game started, you would do the following.
double time = (System.currentTimeMillis() - time_start) / 1000d;
The time variable is the passed time since the game started, and it is in seconds.
Then if you further want to reset the gameplay time at any point you would just call.
time_start = System.currentTimeMillis();
When opening a new Activity, use intent.putExtra(currentTime) so that the next Activity knows where to continue the timer from.
I am making an android app. There is an activity in the app, which when triggered, makes a sparsearray and fills it with data. Now this process takes upto 1 minute on the emulator, which is very long. So I want to make that sparsearray once, right when the app is launched, and save the sparsearray in RAM for the lifetime of the app. And whenever the activity is triggered it should access the sparsearray in RAM instead of making a new one, thus saving time and processing power. Can this be done, if so how? Sorry if this question is dumb, I am new to android. Thanks!
*Edit: This is what the sparsearray making function looks like:
//function which accesses sparsearray making function
public String[] process(){
InputStream is = context.getAssets().open("feedtitlesandaddresses.txt");
InputStreamReader iz=new InputStreamReader(is);
BufferedReader br = new BufferedReader(iz);
String line = null;
while((line=br.readLine())!=null) {
readLine(line);
}}
//sparsearray making function
private void readLine(String line) {
//some string processing(omitted here)
int num1 = Integer.parseInt(firstNumber);
//int num2 = Integer.parseInt(secondNumber);
if(sparseArray.get(num1) == null) {
sparseArray.put(num1, new SparseArray<String>());
}
temporarySparseArray = sparseArray.get(num1);
for(int w=0;w<size;w++){
temporarySparseArray.put(w, array1[w]);
}
sparseArray.put(num1, temporarySparseArray);
temporarySparseArray = null;
}
You can write you object in the android internal or external file system, and you can read the object when you want to use it.
I feel this question deserves an answer, and I was sure someone would come up with one, but, hmm, not really so far; and if I were Tom Wong, it would currently look to me as if StackOverflow was a place where wannabe experts start fighting over questions rather than answering them. ;) So here we go.
Do something at startup, the result of which lasts as long as your App is in memory: The right place to do this is Application.onCreate(), for which you need to subclass Application. It is okay to store the result in the Application object as well in this case, where you can access it from any Activity by getting the Application context.
Do something that will take some time: In general, it's a good idea to use AsyncTask on Android, but it is specialized for doing things which relate to the User Interface but must not block it. Also, without some explicit tuning, its behaviour will vary amongst different Android versions. Currently, without further measures, if you did your computation via AsyncTask, other AsyncTasks would have to wait first. Also, what you want to do is not a classic task for using an AsyncTask, so use a one-time Thread which simply stores its result finally in a volatile reference.
The following code snippet should give you the idea.
final class MyApplication extends Application {
public volatile String[] expensiveToCompute;
#Override
public void onCreate() {
expensiveToCreate = null;
(new Thread() {
#Override
public void run() {
String[] result = computeMyData();
// it is important to assign the locally created data at once here
expensiveToCompute = result;
}
}).start();
}
}
In this way, expensiveToCompute will be null until the asynchronous computation has finished.
It is important to understand that this Thread will run until it has finished, and that it holds a reference to your MyApplication object. This means that even if the user leaves your app, your Thread will still continue if it has not finished until then.
This is in fact nice, because either Android decides to leave your Application object alive just because it can, in which case the user experience improves. Or Android kills the whole process with the Application object, your Thread and any held data, in which case it will start from scratch next time.
One more thing about, umm, suggestions to move expensive computations into AsyncTask etc: Android assigns a default background priority to such threads, and all background threads will (currently) have to live with 10% CPU time altogether no matter whether the Android system is idle otherwise. So many people who move intensive computations to background tasks notice that it will take ten times longer. Hence, you may want to read my suggestions regarding how to handle this here and/or here.
I just started using Wicket (and really am not too familiar with a lot of web development) and have a question with regards to a download link. I have a web app that simply allows users to upload particular files, processes some of the information in the files, and offers downloads of different formats of the processed information. However this is really supposed to be a lite version of some software I am working on, so I really don't want to do much processing. I am wondering if there is a way to set something like a timeout for the download link, so that if the user clicks on the link and the processing takes longer than 20 seconds or so, it will simply quit the processing and send them an error instead. Thanks!
I agree with Xavi that the processing (and possible termination of the processing) should be done with a thread.
However, especially if it takes more than just a few seconds, it is much better to not just wait with the open connection, but rather to check at regular intervals to see whether the thread is done.
I'd do something like this:
Start the thread doing the actual work
Show a Panel that says "Processing your download" or something like that.
Attach an AbstractAjaxTimerBehavior to the panel with a timer duration of, say, 10 seconds or so.
In the timer behavior's onTimer method, check the state of the processing:
If it's still working, do nothing.
If it's canceled because it took too long, show a message like "Canceled" to the user, e.g. by replacing the panel or setting a warning label to visible.
If it's done, show a message like "Your download is starting" and start the download. See this document for how to do an AJAX response and at the same time initiate a download
To be able to cancel processing if it takes more than a given amount of time, it would be appropriate to perform it in a separate thread. This matter is addressed in the following question: How to timeout a thread.
Now for the Wicket part of it: If I understood what you're trying to achieve, you could for instance roll your own Link that would perform the processing, and respond with the results in case it doesn't timeout. In case the processing takes too much time, you can simply throw an error (remember to have a FeedbackPanel so that it can be shown).
The processing, or generation of the file to download, could be implemented in a LoadableDetachableModel for efficiency. See this question for more details: How to use Wicket's DownloadLink with a file generated on the fly?
For instance:
IModel<File> processedFileModel = new LoadableDetachableModel<File>(){
protected File load(){
// Implement processing in a separate thread.
// If it times out it could return null, for instance
}
}
Link<File> downloadLink = new Link<File>("yourID", processedFileModel) {
#Override
public void onClick() {
File processedFile = getModelObject();
if (file != null) {
IResourceStream rs = new FileResourceStream(file);
getRequestCycle().setRequestTarget(new ResourceStreamRequestTarget(rs));
} else {
error("Processing took too long");
}
}
};