I would like to find out how our Android app can listen when some application is launch (and exit) in an Android device by a user(How to listen to it through Broadcast Receivers or any other way)?
I have searched a lot but not find any satisfactory solution. Please help in this regard. Thanks a lot for your time.
I don't think there's any default way to do that by broadcasts.
You could try using ActivityManager.getRunningAppProcesses
Correction: Listen for the broadcasts android.intent.action.MAIN and android.intent.category.LAUNCHER. Create a broadcast receiver and in the onReceive method check to see the name of the application that is started.
Related
I have been scratching my head for the past few days. I am trying to intercept incoming calls in Android. For that I have used a BroadcastReceiver and registered it in my AndroidManifest.xml as follows
<receiver android:name="org.myapp.IncomingCallReceiver"
android:permission="android.permission.READ_PHONE_STATE" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
I am able to get the incoming call number in the onReceive method of my IncomingCallReceiver. But the broadcast receiver does not work if I close my app. I have seen some libraries that does work in the background. For example telephony is a flutter plugin that receives incoming SMSs using broadcast receiver. When my app using the telephony plugin is killed, it continues to receives messages in its IncomingSmsReceiver. I am not quite sure how this is happening. From adb logcat I have seen that a new proecess is created with the broadcast receiver when a new Sms is received (and the app is closed).
D Zygote : Forked child process 6287
I ActivityManager: Start proc 6287:org.sudipta.sms_handler_example/u0a525 for broadcast {org.sudipta.sms_handler_example/com.shounakmulay.telephony.sms.IncomingSmsReceiver}
One thing to notice is that the plugin does not use any Service. There is a closed issue in github regarding this. The author of the plugin replied that
Yes listening in the background will work even if you don't open the app. The reason this works is we are registering a static broadcast receiver in the app's manifest file. That means the android system can initialize that broadcast receiver independent of the app.
My question is that if this really is the case. Will Android system keep the broadcast receiver alive (by forking a new process)? My understanding is that by default this is not going to work, al least that is not happening with my code, the receiver does not get hit when a call is received and the app is not running.
The author of the plugin further replied that
In most cases the device manufacturers and custom android OS environments block most of the app from performing operations in the background. That means your app won't receive any broadcasts and if it tries to start services in the background those will be killed as well.
But I have created a single sample app and using Telephony plugin and my own code (a simple receiver with the above manifest entry). While the plugin works in the background, my code doesn't.
Some help would be nice.
Thanks
I know we can START_STICKY service or use receiver event for start service in android
I'm making chat app for android with java that this service must be active in
any mode in android
There are many method for keep alive this service
START_STICKY
Use phone event
Start Foreground
But I dont know what is best way for keep alive service and android 8 and other version
You know that for chat app,service must be active always for receive new messages
Thanks
I found best solution
We dont have any problem about alive service in android 7 and lower version
Main problem is in android version 8
We can associate service to BroadcastReceiver in manifest and have alive service always
BroadcastReceiver can be TICK or CONNECTIVIYCHANGED or SCREEN event that when this events
occur so us service started without any problem
Thanks
I realized an android application which always has to remain activated in the background. To make that, it is enough to use a Service which remains active after the user is left the application.
Nevertheless, if the user rebooting, the application as well as the Service will not be any more active.
How to solve this problem?
I think of having an idea with BroadcastReceiver but I am not on whether it is the best solution.
Thanks in Advance.
So you want the service to restart if the phone is rebooted ?
Create a BroadcastReceiver that listens for BOOT_COMPLETE (you need a permission for this). Have it start your service.
Android may stop your service due to lack of memory.If you have onStartCommand return START_STICKY and have your onCreate of the service call startService on itself, you'll restart when memory is available.
You can set up an alarm to wake your service if its not running, and set up a broadcast receiver on boot which will start the service when the device boot.
My app will be running in the background and uploads data to the server from time to time. Everything works fine, except that I want my app NOT to upload data to the server on its own by turning on network services. Instead, I want it to only start the upload process when another app or service has started a connection. In some words, I wanna be parasitic to the other app.
I know for a fact that this can be done in Android, however I couldn't find anything in the documentation. Any idea?
I would try registering for the broadcast intent WifiManager.NETWORK_STATE_CHANGED_ACTION, via a BroadcastReceiver. See http://developer.android.com/reference/android/net/wifi/WifiManager.html#NETWORK_STATE_CHANGED_ACTION.
Hope that helps!
I have created (i'm a noob!!!!) a good app that use a service to do some periodic operations.
When the phone is connected to pc via USB (using eclipse with device connected) during test, the service run even in stadby, in the best way possible.
I can see it in logcat and by the result operation...
But when the phone is disconnected the service is running only if the phone is wake up!
How i can solve it without wakelock ?
I mean obtain something like RADIO FM, understand? ;-)
Thanx to all friends!
When the phone is connected to pc via USB (using eclipse with device connected) during test, the service run even in stadby, in the best way possible.
That is probably because the device is not actually in "standby".
How i can solve it without wakelock ?
You can't.
I have created (i'm a noob!!!!) a good app that use a service to do some periodic operations.
Use AlarmManager and something like my WakefulIntentService for this.
I mean obtain something like RADIO FM, understand? ;-)
Android does not have "RADIO FM".