Android通知Notification

点击 发送通知 按钮,则发送通知到设备的通知栏。点击 清除通知 则清除通知栏上的消息通知。

package com.example.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 int NOTIFICATION_ID = 100;
	private int requestCode = 0000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button send = (Button) findViewById(R.id.button1);

		send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				sendmessge();

			}
		});
		Button cleanr = (Button) findViewById(R.id.button2);
		cleanr.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				cleanr();

			}
		});
	}

	public void sendmessge() {
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(this);

		mbuilder.setSmallIcon(R.drawable.ic_launcher);
		// mbuilder.setContentTitle("你有新消息了");
		// mbuilder.setContentText("成都爆炸事件据官方消息调查通知");

		Notification notifocation = mbuilder.build();

		// 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局
		RemoteViews rv = new RemoteViews(this.getPackageName(), R.layout.notification);
		rv.setImageViewResource(R.id.imageview, R.drawable.ic_launcher);
		rv.setTextViewText(R.id.textview1, "腾讯消息讯。。");
		rv.setTextViewText(R.id.textview2, "成都爆炸音又来新消息了");
		notifocation.contentView = rv;

		// 缺省设置为当发送通知到通知栏时候:提示声音 + 手机震动
		notifocation.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
		// 通知的时间
		notifocation.when = System.currentTimeMillis();
		
		
		// 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
		Intent intent = new Intent(this, MainActivity.class);

		// 当用户点击通知栏的Notification时候,切换回MainActivity。
		PendingIntent pi = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		notifocation.contentIntent = pi;
		// 发送到手机的通知栏
		notificationManager.notify(NOTIFICATION_ID, notifocation);
	}

	public void cleanr() {
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notificationManager.cancel(NOTIFICATION_ID);
	}

}
需要的activity_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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.notification.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="34dp"
        android:text="发送通知" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="83dp"
        android:text="清除消息" />

</RelativeLayout>


还有一个布局

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
    
	<ImageView 
	    android:id="@+id/imageview"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    />
	<TextView 
	    android:id="@+id/textview1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    />
	<TextView 
	    android:id="@+id/textview2"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"/>
	 
</LinearLayout>

我的第二个布局写的不是很好。信息显示不全。自己可以拿来更改一下你的布局  用相对布局方便一点儿。线性布局显示不全信息。暂时还没有修改

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值