服务会在一段时间后停止工作.需要持续工作 [英] Service stops working after sometime. Needed to work continuously

查看:80
本文介绍了服务会在一段时间后停止工作.需要持续工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个计步器应用程序,该应用程序可以计算步行的步数并在午夜将其更新到服务器.我有一项不断运行的服务来完成所有这一切.

I am developing a step counter app where I count the number of steps walked and update it to the server at midnight. I have a service running continuously to do all this.

这是我的服务

    public class StepCounterService extends Service implements SensorEventListener, StepListener, WebServiceInterface, GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {

    private static final int SERVICE_ID = 27;
    private static final int SEND_SESSION_REQUEST_CODE = 1;
    private static final int SEND_ACTIVITY_REQUEST_CODE = 2;
    private static final int MAIN_NOTIFICATION_ID = 3;
    private static final int SECONDARY_NOTIFICATION_ID = 4;
    private LocalBroadcastManager broadcaster;

    static final public String STEP_INCREMENT = "com.app.STEP_INCREMENTED";
    static final public String SESSION_COMPLETE = "com.app.SESSION_COMPLETE";
    static final public String ACTIVITY_COMPLETE = "com.app.ACTIVITY_COMPLETE";
    static final public String STEP_INCREMENT_KEY = "step_count";

    Session session;

    private StepDetector stepDetector;
    private SensorManager sensorManager;
    private Sensor sensor;
    private int numberOfSteps = 0;

    private GoogleApiClient googleApiClient;
    private LocationRequest mLocationRequest;
    double currentLatitude;
    double currentLongitude;
    ArrayList<String> arrayListLocations;
    private AlarmManager sendActivityAlarmManager;
    private PendingIntent activityAlarmIntent;

    private NotificationManager notificationManager;
    RemoteViews contentView;
    PowerManager.WakeLock wl;
    private String TAG = "Wake Lock Tag";
    private int SET_LAST_LOCATION_REQUEST_CODE = 5;


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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        return START_STICKY;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        // TODO Auto-generated method stub
        System.out.println("---- In onTaskRemoved Function");
        restartKilledService();
    }

    @Override
    public void onDestroy() {
        System.out.println("---- In onDestroy Function");
        if (wl != null) {
            wl.release();
        }
        super.onDestroy();
        restartKilledService();
    }

    void restartKilledService() {

        System.out.println("---- In restartKilledService Function");

        Intent restartService = new Intent(getApplicationContext(), StepCounterService.class);
        restartService.setPackage(getPackageName());
        PendingIntent restartServicePI = PendingIntent.getService(getApplicationContext(), StepCounterService.SERVICE_ID, restartService, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 100, restartServicePI);
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub

        PowerManager pm = (PowerManager) getApplicationContext().getSystemService(getApplicationContext().POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
        wl.acquire();

        super.onCreate();

        session = new Session(this);
        buildGoogleApiClient();

        broadcaster = LocalBroadcastManager.getInstance(this);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        stepDetector = new StepDetector();
        stepDetector.registerListener(StepCounterService.this);

        Context ctx = getApplicationContext();
        Calendar cal = Calendar.getInstance();
        AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        long interval = 1000 * 60 * 5; // 5 minutes in milliseconds
        Intent serviceIntent = new Intent(ctx, StepCounterService.class);

        PendingIntent servicePendingIntent = PendingIntent.getService(ctx, StepCounterService.SERVICE_ID, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), interval, servicePendingIntent);

        sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);

        notificationManager = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
        contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
        contentView.setImageViewResource(R.id.image, R.drawable.notif_icon);

        if (session.getUser() != null && !(session.getUser().getName().equals("")))
            updateMainNotification(session.getTodaySteps() + "");

        startAlarm();
    }


    protected synchronized void buildGoogleApiClient() {
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        googleApiClient.connect();

        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(10 * 1000)        // 10 seconds, in milliseconds
                .setFastestInterval(1 * 1000);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            stepDetector.updateAccel(event.timestamp, event.values[0], event.values[1], event.values[2]);
        }
    }

    @Override
    public void step(long timeNs) {
        numberOfSteps = session.getTodaySteps();
        numberOfSteps++;

        sendStepIncrementBroadcast(numberOfSteps);

        session.setTodaySteps(numberOfSteps);

        if (session.getUser() != null && !(session.getUser().getName().equals("")))
            updateMainNotification(numberOfSteps + "");
        else {
            try {
                notificationManager.cancel(MAIN_NOTIFICATION_ID);
            } catch (Exception e) {

            }
        }
    }

    public void sendStepIncrementBroadcast(int numberOfSteps) {
        Intent intent = new Intent(STEP_INCREMENT);
        intent.putExtra(STEP_INCREMENT_KEY, numberOfSteps);
        broadcaster.sendBroadcast(intent);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    public float[] getDataFromSteps(int stepsCount) {
        float caloriesCount = 0;
        float creditsCount = 0;
        try {
            double adjustedWeight = Double.parseDouble(session.getUser().getWeight()) / LinksAndKeys.weightAdjuster;
            caloriesCount = Math.round(((adjustedWeight * LinksAndKeys.metValue) / LinksAndKeys.setPace) * (stepsCount / LinksAndKeys.stepsPerMile));
            caloriesCount = caloriesCount * 1.2f;
            creditsCount = caloriesCount / 25.4f;
        } catch (Exception e) {
            caloriesCount = 0;
            creditsCount = 0;
        }

        float[] resultantFloat = {caloriesCount, creditsCount};
        return resultantFloat;
    }

    private void startAlarm() {

        sendActivityAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(this, ActivityCompleteReceiver.class);
        activityAlarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 58);
//        calendar.set(Calendar.HOUR_OF_DAY, generateRandomTime()[0]);
//        calendar.set(Calendar.MINUTE, generateRandomTime()[1]);

        if (System.currentTimeMillis() > calendar.getTimeInMillis()) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }

        sendActivityAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, activityAlarmIntent);
    }

    private void updateMainNotification(String stepsValue) {

        String title = "Today: " + stepsValue + " steps - " + LinksAndKeys.decimalFormat.format(getDataFromSteps(session.getTodaySteps())[1]) + " FIMOs";
        String message = "Keep Walking and Keep Earning";

        contentView.setTextViewText(R.id.textViewTitle, title);
        contentView.setTextViewText(R.id.textViewMessage, message);

        Intent notificationIntent = new Intent(StepCounterService.this, SplashActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(StepCounterService.this, 0, notificationIntent, 0);


        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.running_icon)
                .setContent(contentView).setContentIntent(intent);

        Notification notification = mBuilder.build();
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
//        notificationManager.notify(MAIN_NOTIFICATION_ID, notification);
        startForeground(MAIN_NOTIFICATION_ID, notification);
    }

    private void updateReminderNotification() {

        String title = "A gentle reminder";
        String message = "Its time to get on track";

        contentView.setTextViewText(R.id.textViewTitle, title);
        contentView.setTextViewText(R.id.textViewMessage, message);

        Intent notificationIntent = new Intent(StepCounterService.this, SplashActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(StepCounterService.this, 0, notificationIntent, 0);


        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.running_icon)
                .setContent(contentView).setContentIntent(intent);

        Notification notification = mBuilder.build();
        notificationManager.notify(SECONDARY_NOTIFICATION_ID, notification);
    }

    private int[] generateRandomTime() {
        int[] timeIntegers = new int[2];

        final Random r = new Random();
        timeIntegers[0] = r.nextInt(58 - 56) + 56;
        timeIntegers[1] = r.nextInt(59 - 1) + 1;

        return timeIntegers;
    }
}

这是活动完成接收者:

public class ActivityCompleteReceiver extends BroadcastReceiver implements WebServiceInterface {

    Session session;
    private LocalBroadcastManager broadcaster;
    static final public String ACTIVITY_COMPLETE = "com.fimo.ACTIVITY_COMPLETE";
    private static final int SEND_ACTIVITY_REQUEST_CODE = 1;
    Gson gson;
    MyDatabase myDatabase;
    UserActivity currentUserActivity;

    @Override
    public void onReceive(Context context, Intent intent) {

        session = new Session(context);
        broadcaster = LocalBroadcastManager.getInstance(context);
        myDatabase = new MyDatabase(context);
        gson = new Gson();

        sendActivityToServer(context, session.getUser().getId(), session.getTodaySteps());
    }

    private void sendActivityToServer(Context context, String id, int steps) {

        Calendar c = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = dateFormat.format(c.getTime());

        UserActivity userActivity = new UserActivity();
        userActivity.setDate(date);
        userActivity.setSteps(steps);
        userActivity.setCalories(getDataFromSteps(steps)[0]);
        userActivity.setCredits(getDataFromSteps(steps)[1]);

        currentUserActivity = userActivity;

        if (isNetworkAvailable(context)) {
            HashMap<String, String> paramsList = new HashMap<>();
            ArrayList<UserActivity> arrayListUserActivity = myDatabase.getAllUserActivities();
            arrayListUserActivity.add(userActivity);

            paramsList.put(LinksAndKeys.ID_KEY, id);
            paramsList.put(LinksAndKeys.DATA_KEY, gson.toJson(arrayListUserActivity));

            Log.d("Receiver Request ----", id + " - " + gson.toJson(arrayListUserActivity));

            WebServiceController webServiceController = new WebServiceController(
                    context, ActivityCompleteReceiver.this);
            String hitURL = LinksAndKeys.SEND_ACTIVITY_URL;
            webServiceController.sendSilentRequest(false, hitURL, paramsList, SEND_ACTIVITY_REQUEST_CODE);
        } else {
            myDatabase.addUserActivity(currentUserActivity);
            currentUserActivity = null;

            Intent in = new Intent(ACTIVITY_COMPLETE);
            broadcaster.sendBroadcast(in);

            session.setTodaySteps(0);
        }

    }

    private boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

    @Override
    public void getResponse(int responseCode, String responseString, String requestType, int requestCode) {
        if (requestCode == SEND_ACTIVITY_REQUEST_CODE && responseCode == 200) {
            try {
                JSONObject responseObject = new JSONObject(responseString);
                String message = responseObject.getString("message");
                if (message.equals("Success")) {

                    myDatabase.deleteAllUserActivities();

                    JSONObject jsonObject = responseObject.getJSONObject("data");
                    session.setServerCredits(Float.parseFloat(jsonObject.getString("credits")));

                    Intent in = new Intent(ACTIVITY_COMPLETE);
                    broadcaster.sendBroadcast(in);

                    session.setTodaySteps(0);
                } else {
                    myDatabase.addUserActivity(currentUserActivity);
                    currentUserActivity = null;

                    Intent in = new Intent(ACTIVITY_COMPLETE);
                    broadcaster.sendBroadcast(in);

                    session.setTodaySteps(0);
                }

            } catch (Exception e) {
                myDatabase.addUserActivity(currentUserActivity);
                currentUserActivity = null;

                Intent in = new Intent(ACTIVITY_COMPLETE);
                broadcaster.sendBroadcast(in);

                session.setTodaySteps(0);
            }
        } else {
            if (currentUserActivity != null) {
                myDatabase.addUserActivity(currentUserActivity);
                currentUserActivity = null;

                Intent in = new Intent(ACTIVITY_COMPLETE);
                broadcaster.sendBroadcast(in);

                session.setTodaySteps(0);
            }
        }
    }

    public float[] getDataFromSteps(int stepsCount) {
        float caloriesCount = 0;
        float creditsCount = 0;
        try {
            double adjustedWeight = Double.parseDouble(session.getUser().getWeight()) / LinksAndKeys.weightAdjuster;
            caloriesCount = Math.round(((adjustedWeight * LinksAndKeys.metValue) / LinksAndKeys.setPace) * (stepsCount / LinksAndKeys.stepsPerMile));
            caloriesCount = caloriesCount * 1.2f;
            creditsCount = caloriesCount / 25.4f;
        } catch (Exception e) {
            caloriesCount = 0;
            creditsCount = 0;
        }

        float[] resultantFloat = {caloriesCount, creditsCount};
        return resultantFloat;
    }
}

编写的代码应该以这种方式运行:

The code written is supposed to behave on this way:

每天计算用户采取的步骤数.->在午夜11.56-11.59之间的特定时间,将数据发送到活动完成接收器->接收器接收到数据,并尝试将其发送到服务器(如果可以使用互联网).如果不是,则将其保存到本地数据库->保存或发送后,将步骤重置为0,第二天服务将从0开始重新计数.

Everyday count the number of steps taken by the user. -> At midnight at a particular time between 11.56-11.59, send the data to activity complete receiver -> The receiver receives the data and tries to send it to the server if internet is available. If not, then it saves it to local database -> After saving or sending, reset the steps to 0 and service starts counting again from 0 for the next day.

问题是服务在给定时间几天实际上无法正常工作,并且接收方未收到活动的全部意图.如果我保持手机开机并通过将时间设置为与当前时间相差几分钟的时间来进行测试,则该服务可以正常工作.但是,如果长时间不使用手机,我认为这种情况会在服务停止工作时发生.这是我的猜测,实际问题可能是其他问题.任何建议或解决方案均受到高度赞赏.

The problem is the service is not actually working some days at the given time and receiver does not receive the activity complete intent. If I keep the phone on and test it by setting a time which is a few minutes from current time, service works. But if the phone is kept untouched from a long time then I think this case happens as the service stops working. This is my guess, the actual problem might be something else. Any suggestions or solutions are highly appreciated.

推荐答案

您似乎遇到了在

当设备使用电池供电且屏幕关闭一定时间后,设备会进入打and并应用第一个限制子集:它会关闭应用程序网络访问,并推迟作业和同步.如果进入Doze后设备在一定时间内保持静止,则系统会将其余的Doze限制应用于PowerManager.WakeLock,AlarmManager警报,GPS和Wi-Fi扫描.无论是否应用了部分或全部打restrictions"限制,系统都会在短暂的维护时段唤醒设备,在此期间,允许应用程序访问网络并可以执行任何延迟的作业/同步.

When a device is on battery power, and the screen has been off for a certain time, the device enters Doze and applies the first subset of restrictions: It shuts off app network access, and defers jobs and syncs. If the device is stationary for a certain time after entering Doze, the system applies the rest of the Doze restrictions to PowerManager.WakeLock, AlarmManager alarms, GPS, and Wi-Fi scans. Regardless of whether some or all Doze restrictions are being applied, the system wakes the device for brief maintenance windows, during which applications are allowed network access and can execute any deferred jobs/syncs.

因此,即使您设法触发警报,例如使用 setAndAllowWhileIdle(),您仍然无法访问网络.通常的建议是使用 JobScheduler 或类似的东西. Firebase JobDispatcher JobInfo.Builder.setRequiredNetworkType() .时间将取决于Doze进入其空闲维护"窗口的时间,但是至少您知道发生这种情况时您将可以访问网络.

So even if you manage to get an alarm to fire, e.g. with setAndAllowWhileIdle(), you still won't have network access. The usual recommendation is to use JobScheduler or something like it e.g. Firebase JobDispatcher and Evernote Android-Job. These frameworks allow configuration of jobs that will run when network becomes available, e.g. with JobInfo.Builder.setRequiredNetworkType(). The timing will be at the mercy of when Doze goes into its "idle maintenance" window, but at least you know you'll have network access when it happens.

如果需要对时序进行更多控制,则可以设置一个在空闲时唤醒的警报,然后使用GCM/FCM高优先级消息来强制网络访问.我不确定您如何在Firebase端上挑选这些-这取决于您的服务器端代码.

If more control over timing is required, it may be possible to set an alarm that will wake while idle, and then use GCM/FCM high priority messages to force network access. I'm not sure how you pick those up on the Firebase end though -- that would be up to your server side code.

这篇关于服务会在一段时间后停止工作.需要持续工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆