android清除通知欄訊息

來源:互聯網
上載者:User

這近項目快到結尾了,經理要我處理一個問題,就是我們程式關閉後,程式發出通知 在狀態列上始終沒有消除,需要手動的清楚,
體驗效果極其不好,現在是想在程式推出後,把通知給消除了,琢磨了下,不知怎麼清楚,看了下api 有清除的方法,後面安心多了
,但有出現毛病了,我什麼調用通知管理器把通知消除啊,他是開一個一個服務中的,我們不能new 這個類,是系統的,當時想了下
決定發送廣播清楚, 當程式退出的時候,調用該廣播把訊息清楚,等到快寫完的時候,才發現,既然是系統調用的,系統肯定有結束的回調啊
立馬想到了 ondesory()方法,因為我程式不管怎麼退出,都會調用該方法,而且省了我很大的一筆功夫,代碼也就一行!
 
寫這個只想告訴自己,在應用系統的東西時候,我們應該遵循系統的規則進行遊戲,該建立的時候就要建立,該釋放的地方就要釋放!
看來對系統的生命週期認識還不是很到位啊!
  說了這麼多廢話 ,也貼上測試代碼 ,隨便寫的

package com.liao.notification; 
 
import java.util.ArrayList; 
 
import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Base64; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
 
public class MainActivity extends Activity { 
    private int type ; 
    private String message ; 
    private NotificationManager notiManager; 
     
    Handler handler = new Handler(){ 
         
        public void handleMessage(Message msg) { 
            // TODO Auto-generated method stub 
            if(msg.what ==1){ 
                message = "type ====1"; 
                 
            }else if (msg.what == 2){ 
                message = "type ====2"; 
            } 
            notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
            Notification notification = new Notification(R.drawable.icon, 
                    "您有新訊息" + type, System.currentTimeMillis()); 
            Intent intent = new Intent(); 
            intent.setClass(getApplicationContext(), ReciveActivity.class); 
            intent.putExtra("hello", "hello Dialog"); 
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
                    | Intent.FLAG_ACTIVITY_NEW_TASK); 
 
            PendingIntent pendingIntent = PendingIntent.getActivity( 
                    getApplicationContext(), 100, intent, 
                    PendingIntent.FLAG_UPDATE_CURRENT); 
            notification.setLatestEventInfo(getApplicationContext(), type + "", 
                    message, pendingIntent); 
            notiManager.notify(0, notification); 
             
        } 
    }; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
         
        LinearLayout layout = new LinearLayout(this); 
 
        Button button = new Button(this); 
        button.setText("測試notification1"); 
        button.setOnClickListener(new OnClickListener() { 
 
            public void onClick(View v) { 
                type = 1; 
                handler.sendEmptyMessage(1); 
            } 
        }); 
        Button button2 = new Button(this); 
        button2.setText("測試notification2"); 
        button2.setOnClickListener(new OnClickListener() { 
 
            public void onClick(View v) { 
                //              handler.sendEmptyMessage(2); 
                //              type= 2; 
                notiManager.cancel(0);// 調用他就行 這個0 和notiManager建立時候傳入的0 是唯一的 ,所以可以根據他刪除 
 
            } 
        }); 
        layout.addView(button); 
        layout.addView(button2); 
        setContentView(layout); 
         
    } 
} 


摘自 android小益的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.