Notification-Builder-NotificationManager

使用Notification发送通知的时候会发现很多的 deprecated  虽然不会有什么问题,作为强迫症还是希望能把这个单词从代码里面去掉;

之前的是这么写的 

public class MyNotificationDemo extends Activity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		NotificationManager notificationManager = 
				(NotificationManager) super.getSystemService(Activity.NOTIFICATION_SERVICE);
		Notification notification = 
				new Notification(R.drawable.pic_m,
						"新消息。", 
						System.currentTimeMillis());
		
		
		PendingIntent contentIntent = 
				PendingIntent.getActivity(this, 0,
				super.getIntent(), 
				PendingIntent.FLAG_UPDATE_CURRENT); 
		
		notification.setLatestEventInfo(this, "你有一条新消息",
				"来自XXXXX", contentIntent);
		notificationManager.notify("新消息", R.drawable.pic_m, notification);
	}
}

 可是会出现一系列的 ,警告,虽然没有报错。

然后这么写就不会了,

 

public class notificationActivity extends Activity
{
    private Button but1 = null;
    private NotificationManager nm = null;
    private PendingIntent contentIntent = null;
    private final int NOTIFICATION_BASE_NUMBER = 110;
    private Builder builder = null;
    private Notification n = null;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menutest);
        but1 = (Button) super.findViewById(R.id.noti_bt);
        but1.setOnClickListener(new OnClickListener()
        {
            public void onClick(View arg0)
            {
                    NotificationManager nm = (NotificationManager) notificationActivity.this
                            .getSystemService(NOTIFICATION_SERVICE);
                    Resources res = notificationActivity.this.getResources();
                    builder = new Notification.Builder(
                            notificationActivity.this);
                    builder.setContentIntent(contentIntent)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setLargeIcon(
                                    BitmapFactory.decodeResource(res,
                                            R.drawable.bt_on))
                            .setTicker("this is bitch!")
                            .setWhen(System.currentTimeMillis())
                            .setAutoCancel(true)
                            .setContentTitle("This is ContentTitle")
                            .setContentText("this is ContentText");
                    n = builder.build();// 获取一个Notification
                    n.defaults = Notification.DEFAULT_SOUND;// 设置为默认的声音
                    nm.notify(NOTIFICATION_BASE_NUMBER, n);// 显示通知
            }
        });
        nm = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(this, DialogTestActivity.class);
        contentIntent = PendingIntent.getActivity(notificationActivity.this, 0,
                notificationIntent, 0);
    }
}


挺好的

 

posted @ 2014-07-31 16:31  Monkey菜苗  阅读(136)  评论(0)    收藏  举报