android-service

Binding a Service to an android.app.Activity vs Binding it to an android.app.Application

与世无争的帅哥 提交于 2019-11-30 13:15:17
问题 Is there any fundamental difference in binding a service to an android.app.Activity vs binding it to an android.app.Application . I want to bind the service to an Application because I want to keep some global state/data in the Application instead of duplicating it in all my activities. Thanks. 回答1: No. There is no fundamental difference. That said, subclassing android.app.Application is a very good place to store global/state data. There is only one instance and everything that derives from

How to address android lint complaint about exported Firebase Messaging service implementations?

一世执手 提交于 2019-11-30 12:39:39
问题 Following the Google developer instructions on implementing Firebase in my app, I notice that android lint complains. The idea is that we have to implement two services which inherit from Firebase services: public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { ... } public class MyFirebaseMessagingService extends FirebaseMessagingService { ... } and then register those services in the manifest. But, it's not quite perfect. In particular, these two recommended

Single Notification for multiple Foreground Services using startForeground() call

痴心易碎 提交于 2019-11-30 12:30:06
I have an app that has two services. One is for showing UI for floating (overlay) on other apps using WindowManager . The other is for Location Tracking using GooglePlayAPI . My app always runs these services. I want these services not to be killed by the OS. So I call Service.startForeground() . However there are two notifications in the notification drawer. Is there a way to use a single notification for both services? Yes, it is possible. If we take a look at Service.startForeground() signature, it accept both notification id & the notification itself ( see documentation ). Hence, if we

Is there a straightforward way to stop a service in response to a user clicking a notification?

雨燕双飞 提交于 2019-11-30 12:23:30
I'd like the following behavior: The user clicks a notification and Android stops my Service. The problem is that stopping a Service requires a call to stopService and I cannot easily create a PendingIntent that does that. So the only way I found to do this is to have my Service receive a special Intent extra that causes the Service to call stopSelf and stop. Is there a simpler way to directly cancel a Service from a notification click? You could create a simple BroadcastReceiver that does the stopService() call, and use a getBroadcast() PendingIntent to trigger it. That BroadcastReceiver

Does updating a Notification remove a Service's foreground status?

霸气de小男生 提交于 2019-11-30 12:20:18
In my app, I place my Service in the foreground to prevent it from being killed by using: startForeground(NOTIFY_ID, notification); This also displays the notification to the user (which is great). The problem is that later I need to update the notification. So I use the code: notification.setLatestEventInfo(getApplicationContext(), someString, someOtherString, contentIntent); mNotificationManager.notify(NOTIFY_ID, notification); The question then is: will doing this knock the Service out of it's special foreground status? In this answer , CommonsWare indicates that this behavior is possible,

Binding to a service from another app

徘徊边缘 提交于 2019-11-30 11:35:30
I wrote two apps (target Gingerbread). Let say app1 and app2. App1 has two services started with "BOOT_COMPLETED" and they are started with the return value START_STICKY. They run in separate threads. To make a long story short. One of the service is watching for incoming data on a serial port (a kind of proxy for app communicating with interfaces on the other end of the serial port). The other has a listener watching some system status and waiting for some "instructions" from other apps. I know they are running well because they are listed in the running services and I added some code that

Using Dagger 2 to inject into service

≡放荡痞女 提交于 2019-11-30 11:22:47
问题 I have an app which is basically a service that runs all the time and alarms the user when something happens. When the service creates the alarm, it needs to give it his context so that the alarm can do callbacks to the service when something happens. For example: public MyService extends Service{ private SomeAlarm alarm; @Override public void onCreate() { super.onCreate(); alarm = new SomeAlarm(MyService.this); } } How can I inject the SomeAlarm class into the service, and give the SomeAlarm

When does ServiceConnection.onServiceDisconnected() get called?

牧云@^-^@ 提交于 2019-11-30 10:53:56
I'm messing with Android services, and I have found that ServiceConnection.onServiceConnected() gets called fairly predictably when I bind to a service. However, my onServiceDisconnected() method seems to never be called, even after the VM dies. I have logged debug messages on the service and show that all threads have been shutdown, etc. I know services are implemented as processes; are there threads I don't know about that are preventing the process from exiting? reflog It happens upon remote service crash. So, if a service running in a different process than your client fails on some

Transfer InputStream to another Service (across process boundaries) with ParcelFileDescriptor.createPipe() failes with “EBADF (Bad file number)”

痞子三分冷 提交于 2019-11-30 10:41:30
问题 I want to "send" an InputStream from one Android Service to another service running within a different process by using ParcelFileDescriptor.createPipe(), a stream-to-stream copy thread and a ParcelFileDescriptor, representing the read side of the pipe, which is given to the other service with means of Binder IPC. Sending Code (Process A) I want to send a given InputStream to the receiving service: public sendInputStream() { InputStream is = ...; // that's the stream for process/service B

Android: take a 'screenshot' of a web page from a background service?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 10:24:09
I have a URL for a web page, and I want to take a 'screenshot' of this web page in the background, eg. in a Service, and without showing a UI to the user. I have tried to create a WebView in my Service, and then use the capturePicture() method to get the screenshot when the page has finished loading, but the created Picture (and the Bitmap I create from it ) is always empty. (This works perfectly in a normal Activity, but not in my background Service). Any way to get this to work, or an alternative way to get a 'screenshot' of a webpage without having a UI ? Note: This answer is old - the