您的位置:首页 > 其它

一个简单的顶部菜单消息提醒功能

2014-05-20 22:21 211 查看

package com.mynotificationmannger;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

private Button button1,button2,button3,button4;

//声明一个消息管理器
private NotificationManager notificationManager;

private PendingIntent pendingIntent;

//声明一个Intent跳转机制
private Intent intent;

//声明Notification对象
private Notification notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化NotificationManager对象
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

//构造Notification对象
notification = new Notification();

//点击消息时,移动到指定内容页
intent = new Intent(MainActivity.this,Activity2.class);

//主要是设置点击通知时显示内容的类
pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);

button1.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
//显示消息的图标
notification.icon=R.drawable.ico_notification;

//显示消息的内容
notification.tickerText = "Button1消息....";

//信息提醒时的效果
notification.defaults=Notification.DEFAULT_SOUND;

//消息显示的参数
notification.setLatestEventInfo(MainActivity.this, "今日天气特别热", "大家注意比如抗温", pendingIntent);

//执行notification这个通知
notificationManager.notify(0,notification);
}
});

button2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "Button2消息...";
notification.defaults = Notification.FLAG_HIGH_PRIORITY;
notification.setLatestEventInfo(MainActivity.this, "天气很好", "就是有点热", pendingIntent);
notificationManager.notify(0,notification);
}
});

button3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
notification.icon=R.drawable.ico_notification;
notification.tickerText="Button3消息...";
notification.defaults=Notification.FLAG_NO_CLEAR;
notification.setLatestEventInfo(MainActivity.this, "今天周二", "有一天过去", pendingIntent);
notificationManager.notify(0,notification);
}
});

button4.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
notification.icon=R.drawable.ic_launcher;
notification.tickerText="Button4消息...";
notification.defaults=Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(MainActivity.this, "明天周三", "新的一天开始", pendingIntent);
notificationManager.notify(0,notification);
}
});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  移动