Oschina Android client source code learning 3, oschina android
Today, let's study a function, Message notification.
(1) obtain or generate a message push.
There is such a function in the main. java file.
/*** Polling notification information */private void foreachUserNotice () {final int uid = appContext. getLoginUid (); final Handler handler = new Handler () {public void handleMessage (Message msg) {if (msg. what = 1) {UIHelper. sendBroadCast (Main. this, (Notice) msg. obj) ;}foreachusernotice (); // callback }}; new Thread () {public void run () {Message msg = new Message (); try {sleep (60*1000); if (uid> 0) {Notice notice = appContext. getUserNotice (uid); msg. what = 1; msg. obj = notice;} else {msg. what = 0 ;}} catch (AppException e) {e. printStackTrace (); msg. what =-1;} catch (Exception e) {e. printStackTrace (); msg. what =-1;} handler. sendMessage (msg );}}. start ();}
This is a simple and crude message acquisition process.
Sleep(60*1000 );
After one minute of rest, appContext. getUserNotice (uid); uses this function to get the message. After tracking the function code, we know that it actually requests some data with the open-source Chinese server, and then constructs a Notice object and returns it. Then it is inserted into msg. Finally, send the msg through handler. sendMessage (msg. In the handleMessage above, the foreachUserNotice () function is called. This is the final process, where data is collected every minute. A major is round robin.
Then we can see that there is a sendBroadCast function in this handleMessage, that is to say, the notice obtained after the round-robin is finally sent to broadcast and sent out.
(2) Next, let's take a look at the receiving part of the broadcast.
First, a broadcast is registered statically.
<receiver android:name=".ui.BroadCast"> <intent-filter> <action android:name="net.oschina.app.action.APPWIDGET_UPDATE"/> </intent-filter> </receiver>
We can find the ui. BroadCast class. The following describes how to handle the received BroadCast.
@ Override public void onReceive (Contextcontext, Intent intent) {StringACTION_NAME = intent. getAction (); if ("net. oschina. app. action. APPWIDGET_UPDATE ". equals (ACTION_NAME) {int atmeCount = intent. getIntExtra ("atmeCount", 0); // @ I int msgCount = intent. getIntExtra ("msgCount", 0); // message int reviewCount = intent. getIntExtra ("reviewCount", 0); // comment int newFansCount = intent. getIntExtra ("newFansCount", 0); // new fan Int activeCount = atmeCount + reviewCount + msgCount + newFansCount; // Total Information // dynamic-total if (Main. bv_active! = Null) {if (activeCount> 0) {Main. bv_active.setText (activeCount + ""); Main. bv_active.show ();} else {Main. bv_active.setText (""); Main. bv_active.hide () ;}/// @ me if (Main. bv_atme! = Null) {if (atmeCount> 0) {Main. bv_atme.setText (atmeCount + ""); Main. bv_atme.show ();} else {Main. bv_atme.setText (""); Main. bv_atme.hide () ;}// comment if (Main. bv_review! = Null) {if (reviewCount> 0) {Main. bv_review.setText (reviewCount + ""); Main. bv_review.show ();} else {Main. bv_review.setText (""); Main. bv_review.hide () ;}// leave a message if (Main. bv_message! = Null) {if (msgCount> 0) {Main. bv_message.setText (msgCount + ""); Main. bv_message.show ();} else {Main. bv_message.setText (""); Main. bv_message.hide () ;}// display this in the notification bar. notification (context, activeCount );}}
Check the code to find that all the variable types starting with bv, such as bv_active, are BadgeView variables. This class inherits from the textView class. A small red circle on the top of a control contains numbers. That is to say, the number of new messages that prompt you. This is a very common practice. Let's leave it alone.
Here, the content of notice is taken out to update this control. The message process ends here.
ThisThe. notification function is obviously a notification in the notification bar of the mobile phone.
Let's take a look
Private voidnotification (Context context, int noticeCount) {// create icationicationmanager icationmanagernotifmanager = (icationicationmanager) context. getSystemService (Context. NOTIFICATION_SERVICE); StringcontentTitle = "Open Source China"; StringcontentText = "you have" + noticeCount + "latest information"; int _ lastNoticeCount; // determine whether to send the notification message if (noticeCount = 0) {icationicationmanager. cancelAll (); lastNoticeCount = 0; return;} else if (noticeCount = lastNoticeCount) {return;} else {_ lastNoticeCount = lastNoticeCount; lastNoticeCount = noticeCount ;} // create Notification Notificationnotification = null; if (noticeCount> _ lastNoticeCount) {StringnoticeTitle = "you have" + (noticeCount-_ lastNoticeCount) + "latest information "; notification = newNotification (R. drawable. icon, noticeTitle, System. currentTimeMillis ();} else {notification = newNotification ();} // set to jump to Intentintent = newIntent (context, Main. class); intent. putExtra ("NOTICE", true); intent. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP | Intent. FLAG_ACTIVITY_NEW_TASK); PendingIntentcontentIntent = PendingIntent. getActivity (context, 0, intent, PendingIntent. FLAG_UPDATE_CURRENT); // sets the latest information notification. setLatestEventInfo (context, contentTitle, contentText, contentIntent); // set the click to clear notification. flags = Notification. FLAG_AUTO_CANCEL; if (noticeCount> _ lastNoticeCount) {// set notification mode. defaults | = Notification. DEFAULT_LIGHTS; // set the notification sound-if (AppContext) context. getApplicationContext ()). isAppSound () notification. sound = Uri. parse ("android. resource: // "+ context. getPackageName () + "/" + R. raw. notificationsound); // set the vibration <android requires the user permission. permission. VIBRATE> // notification. vibrate = new long [] {100,250,100,500};} // sends a notification to icationicationmanager. Y (ication_id _id, notification );}
The specifics are not analyzed. It is still very simple.
Android source code
Use Git to obtain the Android source code:
Android.googlesource.com/#/manifest.git
How to obtain the Android source code:
Mkdir/usr/local/android-mirror cd/usr/local/android-mirror repo init -- mirror-u android.googlesource.com/#/manifest repo sync mkdir/usr/local/android-2.3.7 cd/usr/ local/android-2.3.7 repo init-u/usr/local/mirror/platform/manifest. git-B android-2.3.7_r1 repo sync repo init-u android.googlesource.com/platform/manifest repo sync
The android system is open-source, and the system code can be seen. Will the source code of the software be seen?
Product 2 on the first floor
On the third floor of the second floor, the system software is also open-source, but third-party software may not be open-source.