I am developing an application in which i have implement the service.But when i exit from my application, service stops (but onDestroy() is never called) and then starts again (OnCreate() called).
Actually,I dont want to stop service once it is created regardless the state of application.
As service onCreate() is getting called again,service looses the previous processed data.I have tried START_STICKY but no luck.
so, How can i keep my service running?
here is my service code,
public class ddservice extends Service{
public void onCreate() {
super.onCreate();
Log.e("dservice", "onCreate");
/*here i am processing some important data in
background thread*/
}
#Override
public void onDestroy() {
super.onDestroy();
Log.e("ddservice", "onDestroy");
}
#Override
public void onStart(Intent intent, int startid) {
super.onStart(intent, startid);
Log.e("ddservice", "onStart");
}
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.e("ddservice", "onStartCommand");
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}//service class ends here
And i am starting this service from mainActivity as,
startService(new Intent(this, ddservice.class));
You should start your service as foreground Service.startForeground(int, Notification)
Related
I am developing App-Locker app and i have background service which is running. But i want the exact solution to trigger one function inside that background service when any application is launch or open in mobile.
I am using the below service which runs continuously.
public class NotificationService extends Service {
public NotificationService() {
}
#Override
public IBinder onBind(Intent intent)
{
Log.e("Noftication serveric ", " Onbinddddd");
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
#Override
public void onDestroy()
{
super.onDestroy();
//Starting Broadcast on destroy method, where this service is called again to run
Intent restartService = new Intent("RestartService");
sendBroadcast(restartService);
}
public void Trigger_Me_If_Any_App_Launch()
{
//This function should be called if any app launch
}
}
So I have code that I want called when my application is closed. Not just when it is sent to the background or the surface is destroyed. How do I do this? Is there a method that I can override in a SurfaceView or Activity class?
New Edit - current BackgroundService class:
public class BackgroundService extends Service {
private String savedString;
public void onCreate() {
System.out.println("Service created");
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("start command: ");
savedString = intent.getStringExtra("myString);
return super.onStartCommand(intent, flags, startId);
}
public IBinder onBind(Intent intent) {
return null;
}
public void onTaskRemoved(Intent rootIntent) {
System.out.println("the saved string was: " + savedString);
super.onTaskRemoved(rootIntent);
}
public void onDestroy() {
System.out.println("destroyed service");
super.onDestroy();
}
}
Where I then have this in my other class:
Intent serviceIntent = new Intent(activity.getApplicationContext(), BackgroundService.class);
serviceIntent.putExtra("myString", "this is my saved string");
activity.startService(serviceIntent);
you need to add a background service
public class BackgroundServices extends Service
{
#Override
public void onCreate() {
Toast.makeText(this, "start", Toast.LENGTH_SHORT).show();
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
then in your activity. where you want to trigger this service
use
startService(new Intent(getBaseContext(), BackgroundServices.class));
in your case it will be call on onDestory function of that activity
Yes when the process is terminated
That is not possible in general. Nothing in your app is called when the process is terminated.
For example when you open the running apps screen, and swipe away the app to stop it from running
That is a task removal. It may result in your process being terminated, and there are many ways in which your process can be terminated that has nothing to do with task removal.
To detect task removal, override onTaskRemoved() in a Service.
I have an alarm broadcast receiver where I want to check if my app is completely closed, which means app is neither running in foreground nor background.
Can anyone tells me how can I check this ?
You can create a service and overwrite the onTaskRemoved() method
From the Documentation
the user has removed a task means swiping the app out from the task
list. Stopping the Service from the phone's settings does not trigger
Service.onTaskRemoved().
Code:
public class AppStopped extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Service", "Service Started");
return START_NOT_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d("Service", "Service Destroyed");
}
#Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("Service", "END");
Toast.makeText(getApplicationContext(), "App Stopped", Toast.LENGTH_SHORT).show();
//Code here
stopSelf();
}
}
In Manifest:
<service android:name="com.example.AppStopped"
android:stopWithTask="false" />
Start the service in your activity like:
startService(new Intent(getBaseContext(), AppStopped.class));
In your Application class:
public class MyApplication extends Application {
#Override
public void onTerminate() {
super.onTerminate();
// your app is closed
}
}
How Whatsapp service keep working in background in huawei phones ?
I removed whatsapp of protected apps but Whatsapp service not closed in screen
off time.
I'm writing critical app that need to run every time but my service killed in screen off.
I want to write service like Whatsapp or AirDroid service
anyone can explain about that ?
I mean how to write service that specially not close by screen off in HUAWEI phones
This is my service code
AppLifeService
public class AppLifeService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startForeground(5, AppLifeReciever.createNotification(this));
return START_STICKY;
}
#Override
public void onDestroy() {
//startService(new Intent(this, AppLifeService.class)); Updated : not need
super.onDestroy();
}
}
You need to create a Service to "reopen" BroadcastService automatically when it's closed.
For example:
BroadcastService
public class MyBroadcastService extends BroadcastReceiver
{
#Override
public void onReceive(final Context context, Intent intent)
{
//do something
}
}
Service to "reopen" automatically
public class MyService extends Service
{
#Override
public void onCreate()
{
// Handler will get associated with the current thread,
// which is the main thread.
super.onCreate();
ctx = this;
}
#Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i(TAG, "onStartCommand");
//Toast.makeText(this, "onStartCommand", Toast.LENGTH_LONG).show();
return START_STICKY;
}
//launch when its closed
#Override
public void onDestroy()
{
super.onDestroy();
sendBroadcast(new Intent("YouWillNeverKillMe"));
Toast.makeText(this, "YouWillNeverKillMe TOAST!!", Toast.LENGTH_LONG).show();
}
}
Declare on your AndroidManifest.XML
<receiver android:name=".BroadcastServicesBackground.MyBroadcastService">
<intent-filter>
<!--That name (YouWillNeverKillMe) you wrote on Myservice-->
<action android:name="YouWillNeverKillMe"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<!--To launch on device boot-->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".Services.MyService"/>
Service with START_STICKY in retrun onStartCommand() will start again automatically you dont need to start it again in onDestroy()
#Override
public void onDestroy() {
// startService(new Intent(this, AppLifeService.class));
super.onDestroy();
}
#Sohail Zahid Answer tells you a way to repeatedly start a service again and again when stopped. But in order to keep a service alive like playing a song in a background.
The best Approach I found is
startForeground(int, Notification)
where int value must be unique for every notification
You'll need to supply a Notification to the method which is displayed in the Notifications Bar in the Ongoing section. In this way the app will keep alive in background without any interuption.
I hava a simple service with data - ArrayList < MyObject > , when I close the app - service is restarted, called onCreate method and data is losing. This is my onStartCommand method:
public int onStartCommand(Intent intent, int flags, int startId) {
.....
return START_STICKY;
}
Service start in Application class
Intent serviceIntent = new Intent(this, ChatNotificationService.class);
startService(serviceIntent);
I not called stopService method.
I need after closing the application, the service continued to work and the data saved in it.
How I can do it?
In activity:
#Override
protected void onCreate() {
super.onCreate();
bindService(serviceIntent, sConn, 0);
}
#Override
public void onDestroy(){
super.onDestroy();
chatNotificationService.closeConnections();
unbindService(sConn);
}