代码之家  ›  专栏  ›  技术社区  ›  Parth Anjaria

Oreo服务启动后每5秒手机振动一次

  •  0
  • Parth Anjaria  · 技术社区  · 8 年前

    我有一个后台服务,它获取位置并计算用户的行程。 更改后,当我的服务开始计算距离时,手机每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 回复  |  直到 8 年前
        1
  •  0
  •   rdmc    8 年前

    你必须有一个26岁及以上的通知频道,并且需要启动前台服务以避免Android关闭它。将通知通道代码放入onCreate of Service中,因为您只有5秒的时间来启动它。一旦你设置了通知通道,你可以静音的声音和振动。如果不需要任何声音或振动,请确保将通知重要性设置为“低”,并将这两个属性添加到频道(注意-这不是完整的代码,请参阅链接以获取完整示例):

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

    https://medium.com/cr8resume/notification-in-android-8-0-oreo-implementing-notification-channels-d65b0f81ca50

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

    推荐文章