最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Notification實現(xiàn)動態(tài)顯示通話時間

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

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

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

主要思路

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

代碼實現(xiàn)

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

....省略部分代碼
    //震動時長,這里為不振動
    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;

    //我們需要獲取一些全局的變量,已到達不停的創(chuàng)建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初始化的時候就創(chuàng)建CallTimer對象,用來計算時間和更新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){
            //當通話結(jié)束時需要取消計算時間的線程
            mCallTimer.cancel();
        }
    }

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

南江县| 湟源县| 宣恩县| 梓潼县| 攀枝花市| 田林县| 张北县| 道孚县| 河东区| 景宁| 盐山县| 宿迁市| 肇州县| 洪江市| 吴堡县| 新泰市| 临沧市| 友谊县| 苍山县| 射阳县| 上饶市| 普格县| 铜鼓县| 大田县| 库尔勒市| 甘谷县| 平武县| 敦化市| 广宁县| 五原县| 三明市| 东安县| 伽师县| 晴隆县| 乳山市| 临高县| 宝鸡市| 板桥市| 陈巴尔虎旗| 金寨县| 长武县|