您好,登錄后才能下訂單哦!
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如何實現動態顯示通話時間問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。