千家信息网

Android Notification如何实现动态显示通话时间

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,Android Notification如何实现动态显示通话时间,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。基于android N
千家信息网最后更新 2026年11月03日Android Notification如何实现动态显示通话时间

Android Notification如何实现动态显示通话时间,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

基于android N MTK释放的源码,供大家参考,具体内容如下

主要思路

1、我们需要知道通话建立时的时间,即call 的状态从 INCOMING或者DIALING 转变成ACTIVE的时候
2、时间每秒钟都会发生变化,所以我们就需要不停的更新notification的界面,我们这里是不停的创建和notify同一个notification,已到达更新时间的效果
3、我们需要CallTimer线程不停的帮我们计算时间,并控制界面的更新

代码实现

这里是在源码incallUI目录下的StatusBarNotifier.java中修改

....省略部分代码    //震动时长,这里为不振动    private static final long[] IN_CALL_VIBRATE_PATTERN_NULL = new long[] {0, 0, 0};    //线程隔多久执行一次已ms为单位,这里为1S    private static final long CALL_TIME_UPDATE_INTERVAL_MS = 1000;    //我们需要获取一些全局的变量,已到达不停的创建notification并更新同一个notification的UI    private CallTimer mCallTimer;    private Notification.Builder mCopyBuilder;    private int  mCopyCallState;    private ContactCacheEntry mCopyContactInfo;    private int mCopyNotificationType; //当前notification的ID,通过这个ID我们可以一直更新同一个notification并且只会弹出一次    private Bitmap mCopyLargeIcon;    public StatusBarNotifier(Context context, ContactInfoCache contactInfoCache) {        .......省略部分代码        //StatusBarNotifier初始化的时候就创建CallTimer对象,用来计算时间和更新UI        mCallTimer = new CallTimer(new Runnable() {            @Override            public void run() {                updateCallTime(); //更新UI的函数            }        });    }    @Override    public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {        if (callList.getActiveCall() == null || callList.getActiveCall().getState() != Call.State.ACTIVE){            //当通话结束时需要取消计算时间的线程            mCallTimer.cancel();        }    }    //系统构建notification的方法    private void buildAndSendNotification(Call originalCall, ContactCacheEntry contactInfo) {        ....省略部分代码        else if (callState == Call.State.ACTIVE && !mIsCallUiShown) {            //保存一个公共的变量到全部变量里面,方便后面构造新的notification并刷新            copyInfoFromHeadsUpView(builder, callState, contactInfo, notificationType, largeIcon);            //下面是计算显示的时间            final long callStart = call.getConnectTimeMillis();            final long duration = System.currentTimeMillis() - callStart;            //创建一个HeadsUpView显示在notification上面            RemoteViews inCall = createInCallHeadsUpView(duration / 1000, largeIcon);            builder.setContent(inCall);            builder.setCustomHeadsUpContentView(inCall);            //系统原生的方法最后notify通知            fireNotification(builder, callState, contactInfo, notificationType);            //开始我们的计时线程            mCallTimer.start(CALL_TIME_UPDATE_INTERVAL_MS);        }        .....省略部分代码    }    private RemoteViews createInCallHeadsUpView(Long callDuration, Bitmap contactAvatar) {        RemoteViews headsUpView = new RemoteViews(mContext.getPackageName(), R.layout.in_call_headsup);        if (null != contactAvatar) {            headsUpView.setImageViewBitmap(R.id.in_call_hu_avatar, contactAvatar);        }        //格式化时间        String callTimeElapsed = DateUtils.formatElapsedTime(callDuration);        headsUpView.setTextViewText(R.id.in_call_hu_elapsedTime, callTimeElapsed);        return headsUpView;    }    /*according the mCallTimer to update time data*/    public void updateCallTime() {        Call call = CallList.getInstance().getActiveCall();        final long callStart = call.getConnectTimeMillis();        final long duration = System.currentTimeMillis() - callStart;        RemoteViews inCall = createInCallHeadsUpView(duration / 1000, mCopyLargeIcon);        mCopyBuilder.setContent(inCall);        mCopyBuilder.setCustomHeadsUpContentView(inCall);        Notification notification = mCopyBuilder.build();        notification.vibrate = IN_CALL_VIBRATE_PATTERN_NULL;        mNotificationManager.notify(mCopyNotificationType, notification);    }    /*Change local variables  to global variables*/    private void copyInfoFromHeadsUpView(Notification.Builder builder, int callState, ContactCacheEntry contactInfo,                          int notificationType, Bitmap largeIcon){        mCopyBuilder = builder;        mCopyCallState = callState;        mCopyContactInfo = contactInfo;        mCopyNotificationType = notificationType;        mCopyLargeIcon = largeIcon;    }........省略部分代码

CallTimer源码如下

package com.android.incallui;import com.google.common.base.Preconditions;import android.os.Handler;import android.os.SystemClock;/** * Helper class used to keep track of events requiring regular intervals. */public class CallTimer extends Handler {    private Runnable mInternalCallback;    private Runnable mCallback;    private long mLastReportedTime;    private long mInterval;    private boolean mRunning;    public CallTimer(Runnable callback) {        Preconditions.checkNotNull(callback);        mInterval = 0;        mLastReportedTime = 0;        mRunning = false;        mCallback = callback;        mInternalCallback = new CallTimerCallback();    }    public boolean start(long interval) {        if (interval <= 0) {            return false;        }        // cancel any previous timer        cancel();        mInterval = interval;        mLastReportedTime = SystemClock.uptimeMillis();        mRunning = true;        periodicUpdateTimer();        return true;    }    public void cancel() {        removeCallbacks(mInternalCallback);        mRunning = false;    }    private void periodicUpdateTimer() {        if (!mRunning) {            return;        }        final long now = SystemClock.uptimeMillis();        long nextReport = mLastReportedTime + mInterval;        while (now >= nextReport) {            nextReport += mInterval;        }        postAtTime(mInternalCallback, nextReport);        mLastReportedTime = nextReport;        // Run the callback        mCallback.run();    }    private class CallTimerCallback implements Runnable {        @Override        public void run() {            periodicUpdateTimer();        }    }}

关于Android Notification如何实现动态显示通话时间问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

时间 更新 代码 部分 线程 变量 方法 源码 问题 动态 内容 时候 更多 界面 系统 帮助 解答 易行 简单易行 全局 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网络科技 广州紫鲸互联网科技有限公司 疫情对互联网科技公司影响 北京互联网络科技有限公司 致聘互联网科技 互联网 视角下的科技创新 互联网科技汛 顺网科技2021年互联网排名 恒生科技互联网指数分析 互联网系的金融科技公司 浙江互联网科技研学营 互联网科技商业 亳州好赞互联网科技有限公司 想开互联网科技公司怎么样 内蒙古九一互联网科技 恒生互联网科技业指数调整周期 互联网科技融合创新 萌小萌互联网科技河南有限公司 科技互联网专业工作会议发言稿 互联网科技自媒体灵感 互联网科技 互联网科技社 金融科技 互联网金融 成都仁贤领冠互联网科技有限公司 欧洲十大科技互联网公司 特约风豹互联网科技为什么扣费 雨阳互联网卡科技有限公司 银川新华互联网科技学校原画 瑞安市蟠桃会互联网科技有限公司 前海云享互联网科技
0