How to detect user activity/inactivity in Android service/background application? - android

How to detect user activity/inactivity in android service/background application - from same or other applications?
I want to know that user stop touching screen and not busy with another activity to show notifiation popup. Not sure is there no other method than use AccessibilityService.

One of the ways is to know that from reading the Android logs

Use a boot receiver to setup the AlarmManager (and of course also check to start the polling from your main Activity too, for the case when your app is installed and the system is not booted) and have the AlarmManager send an Intent for another receiver.
I think they answered it in this question to do what you are looking for:
android: running a background task using AlarmManager

Define a interface in activity and service.
in oncreate of activity set reference to activity in service method.
Now you have activity reference for entire service lifetime.
another way to do this is, application class, same application class is shared by activity and service, so you can both of these from each other using application class.
If you want see from applications then use broadcastreceivers that is the only wau

Related

How to Start android service without activity

I created an application that contain a Service that start from button click of Activity.
But I want to create a new application that doing the same as the last one - but don't have any GUI interface => that mean without any Activity.
I want to make the application to start on device boot and never stop (until the device is shutdown) .
How to do it ?
Is there any way ?
How to do it ?
By using Context.startService() or Context.bindService() methods.
where Context might be Activity, Application, argument of the onReceive() method of BroadcastReceiver etc.

How to make sure Service gets called from a specific activity

In android how to make sure that the service that I have in the application will get called only through an activity that is within the app.
(In other words I want to limit the service to get played by only a certain activity & not even by other activities within that app)
I tried studying Intent-filters but got a bit confused.
Can someone please suggest, if possible with an example?
Thank You
Use Context.startService(Intent service) to start the service from your activity.
Despite the name it doesnt only start the service. If the service is running already, it just calls it.
From the service perspective, the service will then call its onStartCommand(...) method.
Only an activity within the same application can call/start a service this way.
If you set exported="false" in your <service .. /> element of AndroidManifest.xml, the service cannot be called by activities outside your own app.
I know of no way to limit access to any particular activity within the app, but this seems a less pressing concern. Supposedly you can trust your own code?

Will the Android OS destroy an Application class or AlarmManager class while my app is in background?

I have an AlarmManager class, a simple class, an Application class and some other class.
The simple class is called by AlarmManager and then the simple class try to achieve the goal with the help of Application class. It shouldn't matter whether or not my app is in the background; I need to ensure that these three classes are alive.
Now, my question is: Will the Android OS destroy these three classes (AlarmManager class, simple class, Application class), if my app is in background? If the answer is yes, then should I use Service? If I use Service, how should I implement it with these three classes?
edited:
i am trying to post status using facebook sdk in a particular time. So, the Alarm Manager class will call the simple class in a particular time. then the simple class will get some data (like session, message) from the Application class and then the simple class will post the message to user's wall.
the session , message etc are stored in Application class from other activity class.
I might want to show some code for clarity. You probably don't have an AlarmManager but registered some of your components to be called by creating a PendingIntent and passing that to the system's AlarmManager. If that assumption is correct, if your app is not running (it doesn't have a live process) at the time the alarm is triggered, Android will create one.
And in the process load and instantiate all the classes that are used from your component. If your app is in the background (process is still there, 'cached'), Android will reuse that process and call the necessary components.
Android will destroy all (let me name it "apps" here) after some time of inactivity. That includes Activity-objects and Services. The only thing that survives is a Service that is defined as foreground service (will require a notification image in newer versions of Android, so there is no way to hide it completely).
An Activity when in background (by user click Home or system call another activity), it will come onPause(), and it's not continue run, it's mean if your activity is uploading an image -> pause uploading. If system lack of resource like RAM, the system will destroy your activity automatically. So if you want to make an action with long time like download, upload,... you should use the Service.
Or if you want to download by Activity, you can save the download status or somethings like that in onPause() and resumse download in onResume(). But the process of download is not run until you resumse your activity.

Start activity/send intent from object instance within service

I have the following situation: A service is running in the background of my application and regularly receives UDP packets. It uses an instance of my HandleMessageAgent class which analyses every message and shall start a new activity if necessary.
I would like to perform the following task: No matter which activity is in the front (as long as the service is running) I would like to inform the user about an incoming message under certain circumstances. I also need to update the information regularly as long as it is valid. Afterwards it should be closed automatically.
At first I thought about using a Dialog, but I think I cannot use it when the activity is not visible. Therefore I decided to use an activity, as it can be started from a service all the time.
I want to start the activity within the HandleMessageAgent object (in a method). My problem is, that I do not know how I can define an Intent to start an activity within an object, as the Context is not clear to me.
Is there a more elegant way to perform this task? Or can anybody help me with starting an activity from an object method within a service? Thank you!
There are two situations to consider:
When your service need to notify user your activities are not active, because some other app is active. In this case you should notify user via a system-preferred way: Android Notifications. You should not forcefully show dialogs or activities if user is using some other app. That's what notifications are for.
If one of your activities is active (no matter which) then your service should send a broadcast and interested activities should listen for it and act upon it. That way your service will not depend upon specific activity and will not need to keep track which activities are active at the moment the notification must be shown.
You can make your object Parcelable and add it to the Intent that will start the Activity.
Or you can put it in a subclass of Application because that instance is shared between your activities and your services (as long as they're in the same process)
You might need the "START_NEW_TASK" flag on the Intent

Autostarting Android service upon Application startup

Is there a possibility to autostart Service upon starting Application ? The problem is that I am developing separate UI component that depends on service. Ideally this service should be started as soon as hosted application starts. Could this be done via manifest only or could it be done at all ? I know I can start service from my UI component's code, but I want to start service immediatelly after starting main application even in case if my UI component hasn`t been created yet.
Thanks in advance.
Create a MyApp class which extends Application, and make sure it's declared in your manifest. MyApp's onCreate() is then a good place to start the service if you need to.
See the documentation for the Application class.
You want to use the PERMISSION that notifies you of boot completion. This will allow you to know that the device has started and take action, such as start your service.
RECEIVE_BOOT_COMPLETED Allows an application to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting.

Categories

Resources