在Oreo中开始服务后,手机每隔5秒振动一次

时间:2018-08-09 15:25:40

标签: android android-service android-notifications android-8.0-oreo

我的应用一直运行良好,直到最近我将目标提高到27。 我有一个后台服务,它可以定位并计算用户的行进距离。 更改之后,当我的服务开始计算距离时,手机每3-5秒振动一次。 我还没有编写任何振动的代码。

即使我的应用程序位于前台,我也已经开始发出通知,说明距离的进度。

这是通知代码:

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;
    }

这就是我启动服务的方式:

Intent intent = new Intent(this, WorkoutService.class);
        startService(intent);

1 个答案:

答案 0 :(得分:0)

您必须具有26个及以上版本的通知通道,并且需要启动前台服务以避免Android将其关闭。将通知渠道代码放入服务的onCreate中,因为您只有5秒钟的时间来启动它。设置通知通道后,您可以使声音和振动静音。如果您不希望听到任何声音或振动,请确保将通知重要性设置为LOW,并将这两个属性添加到通道(注意-这不是完整的代码,请参见完整示例的链接):

PrintWindow

有关通知频道的奥利奥要求的很好解释:https://medium.com/cr8resume/notification-in-android-8-0-oreo-implementing-notification-channels-d65b0f81ca50

有关启动前台服务的有用信息:Android O - Old start foreground service still working?