編寫:徐建祥(netpirate@gmail.com)
日期:2010/11/22
網址:http://www.anymobile.org
QQ的狀態列通知機制:當所有QQ的UI Activity切換到後台後,添加狀態通知;切換回來後,刪除該狀態通知。
飛信的狀態列通知方式:運行軟體後,表徵圖一直顯示在狀態列的通知欄中;顯示退出軟體則刪除該狀態通知。
似乎QQ的更有點技術含量,多個程式切換到背景處理而已;以飛信的模式,做個類似的測試,案例如下:
程式路徑:org.anymobile.im
程式入口:org.anymobile.im.LoginActivity(Action:android.intent.action.MAIN;Category:android.intent.category.LAUNCHER)
主介面程式:org.anymobile.im.MainActivity
測試程式流程:未登入的情況下,或者第一次運行會開啟LoginActivity程式;登陸後,一直停留在主介面MainActivity。
所以,通過Notification,需可以回到im項目的上一個介面程式,LoginActivity / MainActivity,這裡就要參考Launcher應用的相關實現,Intent的flag設定。
測試代碼,建立一個android項目,TestNotification,入口程式:TestActivity,代碼如下:
package org.anymobile.test;<br />import android.app.Activity;<br />import android.content.ComponentName;<br />import android.content.Intent;<br />import android.graphics.LightingColorFilter;<br />import android.os.Bundle;<br />import android.view.Menu;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />public class TestActivity extends Activity<br />{<br />private static final int ADD_ID = 0;<br />private static final int DEL_ID = 1;</p><p> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState)<br /> {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> Button button = (Button) this.findViewById(R.id.btn_menu);<br /> button.setOnClickListener(new OnClickListener()<br /> {<br /> public void onClick(View v)<br /> {<br />// TestActivity.this.openOptionsMenu();<br /> String packName = "org.anymobile.im";<br /> String className = packName + ".LoginActivity";</p><p> Intent intent = new Intent();<br /> ComponentName componentName = new ComponentName(packName, className);<br /> intent.setComponent(componentName);</p><p> intent.setAction("android.intent.action.MAIN");<br /> intent.addCategory("android.intent.category.LAUNCHER");<br /> intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br /> intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);</p><p> TestActivity.this.startActivity(intent);<br /> }<br /> });<br />// button.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);<br /> button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));<br /> }<br />@Override<br />public boolean onCreateOptionsMenu(Menu menu)<br />{<br />menu.add(0, ADD_ID, 0, "ADD");<br />menu.add(0, DEL_ID, 0, "DEL");</p><p>return super.onCreateOptionsMenu(menu);<br />}</p><p>}
OK,開始測試狀態列的通知功能:
1、LoginActivity.onCreate() 調用showNotification()方法,建立一個通知表徵圖;
/**<br /> * The notification is the icon and associated expanded entry in the<br /> * status bar.<br /> */<br /> protected void showNotification()<br /> {<br /> CharSequence from = "IM";<br /> CharSequence message = "IM start up";</p><p>Intent intent = new Intent();<br />ComponentName componentName = new ComponentName("com.longcheer.imm",<br />"com.longcheer.imm.activitys.LoginActivity");<br />intent.setComponent(componentName);<br />intent.setAction("android.intent.action.MAIN");<br />intent.addCategory("android.intent.category.LAUNCHER");<br />intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br />intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);</p><p> // The PendingIntent to launch our activity if the user selects this notification<br /> PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);<br /> // construct the Notification object.<br /> Notification notif = new Notification(R.drawable.icon, "IMM Still run background!",<br /> System.currentTimeMillis());<br /> notif.setLatestEventInfo(this, from, message, contentIntent);</p><p> // look up the notification manager service<br /> NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);<br /> nm.notify(R.string.app_name, notif);<br /> }
2、在LoginActivity / MainAcitivity的退出操作中cancel該通知。
private void doExit()<br />{<br /> this.finish();<br /> NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);<br /> nm.cancel(R.string.app_name);<br />}
測試OK!!