具体 *** 作:自定义消息通知内容布局;点击界面中心的“点击发送消息”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,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); }}
没找到原创作者,所以也不知道来自何处,只有等以后知道原创作者后再补上来源网址!!!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的Notification消息通知 自定义消息通知内容布局全部内容,希望文章能够帮你解决Notification消息通知 自定义消息通知内容布局所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)