隨筆-1  評論-68  文章-98  trackbacks-0

          編寫:徐建祥(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,代碼如下:

          1. package org.anymobile.test;  
          2. import android.app.Activity;  
          3. import android.content.ComponentName;  
          4. import android.content.Intent;  
          5. import android.graphics.LightingColorFilter;  
          6. import android.os.Bundle;  
          7. import android.view.Menu;  
          8. import android.view.View;  
          9. import android.view.View.OnClickListener;  
          10. import android.widget.Button;  
          11. public class TestActivity extends Activity  
          12. {  
          13.     private static final int ADD_ID = 0;  
          14.     private static final int DEL_ID = 1;  
          15.       
          16.     /** Called when the activity is first created. */  
          17.     @Override  
          18.     public void onCreate(Bundle savedInstanceState)  
          19.     {  
          20.         super.onCreate(savedInstanceState);  
          21.         setContentView(R.layout.main);  
          22.           
          23.         Button button = (Button) this.findViewById(R.id.btn_menu);  
          24.         button.setOnClickListener(new OnClickListener()  
          25.         {  
          26.             public void onClick(View v)  
          27.             {  
          28. //              TestActivity.this.openOptionsMenu();  
          29.                 String packName = "org.anymobile.im";  
          30.                 String className = packName + ".LoginActivity";  
          31.                   
          32.                 Intent intent = new Intent();  
          33.                 ComponentName componentName = new ComponentName(packName, className);  
          34.                 intent.setComponent(componentName);  
          35.                   
          36.                 intent.setAction("android.intent.action.MAIN");  
          37.                 intent.addCategory("android.intent.category.LAUNCHER");  
          38.                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
          39.                 intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);  
          40.                   
          41.                 TestActivity.this.startActivity(intent);  
          42.             }  
          43.         });  
          44. //        button.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);  
          45.         button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF0xFFAA0000));  
          46.     }  
          47.     @Override  
          48.     public boolean onCreateOptionsMenu(Menu menu)  
          49.     {  
          50.         menu.add(0, ADD_ID, 0"ADD");  
          51.         menu.add(0, DEL_ID, 0"DEL");  
          52.           
          53.         return super.onCreateOptionsMenu(menu);  
          54.     }  
          55.       
          56. }  
           

           

          OK,開始測試狀態欄的通知功能:

          1、LoginActivity.onCreate() 調用showNotification()方法,創建一個通知圖標;

          1. /** 
          2.    * The notification is the icon and associated expanded entry in the 
          3.    * status bar. 
          4.    */  
          5.   protected void showNotification()  
          6.   {  
          7.       CharSequence from = "IM";  
          8.       CharSequence message = "IM start up";  
          9.         
          10. Intent intent = new Intent();  
          11. ComponentName componentName = new ComponentName("com.longcheer.imm",   
          12.     "com.longcheer.imm.activitys.LoginActivity");  
          13. intent.setComponent(componentName);  
          14. intent.setAction("android.intent.action.MAIN");  
          15. intent.addCategory("android.intent.category.LAUNCHER");  
          16. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
          17. intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);  
          18.   
          19.       // The PendingIntent to launch our activity if the user selects this notification  
          20.       PendingIntent contentIntent = PendingIntent.getActivity(this0, intent, 0);  
          21.       // construct the Notification object.  
          22.       Notification notif = new Notification(R.drawable.icon, "IMM Still run background!",  
          23.               System.currentTimeMillis());  
          24.       notif.setLatestEventInfo(this, from, message, contentIntent);  
          25.         
          26.       // look up the notification manager service  
          27.       NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
          28.       nm.notify(R.string.app_name, notif);  
          29.   }  
           

           

          2、在LoginActivity / MainAcitivity的退出操作中cancel該通知。

          1. private void doExit()   
          2. {  
          3.     this.finish();  
          4.     NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
          5.        nm.cancel(R.string.app_name);  
          6. }  
           

           

          測試OK!!

          posted on 2010-12-14 23:08 Xu Jianxiang 閱讀(4715) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 白朗县| 建德市| 武夷山市| 镇康县| 靖远县| 玉林市| 伊川县| 象山县| 宁都县| 泸溪县| 岚皋县| 雷波县| 黔江区| 武定县| 巧家县| 彩票| 彰化县| 靖州| 商洛市| 泾阳县| 南部县| 连云港市| 宁城县| 仁布县| 武冈市| 高安市| 平谷区| 保靖县| 张北县| 秭归县| 临夏县| 武乡县| 长垣县| 洱源县| 离岛区| 喀喇| 高雄县| 伽师县| 城步| 阿城市| 盘山县|