Notification的一些简单的设置和简单的用法

关于通知的一些简单的设置和方法介绍
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
/**
*

  • @项目名:NotificationTest_demo1
  • @类名称:MainActivity
  • @类描述:通知基本方法
  • @创建人:HXF
  • @修改人:
  • @创建时间:2015-8-13 上午10:20:04
  • @version

*/
public class MainActivity extends Activity implements OnClickListener {

private Button sendNotice;
private NotificationManager manager;

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

	sendNotice = (Button) findViewById(R.id.send_notice);
	sendNotice.setOnClickListener(this);
}

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
	switch (v.getId()) {
	case R.id.send_notice:
		manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notification = new Notification(
				R.drawable.ic_launcher, "这是Notification通知",
				System.currentTimeMillis());
		
		/** 实现通知能被点击并跳转界面 */
		Intent intent = new Intent(this,NotificationActivity.class);
		PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		long[] vibrate = {0,1000,1000,1000};
		/** 发送通知的让手机震动的时间设置 */
		notification.vibrate = vibrate;
		
		/** 可以设置通知来的时候有LED提示灯显示亮度 */
		notification.ledARGB = Color.GREEN;
		notification.ledOnMS = 1000;
		notification.ledOffMS = 1000;
		notification.flags = Notification.FLAG_SHOW_LIGHTS;
		
		/** 一行搞定所有以上繁琐工作(设置声音,设置震动,设置LED光亮) */
		notification.defaults = Notification.DEFAULT_ALL;
		
		
		
		notification.setLatestEventInfo(this, "这是Notification通知标题",
				"这是Notification通知内容", pi);
		
		/** 1代表id,可供后面取消的时候使用 */
		manager.notify(1, notification);
		
		break;

	default:
		break;
	}
}

}

实现点击通知的界面的跳转,并且取消通知栏中的通知。
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;

public class NotificationActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	
	setContentView(R.layout.notification_layout);
	
	/** 这边取消通知栏的通知,实在被跳转的界面*/
	NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	/** 1代表id,这里传入1是要取消MainActivity方法中的发出的那个通知 */
	manager.cancel(1);
	
}

}

MainActivity中的布局

<Button
    android:id="@+id/send_notice"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="#6BCBDF"
    android:text="Send notice" />

Notification中的布局都贴出来,希望能帮助初学者:

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="这里是Notification布局"
    android:textSize="24sp" />
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DT从零到壹

您的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值