In my fragment is a button, which when clicked will start a service, which polls sensors, and uses an executor service to store sensor data to the database. The onClick of the fragment looks like this:
public void onClick(View v) {
if (!recordingStarted){
try{
recordingStarted = true;
mainActivity.startService(new Intent(mainActivity, SensorService.class));
startButton.setText(getResources().getString(R.string.start_button_label_stop));
Snackbar.make(coordinatorLayout, "Recording...", Snackbar.LENGTH_SHORT).show();
} catch (SQLException e){
mainActivity.logger.e(getActivity(),TAG, "SQL error insertSubject()", e);
}
} else {
dialog = new ProgressDialog(mainActivity);
dialog.setTitle("Stop recording");
dialog.setMessage("Please wait...");
dialog.show();
mainActivity.stopService(new Intent(mainActivity, SensorService.class));
startButton.setEnabled(false);
Snackbar.make(coordinatorLayout, "Recording stopped.", Snackbar.LENGTH_SHORT).show();
}
}
SensorService class is set up to store sensor data to the database every 10ms using an ExecutorService.
The relevant parts of that class are:
public class SensorService extends Service implements SensorEventListener {
public void onSensorChanged(SensorEvent event) {
//get sensor values here
//insert into database
try{
executor.execute(insertHandler);
} catch (SQLException e) {
Log.e(TAG, "insertData: " + e.getMessage(), e);
}
}
}
#Override
public void onCreate() {
super.onCreate();
dbHelper = new DBHelper(getApplicationContext());
PowerManager manager =
(PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
registerReceiver(receiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
//Executor service and runnable for DB inserts
executor = Executors.newSingleThreadExecutor();
insertHandler = new InsertHandler();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startForeground(Process.myPid(), new Notification());
registerListener();
wakeLock.acquire();
return START_STICKY;
}
#Override
public void onDestroy() {
//Prevent new tasks from being added to thread
executor.shutdown();
try {
//Wait for all tasks to finish before we proceed
while (!executor.awaitTermination(10, TimeUnit.SECONDS)) {
Log.i(TAG, "Waiting for current tasks to finish");
}
} catch (InterruptedException e) {
executor.shutdownNow();
}
//Stop everything else once the task queue is clear
unregisterReceiver(receiver);
unregisterListener();
wakeLock.release();
dbHelper.close();
stopForeground(true);
}
class InsertHandler implements Runnable {
public void run() {
dbHelper.insertData(Short.parseShort(MainActivity.subInfo.get("subNum")), System.currentTimeMillis(),
accelerometerMatrix[0], accelerometerMatrix[1], accelerometerMatrix[2],
accelerometerWorldMatrix[0], accelerometerWorldMatrix[1], accelerometerWorldMatrix[2],
gyroscopeMatrix[0], gyroscopeMatrix[1], gyroscopeMatrix[2]);
}
}
}
So when the user hits the button SensorService goes through its onCreate and onStartCommand methods. Whenever sensor data changes, executor service inserts it into the database
When the user hits the button again to stop, I'm telling the executor to shut down, but to finish processing all of its current tasks first. Once executor queue is cleared, we close the database, unregister listeners etc.
During this stage when the executor is awaiting shutdown and clearing its queue, I want to show a progress dialog to the user to say that its going through the process of closing things down.
So I display the progressdialog in the fragment itself once stop button is pressed. But how do I dismiss the dialog only once the executor service has finish processing its queue and is shut down correctly?
I can easily do this with async tasks, but I have chosen to use executor service for this instead so that is not an option
The progress dialog belongs to the fragment, so I cant do something like MainActivity.dialog.dismiss() from inside the service. I don't feel like thats a very clean way of handling things anyway
So I guess I'm left with Handlers, but not entirely sure how to set it up in this case because the executor service is operating within another service (SensorService). What would the code look like if using a handler to solve this?
You need some mechanism for the service to send a signal to the activity component. The easiest and most common way to do that is using an event bus. The proper implementation of this is completely up to you, but you can use existing event bus libraries to help you. You can find these with a simple google search.
Related
I tried to use the following code trigger toast as background service but it gets executed for 20 times, it was not working till 100. With thread it is not working gives error.
Felt service get destroyed.
How to trigger notification with 30 minutes difference as a background service, though app is closed,
I need to display Good morning, Good Afternoon, Good Evening and Good night as a Notification.
without any internet support.
Is following procedure not ok? I think so. How to do this?
import android.app.Service;
public class HelloService extends Service {
private static final String TAG = "HelloService";
int i=0;
private boolean isRunning = false;
#Override
public void onCreate() {
Log.i(TAG, "Service onCreate");
Toast.makeText(this, " On create Hello Service Started", Toast.LENGTH_LONG).show();
isRunning = true;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Service onStartCommand");
for (;i<100; i++) {
try {
// Thread.sleep(1000);
Toast.makeText(getApplicationContext(), "Hello Service On Loop"+i , Toast.LENGTH_LONG).show();
//
} catch (Exception e) {
}
}
//Stop service once it finishes its task
// i++;
stopSelf();
return Service.START_STICKY;
}
#Override
public IBinder onBind(Intent arg0) {
Log.i(TAG, "Service onBind");
return null;
}
#Override
public void onDestroy() {
isRunning = false;
Log.i(TAG, "Service onDestroy");
}
}
onStartCommand() is called on the main (UI) thread. If you execute a loop inside onStartCommand(), Android will kill the process after about 30 seconds with an ANR (Application Not Responding) because you cannot block the main (UI) thread.
You can do what you want either using AlarmManager to set a timer that will start your Service or trigger a BroadcastReceiver at certain times, or you can post a Runnable to a Handler in onStartCommand() with a certain delay and do whatever you want in the Runnable, or you can start a background thread in onStartCommand() and the background thread can loop and sleep or whatever and then do what you want.
In any case, you cannot show a Toast every second. This will flood the UI with toasts and either Android will dump most of them (ignore them) or the UI will be so busy showing them that Android will kill your app due to ANR or the user will just uninstall your app!
In my android app i have one service which calls some webservices after a fix interval.
App is running perfectly in foreground and refresh data,but when user exit from app and use some other app then my app force close after many times.
Why this app force close while running in background.
Code that i was using for start service -
msgIntent = new Intent(mContext, MyBackgroundService.class);
startService(msgIntent);
and inside onDestroy() of my main activity i have following code to stop service-
if(msgIntent!=null){
stopService(msgIntent);
}
background service call some async task and each aync task onPostExecute() method execute some insert statement in database.
i am not geting why this force close occure.
Please give your comments.
Thanks in advance.
My Service Code
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
callAsynchronousTask();
return Service.START_NOT_STICKY;
}
#Override
public void onCreate() {
mContext = this;
super.onCreate();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
callWebservice();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
timer.schedule(doAsynchronousTask, START_DELAY, DELAY);
}
Actually the problem is here
if(msgIntent!=null){
stopService(msgIntent);
}
in your onDestroy(). Because when you close your application so this above code gets called which is closing your service.
And after closing service again you are trying to insert data by calling service + web service. Hence, there is no service object thats why it gets crashed.
To handle this scenario, you need to comment above code which is in onDestroy() & then check/run it, will solve your problem. & there you need to stop your service by other ways. Go step by step.
you stop the service at onDestroy() method. but services are not depend the activity. So try to neglect the stop service.
(or)
try
{
//stop service code
}
catch(Exception e)
{
}
try this.
Following Googles examples for using a Service, I created a couple of threads like this. I can't use IntentService, because I'm doing something that involves waiting for callbacks.
However, I don't know how to terminate a Thread started in this manner. As I understand it, Threads automatically terminate when the run() method returns. However, this kind of thread doesn't have a run method. My Threads are leaking--they stay alive after stopSelf().
#Override
public void onCreate() {
HandlerThread thread = new HandlerThread("ServiceStartArguments",
android.os.Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
HandlerThread thread2 = new HandlerThread("CallbackHandling",
android.os.Process.THREAD_PRIORITY_BACKGROUND);
thread2.start();
mServiceLooper = thread.getLooper();
mCallbackLooper = thread2.getLooper();
mServiceHandler = new MyHandler(mServiceLooper);
mCallbackHandler = new Handler(mCallbackLooper);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
mMainThreadHandler=new Handler();
// If we get killed, after returning from here, restart
return START_STICKY;
}
private final class MyHandler extends Handler {
public MyHandler(Looper looper) {
super(looper);
}
#Override
public void handleMessage(Message msg) {
cycle();
// Stop the service using the startId, so that we don't stop
// the service in the middle of handling another job
stopSelf(msg.arg1);
}
}
protected void cycle() {
...
mCallbackHandler.post(new Runnable(){
public void run(){
goAskForSomeCallbacks();
}
});
try {
Thread.sleep(GIVE_UP_TIME);
} catch (InterruptedException e){
//The callback will interrupt this thread to stop it from waiting
Log.d(TAG,"Got early callback, stop waiting.");
}
Thread.interrupted(); //clear the interrupt
doStuff();
}
Try calling the quit method on the corresponding Loopers.
You can call quit method on your handler threads.
e.g.
thread.quit();
thread2.quit();
You can also use the following recommended approach:
but be careful before using it because it cancels all pending jobs
handler.quitSafely();
Force closing the app does it but otherwise they just keep going on their own.
I have this application that needs to run a service (background) that beeps periodically.
The phone needs to beep the entire day for 5 seconds every one minute (used a handler in the service). I have implemented this service which does this perfectly, but when the phone goes into deep sleep mode, the execution stops of this handler stops. Using this answer from the question in SO, I managed to use wake locks and it works fine. But when I explicitly put the phone in deep sleep mode, the handler stops executing. Where do I place the wakelock in the service. Code snippet below.
public class PlaySound extends Service{
PowerManager.WakeLock wl ;
PowerManager pm;
private SoundManager mSoundManager;
boolean wakeUpFlag = false;
#Override
public void onCreate(){
super.onCreate();
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.sound);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
startservice();
return START_STICKY;
}
private void startservice() {
System.out.println("Started the service");
timer.scheduleAtFixedRate( new TimerTask() {
public void run() {
toastHandler.sendEmptyMessage(0);
}
}, 0, 60000);
}
private final Handler toastHandler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
result =start();
System.out.println("result"+result);
close();
}
};
protected void close() {
try {
if(wakeUpFlag){
wl.release();
System.out.println("Released the wakelock");
}
if(!pm.isScreenOn()){
System.out.println("Screen is off - back to sleep");
pm.goToSleep(1000);
}
else{
System.out.println("Screen is on - no need to sleep");
}
bs.close();
writer.close();
System.out.println("Closed socket and writer");
System.out.println("Size of file:"+f.length()/1024);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public void start(){
try{
wakeUpFlag = false;
pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
if(!pm.isScreenOn()) {
wakeUpFlag = true;
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,"CollectData");
System.out.println("Screen off - wake lock acquired");
wl.acquire();
}
else{
System.out.println("Screen on - no need of wake lock");
}
}
catch(Exception e){
e.printStackTrace();
}
mSoundManager.playSound(1);
}
I dont think you are using the correct flag accorinding to the android documentation fior PowerManager:
*If you hold a partial wakelock, the CPU will continue to run, irrespective of any timers and even after the user presses the power button. In all other wakelocks, the CPU will run, but the user can still put the device to sleep using the power button.
In other words, try using PARTIAL_WAKE_LOCK as this is the only one that gurantees the cpu to run
Follow the pattern Mark Murphy provides with the WakefulIntentService. I would suggest picking up his books, not only for the detailed explanation of this class and example he includes in one of them, but for the other wealth of information you'll find in them.
I just recently implemented this pattern for my main app and this class works like a charm.
I think you'd be better off using android.app.AlarmManager to schedule a wakeup alarm. Be careful though - you don't want to do any long-running work in your onReceive() method as that's normally called on the main thread, and will hang your activity. You'll still need to acquire the wakelock for the duration of your task to prevent the phone sleeping part-way through.
i have a background service on my android APP that is getting my GPS position and sending it to a remote db. It work's fine.
The problem is when i want to stop the service.... it doesn't stops :S. Also no exception or errors on logcat have appeared... it simply doesn't stops.
this is the code to start my srvice (with a button):
startService(new Intent(GPSLoc.this, MyService.class)); //enciendo el service
this is the code where I stop it (on the onactivityresult method):
stopService(new Intent(GPSLoc.this, MyService.class));
I have been debugged the app, and i checked that the stopService codeline has been called every time that i debugged it, but it doesn't stops......
i am sure that it's not stopped cause on my database i still recive gps positions from the emulator when i have press the button to stop the service.
what i am doing bad?
Have you implemented onDestroy()? If not, I believe that might be the solution - and you stop your Timer or whatever you're using to run the service within onDestroy().
A service can be stopped by calling its stopSelf() method, or by calling Context.stopService().
See this link for some more information.
i am sure that it's not stopped cause on my database i still recive gps positions from the emulator when i have press the button to stop the service.
You probably are not unregistering your LocationListener.
I had the same problem. I found that if the service has GoogleApiClient connected and still get location update, the stopService() has totally no effect, the service's industry() was not called.
To fix the problem, I created a function to stop the location service in the service code. Call the stopLocationService() from the activity, and then call stopService. Here is the code example:
public class myLocationService extends Service{
...
public void stopLocationUpdates() {
LocationService.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);
mGoogleApiClient.disconnect();
}
...
}
In activity,
{
...
if(mService != null && isBound) {
mService.stopLocationUpdates();
doUnbindService();
stopService(new Intent(this, myLocationService.class));
}
...
}
It's very common this situation where I need to stop my service before to finish the process. In some case is not enough with stopService(intent). You should have in mind the onDestroy() implement in my service. Example:
public class MyIntentService extends IntentService {
// Defines and instantiates an object for handling status updates.
private BroadcastNotifier mBroadcaster = null;
private int progress = 0; //THIS IS MY COUNTER FOR EXAMPLE!!!
public MyIntentService() {
super("MyIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
progress = 0;
int tiempo_disponible = intent.getIntExtra("minutos_disponible", 0);
if (mBroadcaster == null){
mBroadcaster = new BroadcastNotifier(this);
}
// Broadcasts an Intent indicating that processing has started.
mBroadcaster.broadcastIntentWithState(Constants.STATE_ACTION_STARTED);
mBroadcaster.broadcastIntentWithState(Constants.STATE_ACTION_RUNNING);
while (progress < tiempo_disponible) {
progress++;
try {
Log.i(Constants.TAG, "Procesing " + progress);
mBroadcaster.notifyProgress(progress);
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Reports that the feed retrieval is complete.
mBroadcaster.broadcastIntentWithState(Constants.STATE_ACTION_COMPLETE);
}
#Override
public void onDestroy() {
progress = 1000000; // WHITH THAT YOU FINISH THE CICLE IF tiempo_disponible NEVER IS MAYOR THAT 1000000, YOU CAN USE OTHER CONDITIONAL!!!!!!
super.onDestroy();
}
}
In this way, when you have stopped the service using stopService method also you will have stopped the process o counter.
public void stopService(){
context.stopService(intent);
LocalBroadcastManager.getInstance(context).unregisterReceiver(responseReceiver);
responseReceiver = null;
intent = null;
}
Take care!
#yaircarreno
If you are tracking GPS location, you probably used GoogleApiClient.
The concept is that the Service WILL NOT stop,
if an GoogleApiClient instance is still connected within it.
(Or any other issue that need to be destroyed / unregistered first)
So to make it works, implement onDestroy() within your service:
#Override
public void onDestroy()
{
// Unregistered or disconnect what you need to
// For example: mGoogleApiClient.disconnect();
super.onDestroy();
}
I have found the best way to stop a service is to make stop itself. This way you are sure it actually will stop and preserve data integrity. If you want to do it from outside (activity) I usually use a global static attribute.
Per example (Kotlin) if I have MyService, MyActivity and MyObject
My Object
object MyObject{
abort = false
}
MyService
override fun onHandleIntent(intent: Intent?) {
startForeground(id,notification)
for (i in range){
if (MyObject.abort) break
// RUN SOME CODE HERE
}
stopForeground(true)
stopSelf()
}
MyActivity
fun startService() {
startForegroundService(Intent(this, OptimizationService::class.java))
}
fun stopService() {
MyObject.abort = true
}
it could be perhaps that you are creating a new Intent everytime you call the stop service.
stopService(new Intent(GPSLoc.this, MyService.class));
perhaps try :
Intent intnet = new Intent(GPSLoc.this, MyService.class); // create el service
startService(intenet);
stopService(intent);
For those who want to send a request to server periodically, this is my solution. You should have this in your Activity or Fragment Activity
{
private static final Long UPDATE_LOCATION_TIME = 30 * 60 * 1000l; // 30 minute
private AlarmManager alarm;
private PendingIntent pIntent;
...
#Override
protected void onResume() {
super.onResume();
// Run background service in order to update users location
startUserLocationService();
Log.e(TAG, "onResume");
}
#Override
protected void onStop() {
super.onStop();
stopUserLocationService();
Log.e(TAG, "onStop");
}
private void startUserLocationService() {
Log.i(TAG, "Starting service...");
Intent intent = new Intent(MainFragmentHolder.this, ServiceUserLocation.class);
pIntent = PendingIntent.getService(this, 0, intent, 0);
alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), UPDATE_LOCATION_TIME, pIntent);
}
private void stopUserLocationService() {
alarm.cancel(pIntent);
Intent intent = new Intent(MainFragmentHolder.this, ServiceUserLocation.class);
stopService(intent);
}
}
my problem solved by removing the added views to WindowManager ondestroy
public void onDestroy() {
isRunning = false;
super.onDestroy();
if (checkBox!=null) {
windowManager.removeView(getlayoutparm(fabsetting,fabrateus,fabexit,true));
windowManager.removeView(checkBox);
}
}
In my case the stopService is called with startService almost simultaneously so no service is there to be stopped. Try delay stopService for a few seconds. :)
#Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.d(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}