4

I want to run service in background even if the app is killed from stack. This functionality is working fine on some devices. But in oppo and vivo phone its not running if app is killed. is there any solution for this. if not then how I can open the allow permission screen.

Here is my code:

Timer_Service.java

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.text.LoginFilter;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import static com.gov.ojas1.WebView.TimerReceiver.CUSTOM_INTENT;


public class Timer_Service extends Service {

    public static String str_receiver = "com.countdowntimerservice.receiver";

    private Handler mHandler = new Handler();
    Calendar calendar;
    SimpleDateFormat simpleDateFormat;
    String strDate;
    Date date_current, date_diff;
    SharedPreferences mpref;
    SharedPreferences.Editor mEditor;

    private Timer mTimer = null;
    public static final long NOTIFY_INTERVAL = 1000;
    Intent intent;
    long int_timer;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mpref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        mEditor = mpref.edit();
        calendar = Calendar.getInstance();
        simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

        mTimer = new Timer();
        mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL);
        intent = new Intent(str_receiver);

        Random r = new Random();
        int i1 = r.nextInt(10800000 - 7200000) + 7200000;
//        int i1 = r.nextInt(300000 - 180000) + 180000;
//        int_timer = (long) (int) i1;

        int_timer =10000;
    }


    class TimeDisplayTimerTask extends TimerTask {

        @Override
        public void run() {
            mHandler.post(new Runnable() {

                @Override
                public void run() {

                    calendar = Calendar.getInstance();
                    simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
                    strDate = simpleDateFormat.format(calendar.getTime());
                    Log.e("strDate", strDate);
                    twoDatesBetweenTime();

                }

            });
        }

    }

    public String twoDatesBetweenTime() {


        try {
            date_current = simpleDateFormat.parse(strDate);
        } catch (Exception e) {

        }

        try {
            date_diff = simpleDateFormat.parse(mpref.getString("data", ""));
        } catch (Exception e) {

        }

        try {


            long diff = date_current.getTime() - date_diff.getTime();
            Log.e("Diff", diff + "");


            Log.e("TImer", int_timer + "");


            long long_hours = int_timer - diff;
            long diffSeconds2 = long_hours / 1000 % 60;
            long diffMinutes2 = long_hours / (60 * 1000) % 60;
            long diffHours2 = long_hours / (60 * 60 * 1000) % 24;


            if (long_hours > 0) {
                String str_testing = diffHours2 + ":" + diffMinutes2 + ":" + diffSeconds2;

                Log.e("TIME", str_testing);

            } else {
                String str_testing = diffHours2 + ":" + diffMinutes2 + ":" + diffSeconds2;
                mEditor.putString("data", strDate).commit();
                fn_update();
                Log.e("TIME", "Finish");


            }
        } catch (Exception e) {
            mTimer.cancel();
            mTimer.purge();
            mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL);
            e.printStackTrace();


        }

        return "";

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("Service finish", "Finish");
    }

    private void fn_update() {

        Intent i = new Intent();
        i.setAction(CUSTOM_INTENT);
        i.putExtra("boolean", true);
        sendBroadcast(i);

    }

}

Thanks in advance.

Dev
  • 275
  • 4
  • 9

2 Answers2

1

You can make your service to run in foreground and that should solve this issue.

You can refer to the following link to do the same :

Android - implementing startForeground for a service?

Community
  • 1
  • 1
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
  • This is an issue specifically on certain devices. Standard solutions like this do not solve the problem. – Mark Aug 07 '17 at 04:52
1

What worked for me was using the android:process tag in the manifest in the service tag:

<service [name, etc]
    android:process=":externalprocess"/>

This makes it run on a separate process so the service isn't killed alongside the app.

Zoe
  • 27,060
  • 21
  • 118
  • 148