Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize dynamic display of talk time by Android Notification

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In order to solve this problem, this article introduces the corresponding analysis and answer in detail about how to realize the dynamic display of call time by Android Notification, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Based on the android N MTK release of the source code, for your reference, the details are as follows

Main ideas

1. We need to know the time when the call is established, that is, when the state of call changes from INCOMING or DIALING to ACTIVE.

2. Time changes every second, so we need to constantly update the interface of notification. Here, we keep creating the same notification as notify, which has reached the effect of update time.

3. We need CallTimer threads to constantly help us calculate the time and control the update of the interface.

Code implementation

Here is the modification in StatusBarNotifier.java under the source code incallUI directory

.... Omit part of the code / / vibration duration, here is non-vibrating private static final long [] IN_CALL_VIBRATE_PATTERN_NULL = new long [] {0,0,0}; / / how often the thread executes is ms, here 1s private static final long CALL_TIME_UPDATE_INTERVAL_MS = 1000 / / We need to get some global variables to continuously create the notification and update the UI private CallTimer mCallTimer; private Notification.Builder mCopyBuilder; private int mCopyCallState; private ContactCacheEntry mCopyContactInfo; private int mCopyNotificationType; / / ID of the current notification of the same notification. Through this ID, we can always update the same notification and pop up private Bitmap mCopyLargeIcon; public StatusBarNotifier (Context context, ContactInfoCache contactInfoCache) {. Omit part of the code / / the CallTimer object is created when StatusBarNotifier is initialized, which is used to calculate the time and update UI mCallTimer = new CallTimer (new Runnable () {@ Override public void run () {updateCallTime (); / / update UI function}}) } @ Override public void onStateChange (InCallState oldState, InCallState newState, CallList callList) {if (callList.getActiveCall ()) = = null | | callList.getActiveCall () .getState ()! = Call.State.ACTIVE) {/ / the thread mCallTimer.cancel () that needs to cancel the calculation time when the call ends }} / / the method of system building notification private void buildAndSendNotification (Call originalCall, ContactCacheEntry contactInfo) {.... Omit part of the code else if (callState = = Call.State.ACTIVE & &! mIsCallUiShown) {/ / save a common variable to all variables to facilitate the construction of a new notification and refresh copyInfoFromHeadsUpView (builder, callState, contactInfo, notificationType, largeIcon); / / the following is the time final long callStart = call.getConnectTimeMillis () Final long duration = System.currentTimeMillis ()-callStart; / / create a HeadsUpView to display on notification RemoteViews inCall = createInCallHeadsUpView (duration / 1000, largeIcon); builder.setContent (inCall); builder.setCustomHeadsUpContentView (inCall); / / system native method final notify notification fireNotification (builder, callState, contactInfo, notificationType) / / start our timing thread mCallTimer.start (CALL_TIME_UPDATE_INTERVAL_MS);}. Omit part of the code} 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);} / / format time 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;}. Omit part of the code

The CallTimer source code is as follows

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 = nextReport) {nextReport + = mInterval;} postAtTime (mInternalCallback, nextReport); mLastReportedTime = nextReport; / / Run the callback mCallback.run ();} private class CallTimerCallback implements Runnable {@ Override public void run () {periodicUpdateTimer () } this is the answer to the question about how Android Notification can dynamically display call time. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report