欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Notification消息通知 自定义消息通知内容布局

程序员文章站 2023-12-19 19:24:34
具体操作:自定义消息通知内容布局;点击界面中心的“点击发送消息”textview控件,模拟发送通知消息,通知栏接收消息,点击几次则发送几次,点击通知栏消息,跳转到详情界面。...

具体操作:自定义消息通知内容布局;点击界面中心的“点击发送消息”textview控件,模拟发送通知消息,通知栏接收消息,点击几次则发送几次,点击通知栏消息,跳转到详情界面。

1.activity_main.xml:

<linearlayout 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"
  android:gravity="center"
  tools:context=".mainactivity">

  <textview
    android:id="@+id/tv_show"
    android:text="点击发送消息"
    android:textsize="24sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</linearlayout>

2.layout_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <imageview
    android:id="@+id/imageview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignparentleft="true"
    android:layout_alignparenttop="true"
    android:src="@mipmap/ic_launcher" />

  <textview
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginleft="18dp"
    android:layout_torightof="@+id/imageview1"
    android:text="textview" />

  <textview
    android:id="@+id/textview2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignleft="@+id/textview1"
    android:layout_below="@+id/textview1"
    android:layout_margintop="15dp"
    android:text="textview" />

  <textview
    android:id="@+id/textview3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textview1"
    android:layout_marginleft="54dp"
    android:layout_torightof="@+id/textview2"
    android:text="textview" />

  <textview
    android:id="@+id/textview4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignparentright="true"
    android:layout_below="@+id/textview1"
    android:text="textview" />

</relativelayout>

3.activity_detail.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">


  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="new text"
    android:id="@+id/name"
    android:textsize="18sp"
    android:layout_gravity="center_horizontal"/>

  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="new text"
    android:id="@+id/price"
    android:textsize="18sp"
    android:layout_gravity="center_horizontal"/>

  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="new text"
    android:id="@+id/detail"
    android:textsize="18sp"
    android:layout_gravity="center_horizontal"/>
</linearlayout>

4.singletonf类:

/**
 * 用于存储消息数目
 * created by admin on 2017/9/4.
 */

public class singletonf{

  private int info_number=0;

  private static class singletonholder {
    /**
     * 单例对象实例
     */
    static final singletonf instance = new singletonf();
  }

  public static singletonf getinstance() {
    return singletonholder.instance;
  }

  /**
  * private的构造函数用于避免外界直接使用new来实例化对象
  */
  private singletonf() {}

  public int getinfo_number() {
    return info_number;
  }

  public void setinfo_number(int info_number) {
    this.info_number = info_number;
  }

//  /**
//   * 若singletonf implements serializable则必须实现readresolve方法
//   * readresolve方法应对单例对象被序列化时候
//   */
//  private object readresolve() {
//    return getinstance();
//  }

}

5.mybroadcastreceiver类:

import java.text.simpledateformat;
import java.util.date;
import android.app.notification;
import android.app.notificationmanager;
import android.app.pendingintent;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.os.handler;
import android.os.message;
import android.support.v4.app.notificationcompat;
import android.util.log;
import android.widget.remoteviews;

public class mybroadcastreceiver extends broadcastreceiver {

  private singletonf singletonf;
  private context context1;

  @override
  public void onreceive(context context, intent intent) {
    this.context1 = context;
//   if (intent.getstringextra("info").equals("广播发送了")) {
//     log.i("静态广播:", "广播我已经接受了");
//   }
    message message = handler.obtainmessage();
    message.what = 0;
    handler.sendmessage(message);
  }

  private handler handler = new handler() {
    @override
    public void handlemessage(message msg) {
      switch (msg.what) {
        case 0:
          singletonf = singletonf.getinstance();
          int first = singletonf.getinfo_number();
          first++;
          date nowtime = new date(system.currenttimemillis());
          simpledateformat sdformatter = new simpledateformat("hh:mm");
          string retstrformatnowdate = sdformatter.format(nowtime);
          singletonf.setinfo_number( first);

          remoteviews contentviews = new remoteviews(context1.getpackagename(), r.layout.layout_item);
          // 通过控件的id设置属性
          contentviews.setimageviewresource(r.id.imageview1, r.mipmap.ic_launcher_round);
          contentviews.settextviewtext(r.id.textview1, "消息标题");
          contentviews.settextviewtext(r.id.textview2, "消息内容");
          contentviews.settextviewtext(r.id.textview3, " (" + first + "条新消息)");
          contentviews.settextviewtext(r.id.textview4, "" + retstrformatnowdate);
          // 点击通知栏跳转的activity
          intent intent = new intent(context1, actdetail.class);
          pendingintent pendingintent = pendingintent.getactivity(context1, 0, intent,
              pendingintent.flag_cancel_current);

          notificationcompat.builder mbuilder = new notificationcompat.builder(context1)
              .setsmallicon(r.mipmap.ic_launcher_round).setticker("new message");
          // 自动管理通知栏消息
          mbuilder.setautocancel(true);
          mbuilder.setcontentintent(pendingintent);
          /// 自定义布局
          mbuilder.setcontent(contentviews);
          // 使用默认提示音
          mbuilder.setdefaults(notification.default_all);
          notificationmanager mnotificationmanager = (notificationmanager) context1
              .getsystemservice(context1.notification_service);
          // notify(int id, notification notification)若id为同一个值,则通知栏只会显示一行,并不停更新此消息内容
          // 若为类似uuid.randomuuid().hashcode()这样不同的唯一标识符,则有几条消息通知栏就显示几行
          mnotificationmanager.notify(1, mbuilder.build());

          break;
        default:
          break;
      }
    }
  };

}

6.mainactivity类:

import android.content.intent;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.textview;

public class mainactivity extends appcompatactivity {

  private textview tv_show;
  private int btn_number = 0;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    tv_show = (textview) this.findviewbyid( r.id.tv_show);
    tv_show.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view view) {
        shownotify();
        btn_number++;
        tv_show.settext("发送了" + btn_number + "次广播");
      }
    });
  }

  private void shownotify() {
    intent intent = new intent();
    intent.setaction("massage_notification");
//    intent.putextra("info", "广播发送了");
    sendbroadcast(intent);
  }

  @override
  protected void onstop() {
    btn_number = 0;
    tv_show.settext("发送了" + btn_number + "次广播");
    super.onstop();
  }

  @override
  protected void onpause() {
    btn_number = 0;
    tv_show.settext("发送了" + btn_number + "次广播");
    super.onpause();
  }

  @override
  protected void ondestroy() {
    btn_number = 0;
    super.ondestroy();
  }

}

7.actdetail类:

import android.app.notificationmanager;
import android.content.context;
import android.os.bundle;
import android.support.annotation.nullable;
import android.support.v7.app.appcompatactivity;

/**
 * created by admin on 2017/9/4.
 */

public class actdetail extends appcompatactivity{

  notificationmanager notificationmanager;
  singletonf singletonf;

  @override
  protected void oncreate(@nullable bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_detail);

    singletonf = singletonf.getinstance();
    notificationmanager = (notificationmanager) getsystemservice(context.notification_service);
    notificationmanager.cancel(1);
    singletonf.setinfo_number(0);
  }
}

没找到原创作者,所以也不知道来自何处,只有等以后知道原创作者后再补上来源网址!!!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: