android 自定义通知布局Notification,点击Notification导航切换回原Activity


一、java代码

package com.hxzy.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends Activity {

	private final int NOTIFICATION_ID = 105;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button send = (Button) findViewById(R.id.sendNotify);
		send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sendNotification();
			}

		});

		Button clean = (Button) findViewById(R.id.cleanNotify);
		clean.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				cleanNotification();
			}
		});

	}

	private void sendNotification() {
		NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
		mBuilder.setSmallIcon(R.drawable.ic_launcher);
		// mBuilder.setContentText("通知的内容");
		// mBuilder.setContentTitle("通知的标题");

		Notification notification = mBuilder.build();

		// 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局
		RemoteViews rv = new RemoteViews(this.getPackageName(), R.layout.notification);
		rv.setImageViewResource(R.id.image, R.drawable.ic_launcher);
		rv.setTextViewText(R.id.title, "标题");
		rv.setTextViewText(R.id.text, "内容");
		notification.contentView = rv;
		// 发送通知到通知栏时:提示声音 + 手机震动 + 点亮Android手机呼吸灯。
		// 注意!!(提示声音 + 手机震动)这两项基本上Android手机均支持。
		// 但Android呼吸灯能否点亮则取决于各个手机硬件制造商自家的设置。
		notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;

		// 通知的时间
		notification.when = System.currentTimeMillis();

		// 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
		// new Intent()中第二个参数代表我们将切换到哪里,比如点击微信通知切换到微信,这里我们切换到MainActivity.
		Intent intent = new Intent(this, MainActivity.class);
		// 当用户点击通知栏的Notification时候,切换回MainActivity。
		PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		notification.contentIntent = pi;

		// 发送到手机的通知栏
		nManager.notify(NOTIFICATION_ID, notification);
	}

	private void cleanNotification() {
		NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		nManager.cancel(NOTIFICATION_ID);
	}
}


二、AndroidManifest.xml

需要注意的是,默认Android的Activity为标准模式,即每次都new一个新的Activity出来,不是原先的Activity,在本例中,可以观察到MainActivity中的onCreate()如果不修改启动模式,则每次本调用每次TextView显示的时间不同(递增),所有为了使用原来的Activity、避免重复new一个新的出来,需要:

在AndroidManifest.xml中修改MainActivity启动模式为:singleTop

 android:launchMode="singleTop"  


三、layout布局文件

1、activit_main.xml

<RelativeLayout 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"
    tools:context="com.hxzy.notification.MainActivity" >

   <Button
       android:id="@+id/cleanNotify"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:text="clean" />

   <Button
       android:id="@+id/sendNotify"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_above="@+id/cleanNotify"
       android:layout_alignLeft="@+id/cleanNotify"
       android:layout_marginBottom="22dp"
       android:text="send" />

</RelativeLayout>


2、notification.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/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="30dp" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_toRightOf="@id/image" />

</RelativeLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值