自定义Notification添加点击事件

前言

在上一篇文章中《Notification自定义界面》中我们实现了自定义的界面,那么我们该怎么为自定义的界面添加点击事件呢?像酷狗在通知栏 有“上一首”,“下一首”等控制按钮,我们需要对按钮的点击事件进行响应,不过方法和之前的点击设置不一样,需要另外处理,下面我将进行简单的说明。

实现

同样,我们需要一个Service的子类MyService,然后在MyService的onCreate中设置,如下代码:

public class MyService extends Service {

    public static final String ONCLICK = "com.app.onclick";


    private BroadcastReceiver receiver_onclick = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ONCLICK)) {
                Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                vibrator.vibrate(1000);
            }
        }
    };
    @Override
    public void onCreate() {
        super.onCreate();
        Notification notification = new Notification(R.drawable.ic_launcher,
                "JcMan", System.currentTimeMillis());
        RemoteViews view = new RemoteViews(getPackageName(),R.layout.notification);
        notification.contentView = view;
        IntentFilter filter_click = new IntentFilter();
        filter_click.addAction(ONCLICK);
        //注册广播
        registerReceiver(receiver_onclick, filter_click);
        Intent Intent_pre = new Intent(ONCLICK);
        //得到PendingIntent
        PendingIntent pendIntent_click = PendingIntent.getBroadcast(this, 0, Intent_pre, 0);
        //设置监听
        notification.contentView.setOnClickPendingIntent(R.id.btn,pendIntent_click);
        //前台运行
        startForeground(1, notification);
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

可以看到,我们先得到BroadcastReceiver的一个对象,然后在onReceiver里面实现我们的操作,我设置成点击时候手机震动一秒钟,当然不要忘记在配置文件添加震动的权限,不然到时候就会出错了。如果对广播没有了解的,那么可以先去了解一下广播的机制,这里我使用的是动态注册广播的方法,还有另外一种方法来注册,不过我更喜欢动态注册的罢了。

小结

看到在Notification添加一个ProgressBar来实现下载的进度提示,这里需要用到更新Notification界面的知识,虽然和在Activity中更新界面不太一样,但是也不是在复杂,因为我并没有用到这方面的知识,所以这里就不给大家介绍了,有兴趣的可以搜相关的内容。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 APP 的状态栏中添加自定义图标,需要先创建一个 Bitmap 对象,然后将其设置为通知的图标。具体步骤如下: 1. 创建一个 Bitmap 对象,用于表示自定义图标。 ``` Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.custom_icon); ``` 其中 R.drawable.custom_icon 是自定义图标的资源 ID。 2. 创建一个 NotificationCompat.Builder 对象,用于构建通知。 ``` NotificationCompat.Builder builder = new NotificationCompat.Builder(context); ``` 3. 设置通知的标题、内容等信息。 ``` builder.setContentTitle("Title") .setContentText("Notification content") .setAutoCancel(true); ``` 其中 setContentTitle 方法用于设置通知的标题,setContentText 方法用于设置通知的内容,setAutoCancel 方法用于设置点击通知后自动清除通知。 4. 将自定义图标设置为通知的图标。 ``` builder.setLargeIcon(icon) .setSmallIcon(R.drawable.notification_icon); ``` 其中 setLargeIcon 方法用于设置通知的大图标,setSmallIcon 方法用于设置通知的小图标。 5. 创建一个 PendingIntent 对象,用于在用户点击通知时启动一个 Activity。 ``` Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); ``` 其中 MainActivity.class 是需要启动的 Activity 类名。 6. 发送通知。 ``` NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(notificationId, builder.build()); ``` 其中 notificationId 是通知的唯一标识符,用于区分不同的通知。 这样,就可以在 APP 的状态栏中添加一个自定义图标了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值