状态栏提醒Notification,NotificationManager

今天学习android一下,分享一下代码:
一个Activity或者Service可以初始化一个状态通知。然而,因为一个activity只有运行在前台并且它所在的窗口获得焦点的时候,它才能执行动作,所以


你通常需要用service来创建通知。当用户正在使用其他应用或者设备待机时,通知可以由后台创建。为了创建通知,你必须用到两个类:Notification和


NotificationManager。
使用Notification类的一个实例去定义状态通知的属性,如图标,通知信息和一些其他的设定,如播放的声音。NotificationManager是系统服务,它来执


行和管理所有的状态通知。你不需要直接初始化NotificationManager。为了把你的通知给它,你必须使用getSystemService()检索到指向


NotificationManager的引用,然后,当你要通知用户的时候,使用android.app.Notification) notify()把你的Notification传给它。
按如下方式创建状态通知:
1. 得到指向NotificationManager的引用:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
2. 初始化Notification:
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
3. 定义通知的信息和PendingIntent:
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
4. 把Notification传递给NotificationManager:
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
上面是简单说一下基础。详细可以查看API:http://developer.android.com/guide/topics/ui/notifiers/notifications.html       
代码:JAVA:
package com.example.test;
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.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Context mcontext;
private Button showButton,cancelButton;
private Notification mnotification;
private NotificationManager mnotificationmanager;
private final static int NOTIFICATION_ID = 0x0001;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==showButton){
Intent intent = new Intent(mcontext,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendIntent = PendingIntent.getActivity(mcontext, 0, intent, 0);
 //这里必需要用setLatestEventInfo(上下文,标题,内容,PendingIntent)不然会报错.
mnotification.setLatestEventInfo(mcontext, "10086", "您的当前话费不足,请充值.哈哈~常联系哦!", pendIntent);
//这里发送通知(消息ID,通知对象)
mnotificationmanager.notify(NOTIFICATION_ID, mnotification);
}else{
//取消信息栏提示(根据ID)
mnotificationmanager.cancel(NOTIFICATION_ID);
}
}
//初始化对象
@SuppressWarnings("deprecation")
private void init(){
mcontext = MainActivity.this;
showButton =(Button)findViewById(R.id.showbn);
cancelButton =(Button)findViewById(R.id.cancelbn);
mnotification = new Notification(R.drawable.ic_launcher,"This is a notification",System.currentTimeMillis());
mnotificationmanager = (NotificationManager) getSystemService(mcontext.NOTIFICATION_SERVICE);
}
}
xml:
<?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" >
    <TextView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
        />
    <Button 
        android:id="@+id/showbn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="showNotification"
        />
    
    <Button 
        android:id="@+id/cancelbn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cancelNotification"
        />
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值