Android状态栏通知Notification使用小结

看了一下Notification的相关内容,总结一下,先看一下android developer guide:

To create a status bar notification:

  1. Get a reference to the NotificationManager:
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  2. Instantiate the Notification:
    int icon = R.drawable.notification_icon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();
    
    Notification notification = new Notification(icon, tickerText, when);
  3. Define the notification's message and PendingIntent:
    Context context = getApplicationContext();
    CharSequence contentTitle = "My notification";
    CharSequence contentText = "Hello World!";
    Intent notificationIntent = new Intent(this, MyClass.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
  4. Pass the Notification to the NotificationManager:
    private static final int HELLO_ID = 1;
    
    mNotificationManager.notify(HELLO_ID, notification);

    That's it. Your user has now been notified.

大体上就是这个步骤

以下是例子:


布局文件非常简单只定义两个Button即可

package com.csdn.sendnotification;

import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.NotificationManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;

public class SendNotificationActivity extends Activity {
    /** Called when the activity is first created. */
    int notificationNumber=0;
	private Button sendButton;
	private Button cancelButton;
    private NotificationManager notiManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sendButton=(Button)findViewById(R.id.sendButton);
        cancelButton=(Button)findViewById(R.id.cancelButton);
        
        notiManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        sendButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				notificationNumber+=1;
				Notification notification = new Notification();  
                notification.icon = R.drawable.icon;  
                notification.tickerText = "texttexttexttexttext";  //如果很长会滚动显示
                notification.when = System.currentTimeMillis();  
                notification.flags=Notification.FLAG_AUTO_CANCEL;  //自动清除Notification,拉下状态栏点击Notification就会清除通知,去掉这句话点击Notification不会消失
                                                                    

                notification.defaults=Notification.DEFAULT_ALL; //震动Notification.DEFAULT_VIBRATE,两者都需在AndroidManifest.xml加入权限:android.permission.VIBRATE
                
            	notification.sound = Uri.parse("file:///sdcard/ringstones/Fire.mp3"); //自定义提示音
            	notification.number=notificationNumber; //如果多次弹出的Notification的ID相同,那么Notification就只会更新而不会再次滚动提醒,会在图标上显示数目

                Intent intent = new Intent(SendNotificationActivity.this, SendNotificationActivity.class);        
                
                PendingIntent pi = PendingIntent.getActivity(SendNotificationActivity.this, 0, intent, 0);  
                notification.setLatestEventInfo(SendNotificationActivity.this, "title:标题","content:内容", pi);  
                notiManager.notify(1234, notification);  //1234为notification的ID 
            }  
        	
        });
        cancelButton.setOnClickListener(new OnClickListener() {  
        	  
            public void onClick(View v) {  
                notiManager.cancel(1234);  //取消通知,另一种及拉下状态栏点击Notification
            }  
        });  
    }
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值