欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Notification實(shí)現(xiàn)動(dòng)態(tài)顯示通話時(shí)間

 更新時(shí)間:2021年09月24日 10:23:51   作者:王先生技術(shù)棧  
這篇文章主要為大家詳細(xì)介紹了Android Notification實(shí)現(xiàn)動(dòng)態(tài)顯示通話時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

基于android N MTK釋放的源碼,供大家參考,具體內(nèi)容如下

本文主要講解如何在 IncallUI 的notification 上面不停地更新顯示當(dāng)前已通話多長(zhǎng)時(shí)間,從而達(dá)到和incallUI通話界面上的通話時(shí)間一致。

主要思路

1、我們需要知道通話建立時(shí)的時(shí)間,即call 的狀態(tài)從 INCOMING或者DIALING 轉(zhuǎn)變成ACTIVE的時(shí)候
2、時(shí)間每秒鐘都會(huì)發(fā)生變化,所以我們就需要不停的更新notification的界面,我們這里是不停的創(chuàng)建和notify同一個(gè)notification,已到達(dá)更新時(shí)間的效果
3、我們需要CallTimer線程不停的幫我們計(jì)算時(shí)間,并控制界面的更新

代碼實(shí)現(xiàn)

這里是在源碼incallUI目錄下的StatusBarNotifier.java中修改

....省略部分代碼
    //震動(dòng)時(shí)長(zhǎng),這里為不振動(dòng)
    private static final long[] IN_CALL_VIBRATE_PATTERN_NULL = new long[] {0, 0, 0};
    //線程隔多久執(zhí)行一次已ms為單位,這里為1S
    private static final long CALL_TIME_UPDATE_INTERVAL_MS = 1000;

    //我們需要獲取一些全局的變量,已到達(dá)不停的創(chuàng)建notification并更新同一個(gè)notification的UI
    private CallTimer mCallTimer;
    private Notification.Builder mCopyBuilder;
    private int  mCopyCallState;
    private ContactCacheEntry mCopyContactInfo;
    private int mCopyNotificationType; //當(dāng)前notification的ID,通過(guò)這個(gè)ID我們可以一直更新同一個(gè)notification并且只會(huì)彈出一次
    private Bitmap mCopyLargeIcon;

    public StatusBarNotifier(Context context, ContactInfoCache contactInfoCache) {
        .......省略部分代碼
        //StatusBarNotifier初始化的時(shí)候就創(chuàng)建CallTimer對(duì)象,用來(lái)計(jì)算時(shí)間和更新UI
        mCallTimer = new CallTimer(new Runnable() {
            @Override
            public void run() {
                updateCallTime(); //更新UI的函數(shù)
            }
        });
    }

    @Override
    public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
        if (callList.getActiveCall() == null || callList.getActiveCall().getState() != Call.State.ACTIVE){
            //當(dāng)通話結(jié)束時(shí)需要取消計(jì)算時(shí)間的線程
            mCallTimer.cancel();
        }
    }

    //系統(tǒng)構(gòu)建notification的方法
    private void buildAndSendNotification(Call originalCall, ContactCacheEntry contactInfo) {
        ....省略部分代碼
        else if (callState == Call.State.ACTIVE && !mIsCallUiShown) {
            //保存一個(gè)公共的變量到全部變量里面,方便后面構(gòu)造新的notification并刷新
            copyInfoFromHeadsUpView(builder, callState, contactInfo, notificationType, largeIcon);
            //下面是計(jì)算顯示的時(shí)間
            final long callStart = call.getConnectTimeMillis();
            final long duration = System.currentTimeMillis() - callStart;
            //創(chuàng)建一個(gè)HeadsUpView顯示在notification上面
            RemoteViews inCall = createInCallHeadsUpView(duration / 1000, largeIcon);
            builder.setContent(inCall);
            builder.setCustomHeadsUpContentView(inCall);
            //系統(tǒng)原生的方法最后notify通知
            fireNotification(builder, callState, contactInfo, notificationType);
            //開(kāi)始我們的計(jì)時(shí)線程
            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);
        }
        //格式化時(shí)間
        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();
        }
    }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論