<Notification>的功能与方法

来源:互联网 发布:日线里引用分时数据 编辑:程序博客网 时间:2024/05/29 03:15
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);        //获取系统服务 NotificationManager        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    }    // 发送通知的按钮的点击事件    public void send(View v) {        //创建一个 Intent        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("恭喜你.涨工资啦!转正啦!")  //设置通知内容                        //设置使用系统默认的声音,默认的 LED灯                .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提供的常用方法](http://img.blog.csdn.net/20160604175813682)
0 0