I would like to call a method every 10 seconds while app is closed. I have read that I have to create a service but I don't understand how. It would send information to a webserver (only personal uses).
Thanks guys!
If you want to send information to your webserver only while the application is running, you can use a thread within unity.
If you want some code to keep running, irrespective of whether or not your app is running, you need service. This should get you started on how to write a service.
You can write a small plugin in android native code, and include it's aar/jar in your unity project. Then you may send a message from your unity app via JNI to start the service. Totally depends on how you want to implement it though.
I have found a way of doing it. You can call a method via OnApplicationQuit() and this will run when the app gets closed. I have used this to get changes from my firebase database and send a notification when a child is added. It works. In this case you would have to subscribe to a method and this will run even if the app is closed. Then unsubscribe to this method via the OnAwake() method.
Check this
< https://developer.android.com/training/run-background-service/create-service>
You have to create a service but probably its very difficult for you.
Related
I recently hooked an app, and I've found some methods that I can call them in it, like I hooked a before onCreate method in the main activity, then I can call some method as well. That's perfect.
But I want to call them directly via my app, I can do it through BroadCast, but the broadcast can't run more than 10 seconds, it's very bad run some big task, and sometimes I prefer a result can be returned.
So seems it's wonderful I can add my service in the remote app and start it, but the problem is it isn't in the AndroidManifest.xml.
By the way, how can I get the ClassLoader from the remote app, I am not sure it will be working if I got the ClassLoader.
Welcome to correct me!
Thank you all. Waiting for your opinions.
Feel free to contact me with email shiqwang#gmail.com or Wechat(id:imwangshiqi)
How can I add my service into remote app and start it which I injected
by Xposed?
Short answer: You cannot start a service without registering it in the 3rd party app manifest. While with Xposed you could likely modify this behavior, it is likely a bad idea, not only because you would be messing with Android internals but also because there are easier alternatives to perform "remote invocations".
Long answer: If i understood correctly you want to build your own application that is able to control other Android applications hooked via Xposed.
If such is the case, understand that both apps will be completely isolated (different VM), you cannot simply pass an object from the 3rd party app (i.e., hooked app) to your app and expect any invocations on this object to be performed on the 3rd party app. You should check Remote Method Invocation (RMI) to understand these concepts. This answers your question regarding the class loader.
Nonetheless, if all you want is to invoke one method of yours remotely on that app, then you can surely do that. As you mentioned you cannot launch a service in a 3rd party app if it is not specified in its manifest (well at least not without major hacking). But if you launch the service in your app, you can force the 3rd party app to bind to it, and implement message handlers that invoke your methods on the 3rd party app (via IPC).
In fact I have done so before using the Android Messenger for performing this communication (i have written a detailed post on it). You will need to hook the app Activities (and other starting points as well if you want, e.g., broadcast receivers) to bind to the remote service. Then the service can send messages to to trigger actions on the hooked app.
I'm making an application that detects incoming text messages.
When a message is received, the application must perform a certain action, depending on the sender and the content.
The problem is that the application must work at all times.
Detecting messages works through a BroadcastReceiver class. Via a Toast message I can see that the application (open or closed) receives the message.
The problem then is that I must perform certain actions, which are stored in a local database (DB40). But I can't access the database when the application is closed.
So, how can I perform database access and run other code (like making the phone vibrate, or play a ringtone) when the application is closed?
Thanks in advance
You can access the DataBase... because you are getting the context from the receiver, from the context create the instance of the Database and go further according to your requirement..
Check this...
You need to run a service to perform all the task even app is closed.
Just go through with few sample and study:
http://developer.android.com/guide/components/services.html
Do you know about Services?
You can use service as back-end to perform action on incoming messages or can use database.
Database need only 'Context' object for read/write and service provide 'Context' object by 'getApplicationContext()' method.
If you have any ambiguity about answer, just give me your little code, i will give you back with solution.
I have been working on an Android app using Phonegap and now would like to make it so when the app is closed it can still execute the java/js code in the app. So I understand I need to create a service. If I create a service plugin on phonegap can I still execute the javascript code or only the java?
Has anyone does something like this? I found this discussion but did not seem to work: http://groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6
So that is all I have right now.
Before I turn to developing it native if I can't figure it out thought I would ask if anyone has done this before. I can't seem to find any of the phonegap plugins that do something similar.
EDIT: I have got an app that executes Java code as a service. However when it calls sendjavascript it does not work. So is there a way to have the javascript code running in the background as well when an app is closed with phonegap?
Thanks
No, it is not possible to run Javascript code in the background (at least in my opinion) as a service. Phonegap on Android uses an special activity called Droidgap, which hosts a WebView. This browser control executes the JavaScript. This means that JS execution can only handled inside this activity, regardless if it is visible or not.
The code you linked from Google Groups tries to bind a service developed in Java to the DroidGap activity, so the service is NOT written in JS.
You can have some background activity within your JS code inside your child activity derived from the DroidGap activity. For example have a background thread in your activity, have a JS callback function and let the thread call this callback functionality.
If you really need a service you have to go native.
Update:
JS code can only be executed with the Droidgap activity. An activity can have 3 states (based on the Lifecycle of activites):
visible
invisible but still loaded
not loaded
I provided a sample in which I implemented a Phonegap plugin. The plugin allows the activity to register itself to SMS_RECEIVED. When the activies goes out of scope (event onbeforeunload), it deregisters, so only issue 1 is handled.
When you want all 3 issues handled, you have to forward the incoming SMS intent to the activity. When it is not loaded the system will automatically load and activate the activity. But this is not a background service anymore, your app would become visible whenever a SMS is received.
If you don't want this (if you really want a background service), you have to provide a native implementation.
There is this article on how to create a service on Android with Phonegap which gives some good information on your problem.
It's using a great plugin in order to build a background service with phonegap easily. But you can't use JS though
I didn't find a way to make JS to run in the Background. BUT you can pass parameters from Java to JS and vice versa with the plugin...which is pretty useful.
You would still need to rewrite your JS code in Java though.
Unless you do have a specific reason to only want JS to be run? (But there shouldn't be...)
Hope that could be useful to some people visiting this page.
YES, and it is very simple... just install the plugin backgroundJS:
https://build.phonegap.com/plugins/430
It allows you to run javascript on the background and combined with the local notification plugin, you can even send notifications to the user at any time, just keep in mind that doing this will cause the battery to run out faster, also consider that this might create a problem with the iOS policy. good luck!!!
You can try to add plugin cordova-plugin-background-mode
But as author says:
Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed.
Use the plugin by your own risk!
I have been working on an Android app using Phonegap and now would like to make it so when the app is closed it can still execute the java/js code in the app. So I understand I need to create a service. If I create a service plugin on phonegap can I still execute the javascript code or only the java?
Has anyone does something like this? I found this discussion but did not seem to work: http://groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6
So that is all I have right now.
Before I turn to developing it native if I can't figure it out thought I would ask if anyone has done this before. I can't seem to find any of the phonegap plugins that do something similar.
EDIT: I have got an app that executes Java code as a service. However when it calls sendjavascript it does not work. So is there a way to have the javascript code running in the background as well when an app is closed with phonegap?
Thanks
No, it is not possible to run Javascript code in the background (at least in my opinion) as a service. Phonegap on Android uses an special activity called Droidgap, which hosts a WebView. This browser control executes the JavaScript. This means that JS execution can only handled inside this activity, regardless if it is visible or not.
The code you linked from Google Groups tries to bind a service developed in Java to the DroidGap activity, so the service is NOT written in JS.
You can have some background activity within your JS code inside your child activity derived from the DroidGap activity. For example have a background thread in your activity, have a JS callback function and let the thread call this callback functionality.
If you really need a service you have to go native.
Update:
JS code can only be executed with the Droidgap activity. An activity can have 3 states (based on the Lifecycle of activites):
visible
invisible but still loaded
not loaded
I provided a sample in which I implemented a Phonegap plugin. The plugin allows the activity to register itself to SMS_RECEIVED. When the activies goes out of scope (event onbeforeunload), it deregisters, so only issue 1 is handled.
When you want all 3 issues handled, you have to forward the incoming SMS intent to the activity. When it is not loaded the system will automatically load and activate the activity. But this is not a background service anymore, your app would become visible whenever a SMS is received.
If you don't want this (if you really want a background service), you have to provide a native implementation.
There is this article on how to create a service on Android with Phonegap which gives some good information on your problem.
It's using a great plugin in order to build a background service with phonegap easily. But you can't use JS though
I didn't find a way to make JS to run in the Background. BUT you can pass parameters from Java to JS and vice versa with the plugin...which is pretty useful.
You would still need to rewrite your JS code in Java though.
Unless you do have a specific reason to only want JS to be run? (But there shouldn't be...)
Hope that could be useful to some people visiting this page.
YES, and it is very simple... just install the plugin backgroundJS:
https://build.phonegap.com/plugins/430
It allows you to run javascript on the background and combined with the local notification plugin, you can even send notifications to the user at any time, just keep in mind that doing this will cause the battery to run out faster, also consider that this might create a problem with the iOS policy. good luck!!!
You can try to add plugin cordova-plugin-background-mode
But as author says:
Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed.
Use the plugin by your own risk!
I have just began researching services in Android. What I'm trying to do is remotely call a method within my application. How I believe it will work is the service will listen for requests from the server in the background, then depending on the request, it will call a certain method within the application. Is this possible to do with a service? or are there better ways to do this?
I'm not asking for implementation of this, just confirmation that it could work and is the best way.
Thanks
Local service can call any functions you want inside your application.
This might not be the best application design, but this certainly will work. Local service is equivalent to any other locally created object. The difference is that service is created by Android System, not by your code.