0

My app was working perfectly till I recently increased my target to 27. I have a background service which takes location and calculates the distance traveled by the user. After this change when my service starts to calculate the distance, the phone vibrates after every 3-5 secs. I haven't written any code to vibrate.

I have started a notification even if my app is in foreground stating the progress of the distance.

This is the code of notification :

private NotificationCompat.Builder getForegroundNotificationBuilder() {

        int rupees = Utils.convertDistanceToRupees(mCauseData.getConversionRate(), getTotalDistanceCoveredInMeters());
        String amountString = UnitsManager.formatRupeeToMyCurrency(rupees);

        String pauseResumeAction, pauseResumeLabel, contentTitle;
        int pauseResumeIntent;
        int pauseResumeDrawable;
        if (tracker.isRunning()) {
            pauseResumeAction = getString(R.string.notification_action_pause);
            pauseResumeLabel = getString(R.string.pause);
            contentTitle = getString(R.string.impact_with_sponsor, mCauseData.getSponsor().getName());
            pauseResumeIntent = MainActivity.INTENT_PAUSE_RUN;
            pauseResumeDrawable = R.drawable.ic_pause_black_24px;
        } else {
            pauseResumeAction = getString(R.string.notification_action_resume);
            pauseResumeLabel = getString(R.string.resume);
            contentTitle = getString(R.string.paused);
            pauseResumeIntent = MainActivity.INTENT_RESUME_RUN;
            pauseResumeDrawable = R.drawable.ic_play_arrow_black_24px;
        }
        /*Intent pauseResumeIntent = new Intent(this, NotificationActionReceiver.class);
        pauseResumeIntent.setAction(pauseResumeAction);
        PendingIntent pendingIntentPauseResume = PendingIntent.getBroadcast(getContext(), 100, pauseResumeIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);*/
        NotificationCompat.Builder mBuilder =null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
           mBuilder = Utils.createChannelForNotification(getContext(),getContext().getString(R.string.channel_description_workout));
        }else
        {
            mBuilder = new NotificationCompat.Builder(this);
        }


            mBuilder.setContentTitle(contentTitle)
                        .setContentText("Test distance")

                        .setSmallIcon(getNotificationIcon())
                        .setColor(ContextCompat.getColor(getContext(), R.color.bright_sky_blue))
                        .setLargeIcon(getLargeIcon())
                        .setTicker(getBaseContext().getResources().getString(R.string.app_name))
                        .setOngoing(true)
                        .setVisibility(1);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            mBuilder.addAction(pauseResumeDrawable, pauseResumeLabel, MainApplication.getInstance().createNotificationActionIntent(pauseResumeIntent, pauseResumeAction))
                    .addAction(R.drawable.ic_stop_black_24px, "Stop", MainApplication.getInstance().createNotificationActionIntent(MainActivity.INTENT_STOP_RUN, getString(R.string.notification_action_stop)));
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
        }

        mBuilder.setContentIntent(MainApplication.getInstance().createAppIntent());
        return mBuilder;
    }

And this is how I start the service :

Intent intent = new Intent(this, WorkoutService.class);
        startService(intent);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Parth Anjaria
  • 3,961
  • 3
  • 30
  • 62
  • Android Oreo has limitation on Services: https://developer.android.com/about/versions/oreo/background I think your background service is killed every 5 seconds and then recreated by the System. Please read that link. – emandt Aug 09 '18 at 15:34

1 Answers1

0

You must have a notification channel for 26 and up, and a foreground service needs to be started to avoid Android shutting it down. Put the notification channel code in your onCreate of Service, because you only have 5 seconds to start it. Once you set the notification channel you can mute the sound and vibration. If you don't want any sound or vibration, make sure you set the notification importance to LOW, and add these two attributes to the channel (note- this is not the full code, see the links for full examples):

NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Channel human readable title", NotificationManager.IMPORTANCE_LOW);
        channel.setSound(null, null);
channel.enableVibration(false);

A good explanation of Oreo requirements for a notification channel: https://medium.com/cr8resume/notification-in-android-8-0-oreo-implementing-notification-channels-d65b0f81ca50

Helpful information about starting a foreground service: Android O - Old start foreground service still working?

rdmc
  • 21
  • 5