//activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/send_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送"
/>
<Button
android:id="@+id/cancle_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"
/>
</LinearLayout>
//MainActivity.java
package com.example.test_notification;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog.Builder;
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;
@SuppressLint("NewApi") public class MainActivity extends Activity implements OnClickListener{
private Button send_bt;
private Button cancle_bt;
private NotificationManager manager;//通知控制类
private int notificationId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
send_bt=(Button) findViewById(R.id.send_bt);
cancle_bt=(Button) findViewById(R.id.cancle_bt);
send_bt.setOnClickListener(this);
cancle_bt.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.send_bt:
sendNotification();
break;
case R.id.cancle_bt:
manager.cancel(notificationId);
break;
default:
break;
}
}
/**
* 构造notification并发送到通知栏
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi")
private void sendNotification(){
Intent intent=new Intent(this,MainActivity.class);
PendingIntent pIntent=PendingIntent.getActivity(this, 0, intent, 0);
android.app.Notification.Builder builder=new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);//设置图标
builder.setTicker("hello"); //手机状态栏的提示
builder.setWhen(System.currentTimeMillis()); //设置时间
builder.setContentTitle("通知栏通知");
builder.setContentText("我来自NotificationDemo."); //设置通知内容
builder.setContentIntent(pIntent); //设置点击通知栏后的意图
// builder.setDefaults(Notification.DEFAULT_LIGHTS); //设置提示灯光
// builder.setDefaults(Notification.DEFAULT_SOUND); //设置提示声音
// builder.setDefaults(Notification.DEFAULT_VIBRATE); //设置振动
builder.setDefaults(Notification.DEFAULT_ALL); //设置提示灯光、声音及振动
//需要在AndroidManifest文件中添加灯光及振动权限
//<uses-permission android:name="android.permission.FLASHLIGHT"/>
//<uses-permission android:name="android.permission.VIBRATE"/>
Notification notification=builder.build(); //开发环境4.1及以上
// builder.getNotification();//开发环境4.1以下
manager.notify(notificationId, notification);
}
}
//需要在AndroidManifest文件中添加灯光及振动权限
<uses-permission android:name="android.permission.FLASHLIGHT"/> <uses-permission android:name="android.permission.VIBRATE"/>
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_notification"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test_notification.MainActivity"
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>