您的位置:首页 > 其它

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

2015-08-14 16:17 288 查看
关于通知的一些简单的设置和方法介绍

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中的布局
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: