点击 发送通知 按钮,则发送通知到设备的通知栏。点击 清除通知 则清除通知栏上的消息通知。
package com.example.notification;
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.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
public class MainActivity extends Activity {
private int NOTIFICATION_ID = 100;
private int requestCode = 0000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button) findViewById(R.id.button1);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
sendmessge();
}
});
Button cleanr = (Button) findViewById(R.id.button2);
cleanr.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
cleanr();
}
});
}
public void sendmessge() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(this);
mbuilder.setSmallIcon(R.drawable.ic_launcher);
// mbuilder.setContentTitle("你有新消息了");
// mbuilder.setContentText("成都爆炸事件据官方消息调查通知");
Notification notifocation = mbuilder.build();
// 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局
RemoteViews rv = new RemoteViews(this.getPackageName(), R.layout.notification);
rv.setImageViewResource(R.id.imageview, R.drawable.ic_launcher);
rv.setTextViewText(R.id.textview1, "腾讯消息讯。。");
rv.setTextViewText(R.id.textview2, "成都爆炸音又来新消息了");
notifocation.contentView = rv;
// 缺省设置为当发送通知到通知栏时候:提示声音 + 手机震动
notifocation.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
// 通知的时间
notifocation.when = System.currentTimeMillis();
// 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
Intent intent = new Intent(this, MainActivity.class);
// 当用户点击通知栏的Notification时候,切换回MainActivity。
PendingIntent pi = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notifocation.contentIntent = pi;
// 发送到手机的通知栏
notificationManager.notify(NOTIFICATION_ID, notifocation);
}
public void cleanr() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}
}
需要的activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.notification.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_marginTop="34dp"
android:text="发送通知" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="83dp"
android:text="清除消息" />
</RelativeLayout>
<?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" >
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
我的第二个布局写的不是很好。信息显示不全。自己可以拿来更改一下你的布局 用相对布局方便一点儿。线性布局显示不全信息。暂时还没有修改