Posts

Showing posts with the label Service

Android JobScheduler Won't Stop With JobFinished(params, False)

Answer : You have specified that a job is run periodically (every 60 seconds), so every 60~ seconds a new job is created and executed. jobFinished() is specific to a job, and simply indicates that it is done executing. You haven't cancelled anything. Your (currently) accepted answer works for cancelling your scheduled job, but if all you want is a job that executes within 60 seconds and then stops, you should ommit setPeriodic() and use setOverrideDeadline(60000) instead. The job will run within 60 seconds and no more will be scheduled after it. Well I figured it out if anyone else has this problem. jobFinished() won't stop the periodic time you set from continuing. It just tells the job that you are finished, to release the wakelock, so Android doesn't have to kill the job off forcefully. What I had to do was recreate the jobScheduler in my service and call cancelAll(). You could also apparently call cancel(job_id). jobScheduler = (JobScheduler)this.ge...

Android: StopService Doesn't Call OnDestroy

Answer : First, it's onDestroy, not OnDestroy . Second, you must use the @Override annotation for compile-time checking, so your Service code should look somewhat like this: @Override public void onDestroy(){ Log.v("SERVICE","Service killed"); player.stop(); super.onDestroy(); } First, you need to clarify how many types of services in Android. AFAIK, there are: Foreground service. Background service. Bound service. Intent service. These services stop in different ways. Foreground: only stop when you intentionally stop it by calling stopService() in activity or fragment where you start that service or call stopSelf() in its own service. And Please note only these methods trigger service's onDestroy() . Background: stopService() and stopSelf() do in the same way as foreground. Moreover, you should know this service is in the same thread with activity or fragment calling it, so if you destroy activity or fragment, this ser...

Bind Service To Activity In Android

Answer : "If you start an android Service with startService(..) that Service will remain running until you explicitly invoke stopService(..) . There are two reasons that a service can be run by the system. If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed) and then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand() ), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called; however, services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed. Clients can also use Context.bindServ...

Android - Implementing StartForeground For A Service?

Answer : I'd start by completely filling in the Notification . Here is a sample project demonstrating the use of startForeground() . From your main activity, start the service with the following code: Intent i = new Intent(context, MyService.class); context.startService(i); Then in your service for onCreate() you would build your notification and set it as foreground like so: Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.app_icon) .setContentTitle("My Awesome App") .setContentText("Doing some work...") .setContentIntent(pendingIntent).build(); startForeground(1337, notification); Solution for Oreo 8.1 I've encountered some problems such as RemoteServiceException becau...