实现效果如图所示:
MainActivity.java文件
package com.yongninggo.helloworld; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button send; private Button cancle; NotificationManager manager; int notification_id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); send = (Button) findViewById(R.id.send); cancle = (Button) findViewById(R.id.cancle); manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Notification.Builder builder = new Notification.Builder(MainActivity.this); builder.setSmallIcon(R.drawable.image1); builder.setTicker("World"); builder.setWhen(System.currentTimeMillis()); builder.setContentTitle("标题栏"); builder.setContentText("这个是显示出来的内容部分"); Intent intent = new Intent(MainActivity.this, Activity.class); PendingIntent ma = PendingIntent.getActivity(MainActivity.this,0,intent,0); builder.setContentIntent(ma);//设置点击过后跳转的activity /*builder.setDefaults(Notification.DEFAULT_SOUND);//设置声音 builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯 builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动*/ builder.setDefaults(Notification.DEFAULT_ALL);//设置全部 Notification notification = builder.build();//4.1以上用.build(); notification.flags |= Notification.FLAG_AUTO_CANCEL;// 点击通知的时候cancel掉 manager.notify(notification_id,notification); } }); cancle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { manager.cancel(notification_id); } }); } }
activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.yongninggo.helloworld.MainActivity" android:gravity="center_horizontal" android:layout_marginTop="10dip"> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送通知"/> <Button android:id="@+id/cancle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消通知" android:layout_marginLeft="10dip"/> </LinearLayout>
提示音,闪光灯,震动效果需要添加权限:
<uses-permission android:name="android.permission.VIBRATE"> </uses-permission>
图标不知道什么原因,尝试了各种方法 反正就是个机器人,我的是红米3 MIUI Android5.1;(莫名的心酸呐~)