how to make our app to start automatically? - java

I have an apk which runs only on background. I want it to start itself every X seconds.
I can do it from terminal with sh command:
#!/bin/bash
while true
do am start --user 0 -a android.intent.action.MAIN -n com.xxxxxxx/.MainActivity
sleep 20
done
But after reboot it stops running itself automatically. Besides, I don't want to use sh command everytime. So I decompiled the apk with apktool. What do I need to add to make our app start it self every x seconds?
p.s. I know that if I save the commands as sh file into etc/init.d/ it'll be persistent but I'm trying to learn how can we add it to source.

What do I need to add to make our app start it self every x seconds?
You need to schedule it with the AlarmManager. You can find more about that here.
But after reboot it stops running itself automatically.
You need to register a receiver for android.intent.action.BOOT_COMPLETED for you app to be invoked after boot. You can find an answer for that here. When your receiver is invoked, you can then again schedule your app for restarting.

Related

How to show windows background process in the foreground

My problem description:
I have a server with OS version Windows 2012R2, I have two process running on it.
I have a .bat file to start them.
Below is the detail of .bat file:
e:
cd E:\autostart
echo ========================= 1.RUN PCG =========================
start startWebworksServer_PCG
echo ========================= 2.RUN PHONE =========================
start startWebworksServer_PHONE
Now I create a .jar file and make the jar as a windows service. just like this :
This service will start these 2 processes in the background, I cannot see the windows of these 2 processes.
I want to find a way to have this service start these 2 processes in the foreground, so that I can see these 2 windows after logging in to the server. It is best to use java language.
PS:I tried to use JNA because I was first exposed to JNA and I failed. I tried to use Advapi32.INSTANCE.CreateProcessWithLogonW but it still started the process in the background and I still can't see the process window.
Services usually does not show any window, but as far as I remember, you should have a checkbox "allow this service to interact with the desktop" in the service properties. This of course would work only if there is a user session.

Run application automatically when system back from hibernate

I want to start my application automatically for all users, whenever system starts from shutdown or hibernate (I don't want user manually click on my exe icon to run application). For example, if same system is used by two users, it should work for both users.
I've tried following, but it is not working for all users, neither for start from hibernate:
I had added my exec shortcut in shell:startup
I m also using batch file code like this REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v JApp /t REG_SZ /d /h "%~MoodOMeter.exe"
now a problem which i m getting is that it only works for current user i.e if it registers on startup folder on userid 1 then it will not run for userid 2 and the last thing that not working is that if system start from hibernate then it will not run automatically can anyone help me with this issue
You can use Task Scheduler to trigger an action on resuming system.
But Task Scheduler has its limitation (in context of your question).
You can setup the task in two modes:
For specific account with "Run whether user is logged on or not" option. For that you need to know a password of some local account. And the task will run with privileges of that account, so not with privileges of the logged on account. That would be problem, if you need to write files or registry in user profile. Not to mention that there can be multiple logged on users at the same time (the task would be run for one of them only).
With "Run when user is logged on" option, you won't need the password and the task is run with correct privileges, but only for the account that created the task. So if you create the task in installer, it would be executed only for the account that run the installer. To solve this, you can make the application itself create the task, when it's executed (and the task does not exists yet).
If the above limitation is ok, then it's a solution.
Other way is to keep your application running in the background and monitor the system to trigger your desired action on resuming.

Android java back to activity

In my app I wrote mechanism which lets return to the activity whet the application is suddenly removed from the device memory. Sometimes it happens eg. when the phone rings and there is not enough memory for phone application and my app. After the phone app ends the system call quite new process for my app and opens this activity which was closed previously.
I hope the mechanism I wrote works fine but I don’t know how to test it. I work in android studio.
So the question is how to simulate the process of killing and removal the application from system, and calling back my app process and activity.
fire this by cmd
echo 'am broadcast -a com.android.vending.INSTALL_REFERRER -n "package name/path of reciver" --es "referrer" "utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name"; exit' | ./adb shell

How to check solr server restarted sucessfully or not?

I am writing an automate script, where I am restarting solr servers one by one. But some time they are not restarting.
I am using the below command to restart it:
stopsolr; //stoping the solar server
cd $SolrFolder; //going to index folder
unzip -o $solrindex; //extracting the index
sleep 240; //waiting some time
startsolr" >> $SolrLog;//starting server
Now I want to check if the server started or not. If not started, then I want to stop my script from execution and will return an error code. I am vary new to Linux environment/shell scripting. Which command I can use to check the status of solr server?
you can do something along the lines of:
curl 'http://127.0.0.1:8983/solr/core0/select/?q=*%3A*
parse the response, and verify numfound= your expected number, or numfound> X if you don't know the size for sure.
Solr has a ping handler which is handy to use when you want to check status of a Solr instance programmatically. SolrCloud uses this to keep handle on its cluster.
http://localhost:8983/solr/admin/ping
look for status "ok". This is exactly the purpose this handler.
More about it here : http://lucene.apache.org/solr/4_1_0/solr-core/org/apache/solr/handler/PingRequestHandler.html

How to stop a running *.JAR file with a batch script?

We are facing trouble restarting closing a running *.JAR file started with the "java -cp".
Killing java.exe worked fine.
But... isn't there any smoother way for the application? (It saves data on shut-down)
Also, how can one simulate any key input at the following message "Enter any key to continue..." with a batch file?
Thanks for all help!
The following set of batch scripts can be used to start/stop jars/any program
start-jar.bat
start "win_title" java -jar [jar file] [jar options]
this basically starts your jar(the program) in a window with title set to "win_title".
you could use another batch to kill the window started
stop-jar.bat
TASKKILL /FI "WINDOWTITLE eq win_title
Use the PAUSE [message] to wait for a key press:
PAUSE Hit any key to continue..
As for killing your app, there are JMX ways to do it - but I find an easy way to have a socket listening on a local port and then you can easily send a kill commnad to it and let your program handle the shutdown.
The excellent Java Service Wrapper will let you effortlessly install signal handlers for your Java app.
Have your app create a temp file on startup and periodically check if it still exists. Your batch script can just delete that file to terminate the app.
If you need to do some cleaning up before your process is shutdown, take a look at Shutdown Hooks.
from the Q&A in the link:
Okay, but won't I have to write a lot of code just to register a simple shutdown hook?
No. Simple shutdown hooks can often be written as anonymous inner classes, as in this example:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() { database.close(); }
});

Categories