如何添加类似WhatsApp通话通知的通知计时器?

4

我有一个通知构建器。我想添加类似于WhatsApp通话通知的通知计时器。所以如果我调用updateNotifTime函数,它不起作用。

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, callChannelId)
                .setSmallIcon(R.drawable.call_icon)
                .setContentIntent(pendingIntent)
                .setOngoing(true)
                .setShowWhen(true)
                .setWhen(System.currentTimeMillis())
                .setPriority(NotificationCompat.PRIORITY_HIGH);

public void updateNotifTime() {
    this.callingElapsedRunnable = new Runnable() {
                @Override
                public void run() {

                    elapsedTime += 1000;

                    String timer = DateUtils.calculateTime(elapsedTime);

                    builder.setWhen(elapsedTime);

                    callingElapsedHandler.postDelayed(this, 1000);
                }
            };

callingElapsedHandler.postDelayed(callingElapsedRunnable, 1000);
    }

whatsapp notification timer my app notification


尝试使用:.setUsesChronometer(true) 提示:https://developer.android.com/reference/android/app/Notification.Builder.html#setUsesChronometer(boolean) - ManmeetP
1
setChronometerCountDown(boolean)未定义。 - propoLis
.setChronometerCountDown(true) - ManmeetP
是的,它不起作用了,被红线覆盖了。 - propoLis
让我们在聊天中继续这个讨论。 (http://chat.stackoverflow.com/rooms/158833/discussion-between-manmeetp-and-questioner) - ManmeetP
显示剩余2条评论
3个回答

3
我已为<5.0 SDK版本添加了自定义布局: < p> < 5.0
public void startChronometer(String elapsedTime) {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && builder != null) {
            RemoteViews mContentView = new RemoteViews(context.getPackageName(), R.layout.call_notification);
            //timerTxt
            TextView timerTxt = new TextView(context);
            timerTxt.setId(R.id.timerTxt);

            //callTypeTxt
            TextView callTypeTxt = new TextView(context);
            callTypeTxt.setId(R.id.callType);

            //calleeNameTxt
            TextView calleeNameTxt = new TextView(context);
            calleeNameTxt.setId(R.id.calleeName);

            mContentView.setTextViewText(timerTxt.getId(), elapsedTime);
            mContentView.setTextViewText(callTypeTxt.getId(), context.getString(R.string.call_in_progress));

            builder.setCustomBigContentView(mContentView);
            notificationManager.notify(CALL_NOTIFICATION_ID, builder.build());
        }
    }

其他版本

(相关于IT技术)
builder.setUsesChronometer(true);

@Kishore Jethava 问题:

Intent intent = new Intent(mContext, YourActivity.class);
intent.putExtra("tappedTime", System.currentTimeMillis());

PendingIntent pendingIntent = PendingIntent.getActivity(mContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

你需要通知计时器吗? - propoLis
是的,和 WhatsApp 通话一样。 - Kishore Jethava
谢谢,现在它可以工作了。当用户点击通知时,我能获取到那个时间吗? - Kishore Jethava
是的,我知道我们可以使用PendingIntent,但是我该如何在Activity中获取它的运行时间? - Kishore Jethava
请尝试 - propoLis
显示剩余2条评论

2
private void sendNotification(String from, String messageText) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(from)
            .setContentText(messageText)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}

0

我使用了一种非常简单的方法来解决这个问题。 我的初始通知

notification = new NotificationCompat.Builder(this, CALL_CHANNEL_ID)
            .setContentText("Incoming Voice Call")
            .setContentTitle(getDoctor().name)
            .setSmallIcon(R.drawable.ic_baseline_call_received_24)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_CALL)
            .setShowWhen(true)
            .setUsesChronometer(true)
            .setFullScreenIntent(voicePendingIntent, true);
    startForeground(1, notification.build());

存储服务启动时的时间戳

    public void startTimer() {
    if (!isTimerRunning) {
        startTime = System.currentTimeMillis();
        isTimerRunning = true;
    } else {
        Log.e(TAG, "startTimer request for an already running timer");
    }
}

每次调用elapsedTime()(通常在活动中获取时间),通知都会更新。
    public long elapsedTime() {
    if (isTimerRunning()) {
        durationMillis = System.currentTimeMillis() - startTime;
        notification.setWhen(startTime);
        notification.setOnlyAlertOnce(true);
        notificationManager.notify(1, notification.build());
        return (durationMillis);
    } else {
        return 0;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接