您的位置:首页 > 移动开发 > Android开发

Android 实现任务栏出现类似短信的提示功能

2015-02-02 13:55 148 查看
要实现Android的消息提示,就是提示用户有新的更新或者信息。即使当用户退出了应用程序。也会类似短信提示那种功能。长话短说,开个service,在后台一直跑,进行数据监控,发现有更新就触发消息提示功能。

上代码:

package com.example.notificationdemo;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends Activity {
Button start;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
start.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// showDefaultNotification();
showCustomizeNotification();
}
});
}

// 自定义显示的通知 ,创建RemoteView对象
private void showCustomizeNotification() {

CharSequence title = "信息";
int icon = R.drawable.icon_blue;
long when = System.currentTimeMillis();
Notification noti = new Notification(icon, title, when + 10000);
// noti.flags = Notification.FLAG_INSISTENT;

noti.defaults |= Notification.DEFAULT_SOUND;
noti.defaults |= Notification.DEFAULT_VIBRATE;
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE;

// 1、创建一个自定义的消息布局 view.xml
// 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
RemoteViews remoteView = new RemoteViews(this.getPackageName(),
R.layout.notification);
remoteView.setImageViewResource(R.id.noti_icon, R.drawable.icon_blue);
remoteView.setTextViewText(R.id.noti_content, "出现预警,请查实!");
noti.contentView = remoteView;
// 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)

// 这儿点击后简单启动Settings模块
PendingIntent contentIntent = PendingIntent.getActivity(
MainActivity.this, 0, new Intent("android.settings.SETTINGS"),
0);
noti.contentIntent = contentIntent;

NotificationManager mnotiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mnotiManager.notify(0, noti);

}

// 默认显示的的Notification
private void showDefaultNotification() {
// 定义Notication的各种属性
CharSequence title = "i am new";
int icon = R.drawable.icon_blue_small;
long when = System.currentTimeMillis();
Notification noti = new Notification(icon, title, when + 10000);
noti.flags = Notification.FLAG_INSISTENT;

// 创建一个通知
Notification mNotification = new Notification();

// 设置属性值
mNotification.icon = R.drawable.icon_blue_small;
mNotification.tickerText = "NotificationTest";
mNotification.when = System.currentTimeMillis(); // 立即发生此通知

// 带参数的构造函数,属性值如上
// Notification mNotification = = new
// Notification(R.drawable.icon,"NotificationTest",
// System.currentTimeMillis()));

// 添加声音效果
mNotification.defaults |= Notification.DEFAULT_SOUND;

// 添加震动,后来得知需要添加震动权限 : Virbate Permission
// mNotification.defaults |= Notification.DEFAULT_VIBRATE ;

// 添加状态标志

// FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
// FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉
// FLAG_ONGOING_EVENT 通知放置在正在运行
// FLAG_INSISTENT 通知的音乐效果一直播放
mNotification.flags = Notification.FLAG_INSISTENT;

// 将该通知显示为默认View
PendingIntent contentIntent = PendingIntent.getActivity(
MainActivity.this, 0, new Intent("android.settings.SETTINGS"),
0);
mNotification.setLatestEventInfo(MainActivity.this, "通知类型:默认View",
"一般般哟。。。。", contentIntent);

// 设置setLatestEventInfo方法,如果不设置会App报错异常
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// 注册此通知
// 如果该NOTIFICATION_ID的通知已存在,会显示最新通知的相关信息 ,比如tickerText 等
mNotificationManager.notify(2, mNotification);

}

private void removeNotification() {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 取消的只是当前Context的Notification
mNotificationManager.cancel(2);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}


配套demo:http://download.csdn.net/detail/vaecer/8416543
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: