资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android自定义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;
 }
}


新闻标题:Android自定义Notification添加点击事件-创新互联
转载源于:http://cdkjz.cn/article/iccgc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220