自定义Notification(一)

本文详细介绍了如何在Android应用中创建通知,包括通过不同组件创建通知的方法,并提供了具体的代码示例。此外,还讲解了如何设置通知的声音、震动及闪光效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通知QwQ

可以在活动中创建 也可以在广播接收器中创建 还可以在服务中创建


以下是以在活动中创建为例(其他方法相同)

①需要一个NotificationManager来对通知进行管理 可以调用Content的getSystemService()获取

②使用Builder构造器创建Notification对象


下面看代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnSend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSend = (Button) findViewById(R.id.btn_send);
        btnSend.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_send:
                Intent intent  = new Intent(getApplicationContext(),NotificationActivity.class);
                PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);


                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(this).setContentTitle("This is content title")//指定通知的标题
                        .setContentText("This is content text")//指定通知的内容
                        .setWhen(System.currentTimeMillis())//指定呗创建的时间 以毫秒为单位
                        .setSmallIcon(R.mipmap.ic_launcher)//小图标
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))//大图标
                        .setContentIntent(pi)//点击
                        .setAutoCancel(true)//点击通知后 通知自动取消
                        .setVibrate(new long[]{0,1000,1000,1000})//震动
                        .build();
                manager.notify(1,notification);//将通知显示出来 参数一:id 参数二:Notification对象
                break;
        }
    }
}

补充:

1.默认声音

    notification.defaults |= Notification.DEFAULT_SOUND; 
    如果要使用自定义声音,那么就要用到sound了。如下: 
    soundnotification.sound=Uri.parse("file:///sdcard/notification/apple.mp3"); 
    需要注意一点,如果default、sound同时出现,那么sound无效,会使用默认铃声。 


2.震动

    如果是使用默认的振动方式,那么同样也是使用default。 
    notification.defaults|=Notification.DEFAULT_VIBRATE; 
   自己定义振动形式,这边需要用到Long型数组
   long[]vibrate={0,100,200,300}; 
   notification.vibrate=vibrate; 
   这边的Long型数组中,第一个参数是开始振动前等待的时间,第二个参数是第一次振动的时间,第三个参数是第二     次振动的时间,以此类推,随便定义多长的数组。但是采用这种方法,没有办法做到重复振动。 
   同样,如果default、vibrate同时出现时,会采用默认形式。

  【说明】:加入手机震动,一定要在manifest.xml中加入权限:
    <uses-permission Android:name="android.permission.VIBRATE" />


3.闪光

   使用默认的灯光
   notification.defaults|=Notification.DEFAULT_LIGHTS; 
   自定义闪光
   notification.ledARGB=0xff00ff00; 
   notification.ledOnMS=300; 
   notification.ledOffMS=1000; 
   notification.flags|=Notification.FLAG_SHOW_LIGHTS; 
   其中ledARGB表示灯光颜色、ledOnMS亮持续时间、ledOffMS暗的时间。 
   【说明】:这边的颜色跟设备有关,不是所有的颜色都可以,要看具体设备。
    DEFAULT_ALL    使用所有默认值,比如声音,震动,闪屏等等


4.NotificationManager常用方法介绍:

    public void cancelAll() 移除所有通知(只是针对当前Context下的Notification)
    public  void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
    public  void notify(String tag ,int id, Notification notification) 将通知加入状态栏,标签为tag,标记为id
    public  void notify(int id, Notification notification) 将通知加入状态栏,标记为id



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值