I implemented a test service:
public class TestService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
}
Here is code that started the service:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, TestService.class));
}
}
I declared service in manifest
<service android:name=".TestService"/>
After starting the service, I have seen the service process in the Android task manager. I tried kill the process, and the Android restarted it.
I set the device aside for a while. After an hour the service was killed. I didn't see it in the Android task manager.
What am I doing wrong?
My device is Samsung A3 (2016).
Related
I created an app that reads books to the user.
The user needs to be able to continue reading
from the same position as he was in a previous app session.
How do i do something like that, if a process kill can occur anytime?
you can create here a service class in your app like this and create an api with your backend too :-
Service Class OnClearFromRecentService.class:-
public class OnClearFromRecentService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onTaskRemoved(Intent rootIntent) {
callApi();
stopSelf();
}
public void callApi(){
//set your api here
}
}
and set this service class in your manifeast :-
<service
android:name=".halper.OnClearFromRecentService"
android:stopWithTask="false" />
and start this service when your video is play :-
OnClearFromRecentService onClearFromRecentService = new OnClearFromRecentService();
or stop your service where your you need to stop service like this:-
onClearFromRecentService.onTaskRemoved(new Intent(LandingScreen.this, OnClearFromRecentService.class));
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 am trying to build an app which can be also opened by pressing device power button twice. I have followed this question's answer to build my app. But the background service is not working when the application is closed. Though it seems to work fine when the application is running. Here is the code for service LockService.java
public class LockService 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) {
final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
final BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
return super.onStartCommand(intent, flags, startId);
}
public class LocalBinder extends Binder {
LockService getService() {
return LockService.this;
}
}}
BroadcastReceiver class for application ScreenReceiver.java
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(getApplicationContext(), LockService.class));
}}
Finally AndroidManifest.xml file is here.
What should i do to work the app properly?
When your application is closed, LockService gets destroyed. That is why it doesn't work. What you can do is in your Main Activity's onDestroy method, stop the service. Further, in onDestroy method of LockService, send an intent to a broadcast receiver which will start your service again. In this way, whenever your app is closed, broadcast receiver will start your service again and the code inside the service will then be executed.
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'm using the below code to start a service. this service should start Async task that checks mysql and provide notifications. the service isn't starting and I can't find why? I receive no Log from the service.
(when I call the Async task from main activity directly without service it works fine).
Main Activity code:
Intent startIntent = new Intent(MainActivity.this, ForegroundService.class);
MainActivity.this.startService(startIntent);
Service Class Code:
public class ForegroundService extends Service {
private static final String LOG_TAG = "ForegroundService";
#Override
public void onCreate() {
Log.i(LOG_TAG, "On Create");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(LOG_TAG, "On Start");
new Thread(new Runnable() {
#Override
public void run() {
String newoperator = "Go";
TimeSync ReportCheck= new TimeSync(getApplicationContext());
ReportCheck.execute(newoperator);
}
}).start();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Log.i(LOG_TAG, "In onDestroy");
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
I also found some solutions adding the below command in main activity but the command isn't applicable in my android studio.
startIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
What version of android studio do you use? Have you tried to updated to the latest version?
Download Android Studio 2.1