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);
}
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 can't stop my service.
I have two services. Service1 starts Service2 with startService(intent).
I stop service1 with stopSelf();
Then I'm doing something in Service2 and start Service1 again and stop Service2.
So they are always starting each other again.
The behavior of the services are different after the first start.
I write some log messages and I can see that the information are multiply times in log cat.
I also tried to stop the activities in the onStartCommand of the other activty with stopService. (In onStartCommand of Service1 I call stopService service2)
Here is a Part of my Code:
Service1:
public class Service1 extends Service{
private int startId;
#Override
public void onCreate(){
super.onCreate();
}
private void doSomething(){
...
...
Intent intent = new Intent(getApplicationContext(), Service2.class);
startService(intent);
this.stopSelf(startId);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.startId=startId;
Intent beaconIntent = new Intent(getApplicationContext(), Service2.class);
stopService(beaconIntent);
doSomething();
return START_STICKY;
}
}
Service2:
public class Service2 extends Service implements BeaconConsumer, RangeNotifier{
private BeaconManager mBeaconManager;
private int startId;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent service1 = new Intent(getApplicationContext(), Service1.class);
stopService(service1);
mBeaconManager = BeaconManager.getInstanceForApplication(this);
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
mBeaconManager.bind(this);
this.startId = startId;
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
mBeaconManager.unbind(this);
}
#Override
public void onBeaconServiceConnect() {
region = new Region("all-beacons-region", null, null, null);
try {
mBeaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
mBeaconManager.addRangeNotifier(this);
}
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if(!beacons.isEmpty()){
doSomething...
...
...
Intent intent = new Intent(getApplicationContext(), Service1.class);
startService(intent);
this.stopSelf();
}
}
}
What am I doing wrong?
Edit: I didn't mention, that I'm working with altbeacon library. I thought that doesn't make any impact.
But when I'm looking at the services which are running when I start the app and after I stop the Service2 there are always two altbeacon services running(BeaconIntentProcessor and BeaconService).
Maybe they calling my service mutlipy times.
I got what I did wrong.
The problem was not the service it self, it was the altbeacon library. More concrete the BeaconManager which was still searching in the background for a beacon, even when the service was destroyed.
In the logs it looked like the service is running multiple times.
The solution was to not only unbind, also to stop Ranging and remove the range notifier.
mBeaconManager.stopRangingBeaconsInRegion(region);
mBeaconManager.removeAllRangeNotifiers();
mBeaconManager.unbind(this);
I create Service and call it from MainActivity.class with put string extra to intent but when I call getStringExtra in onStartCommand, it returns NullPointerException
Here is my code:
MainService.class :
public class MainService extends Service {
private WindowManager wm;
private WindowManager.LayoutParams params;
#Override
public IBinder onBind(Intent i) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
super.onDestroy();
if (mView != null) {
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.removeView(mView);
}
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
final String quote = intent.getStringExtra("quote");
Log.d("datastring",quote);
return super.onStartCommand(intent, flags, startId);
}
}
In MainActivity.class I called :
Intent i=new Intent(MainActivity.this, MainService.class);
i.putExtra("quote", "dataquote");
stopService(i);
How I can get string from MainActivity in MainService?
If you want to provide more data to your Service you need to use something like:
Intent intent = new Intent(MainActivity.this, MainService.class);
intent.putExtra("quote", "dataquote");
startService(intent);
This will start the service and you should be able to get the data intent in the onStartCommand(). If the Service is already running, onStartCommand() will still be called with the new intent. This is because Service components are inherently singleton, so only one instance of a particular service run at a time.
In your case, you are providing the data via stopService(intent). Not only its not possible to get this intent, your Service will also stop after this statement so, you cannot really do much with the data even if you could have read it.
If you still need to stop your Service and pass data at the same time, you should check this post.
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)