Android 如何添加通知计时器,如whatsapp呼叫通知?
我有一个通知生成器。我想添加通知计时器,如whatsapp调用通知。所以如果我调用updateNotifTime函数,它就不起作用了Android 如何添加通知计时器,如whatsapp呼叫通知?,android,timer,notifications,call,Android,Timer,Notifications,Call,我有一个通知生成器。我想添加通知计时器,如whatsapp调用通知。所以如果我调用updateNotifTime函数,它就不起作用了 NotificationCompat.Builder builder = new NotificationCompat.Builder(context, callChannelId) .setSmallIcon(R.drawable.call_icon) .setContentIntent(pendi
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);
}
我为添加了自定义布局,我用一种非常简单的方法解决了这个问题。 我最初的通知
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;
}
}
尝试使用:.setUsesChronometer(true)提示:我尝试过这个,但仍然不起作用:(哦,你能尝试并行使用吗:setChronometerCountDown(布尔)setChronometerCountDown(布尔)未定义
。setChronometerCountDown(true)
我已经使用了这个代码,但是我仍然收到了错误的通知
你想要通知计时器吗?是的,和什么应用程序调用一样。谢谢,它现在工作了。当用户点击那个通知时,我能得到那个时间吗?是的,你可以收听那个点击的通知。
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());
}
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");
}
}
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;
}
}