I am creating an android service using this code:
var intent = Ti.Android.createServiceIntent({
url : 'service.js'
});
Ti.Android.startService(intent);
As you can see, this service is started only once, it is not called in an interval every X milliseconds. Now when the application is exited (not put to background) and started up again, I would like to retrieve the reference of the service and stop it if the user clicks the stop button. Is there any way of achieving this? Thank you
Stop a Service in app.js by
Identifying the stop button click event and call this to stop service
if(Ti.Android.isServiceRunning(intent){
Titanium.Android.stopService(intent);
}
Related
I currently started developing for Android and ran into a problem.
I made a basic application in wich i can set a timer between 5 and 60 seconds. After a button press an Intent starts a PendingIntent wich will register an alarm in the Android AlarmManager.
I can simply set an alarm for 30 seconds, close my application, remove it from the running application list and wait. Eventhough my application is totally shut down, after 30 seconds my custom made activity with a simple view pops-up and i can stop my alarm.
The problem: even though i removed my APPLICATION from the running app list after i did set an alarm, after it goes of and i pressed the stop alarm button so the activity is closed i see my ACTIVITY in the running app list..... i can click on it and my basic custom made acticity pops-up again.
In the stop button onClick() i call finish() in the onStop() i call super.onStop()....
How to show my custom alarm activity with my stop alarm button and after stopping or snoozing DO NOT show it in the running app list?
Thanks!
You can use android:excludeFromRecents="true" as an attribute for your activity <activity /> tag in your manifest file.
I'm so lost with all that workflow of notifications and services in Android. My sceneario is this:
I have an Android application that communicate to a MySQL database through a web-service using JSON-RPC. The data retrieved from the service will be displayed in the application.
The data will get updated over time, so the application needs to listen for changes of this and, if a change occur, show a notification and update the data displayed in the app.
To listen for changes I will need to run an "infinite"(until the app is destroyed or maybe until the app destroys it) thread that from time to time will call a method on th web-service which will return the changes since the last check.
UPDATE: Ok, I have been trying using Service and IntentService, but non of them fits my needs: a Service execute in the Main Thread, so If I perform an infinite loop there my app will freeze, IntentService has it's own worker thread but there is no comunication with the App, and I need it, or at least I need a way to know if the app is in foreground (in this case the notification will not popup but the data will be passed and updated) or in background (int this case the notification will pop up and on click it will direct the user to the app with the updated data)
#1 You can fire a broadcast message from your Service and define a Broadcast receiver in your Activity to receive this broadcast.
SEND BROADCAST-from Service
Intent i = new Intent("ALERT_CHANGE");
i.putExtra("DATA","News");
sendBroadcast(i);
RECEIVE BROADCAST-in Activity
registerReceiver(uiUpdated, new IntentFilter("ALERT_CHANGE"));
private BroadcastReceiver uiUpdated= new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
String DATA = i.getStringExtra("Data");
txt.settext(DATA);
}
};
Ok, after a lot of testing and thanks to the info given here I finally found a way to handle with the issue, so I will share it here:
On the IntentService I have a public static AtomicBoolean to control the end of the loop and be able to stop the service.
Then to determine if the Activity is in foreground or not I use the method suggested here https://stackoverflow.com/a/5504711/3107765
With the difference that I use the static modifier there, so I can check it from the service.
if the activity is in foreground I send a broadcast as it was suggested here by Eu. Dr. otherwise I use a notification that once clicked will let the user to the activity.
I was playing around with services and dialogs, and I got a doubt. Within a dialog, I am starting a service like this:
Intent lock = new Intent(getActivity(),AppLockService.class);
getActivity().stopService(lock);
getActivity().startService(lock);
Now the first time I call the dialog through
dialog_name.show(getFragmentManager(), "dropbox");
Upon pressing the OK button, the intent is launched. Now later, during the same app execution, the dialog is triggered again ( which is according to my code logic -- nothing wrong here). The code in the dialog then stops the previously triggered intent and starts the new intent.
My question is this:
lock is a local intent variable as per my definition. So how does it know that it has to stop that particular service I have triggered here the first time? Would someone please explain this to me?
You don't have to keep track of the service in a variable because Android does it for you.
The way that the OS treats a service is that it will not allow more than one instance of the service be to running at any time.
So at any moment there are 0 or 1 instances of your service. If there are 0, no problem, the OS will ignore the call to StopService. If there is 1 instance, it must be the instance you started previously - so it will be stop that one.
Application Cycle :
I have a mainActivity, which starts a service, bind with it, and finish() after X seconds, the bind should be broken but the service should keep running.
The service with a specific trigger will start the MainActivity again (startService will not be called , i check if the service already exists).
So on the first time mainActivity finishes, the warning doesn't appear, while on the second time and on it does.
I tried removing all relevant 'binding' calls so no bind will be made and the warning still appears, it leaves us with a single option : is it because i start the service and not stopping it ? if so , how should I do it without getting this message ?
I start the Service using the following code :
Intent intent = new Intent(getApplicationContext(), LockerService.class);
intent.addCategory("LockerServiceTag");
startService(intent);
The stop is from another activity and not relevant since its not the problem for sure.
I DONT want to stop service when exiting.
Any insights why this warning message happens ?
In my android application,I am getting data from internet and I refresh data in every minute.
So my question is:
When application is minimized, i want to inform the users that new data is available via notifications.
How can i do this?
use onUserLeaveHint() of Activity` because this method is called when an activity is about to go into the background as the result of user choice like by pressing Home key , menu key,search key or any incoming phone call.
So when this method is called then start your service for data notifaction because onUserLeaveHint() is called when your application is minimized.
Set your application in a way that the data receiving and sending is handled by a Service. From the service, on events of data send/receive, fire up notifications.
In your case, I think its best to use unbound services
Try this::
#Override
protected void onUserLeaveHint()
{
System.out.println("Home Button Pressed");
super.onUserLeaveHint();
}