状态栏通知Notification用法

Notification的简单理解:[url]http://blog.csdn.net/manymore13/article/details/6801471[/url]
Android 状态栏通知Notification用法:[url]http://www.pocketdigi.com/20100905/89.html[/url]
Android Notification 传递参数:[url]http://renzhen.iteye.com/blog/1176746[/url]

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pandy.notifi"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />
[color=red]<uses-permission android:name="android.permission.VIBRATE" />[/color]

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".NotificationDemoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示Notification" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除Notification" />

</LinearLayout>



package com.pandy.notifi;

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.widget.Button;

public class NotificationDemoActivity extends Activity {
/** Called when the activity is first created. */
private Button button1, button2;
NotificationManager nm;
int notification_id=19172439;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

button1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Notification nf = new Notification(R.drawable.ic_launcher,"我就是发个通知到状态栏来显示而已.",System.currentTimeMillis());
//后面的参数分别是显示在顶部通知栏的小图标,小图标旁的文字(短暂显示,自动消失)系统当前时间(不明白这个有什么用)
nf.defaults=Notification.DEFAULT_ALL;
//这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND
//振动为Notification.DEFAULT_VIBRATE;
//Light为Notification.DEFAULT_LIGHTS,在我的Milestone上好像没什么反应
//全部为Notification.DEFAULT_ALL
//如果是振动或者全部,必须在AndroidManifest.xml加入振动权限
PendingIntent pt=PendingIntent.getActivity(NotificationDemoActivity.this, 0, new Intent(NotificationDemoActivity.this,NotificationDemoActivity.class), 0);
//点击通知后的动作,这里是转回main 这个Acticity
//往下拖动后,看见列表里面的内容
nf.setLatestEventInfo(NotificationDemoActivity.this,"往下拖动后的列表标题","点击查看内容",pt);
nm.notify(notification_id, nf);


}
});
button2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//取消状态栏的通知
nm.cancel(notification_id);

}
});

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值