package com.crazyit.ui.notificationdemo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
/**
* 通知的功能与简单用法
*/
public class NotificationActivity extends AppCompatActivity {
public static int NOTIFITION_ID = 0x123;
NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
public void send(View v) {
Intent intent = new Intent(NotificationActivity.this, OtherActivity.class);
PendingIntent pi = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);
Notification notification = new Notification.Builder(this)
.setAutoCancel(true)
.setTicker("有新消息")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("一条心通知")
.setContentText("恭喜你.涨工资啦!转正啦!")
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
.setSound(Uri.parse("android.resourse://org.crazy.ui/" + R.raw.msg))
.setWhen(System.currentTimeMillis())
.setContentIntent(pi).build();
manager.notify(NOTIFITION_ID,notification);
}
public void del(View view){
manager.cancel(NOTIFITION_ID);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.crazyit.ui.notificationdemo.NotificationActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:onClick="send"
android:layout_marginStart="36dp"
android:layout_marginTop="125dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:id="@+id/button2"
android:onClick="del"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/button" />
</RelativeLayout>
![NOtification.Bulider提供的常用方法](https://img-blog.csdn.net/20160604175813682)