代码:
package com.my;
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.provider.ContactsContract.CommonDataKinds.Note;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.RemoteViews.RemoteView;
import android.widget.TextView;
public class NotificationActivity extends Activity implements OnClickListener {
private Button btnNotify_all;
private Button btnNotify_lights;
private Button btnNotify_sound;
private Button btnNotify_vibrate;
private Button btnCancelAll;
private Button btnCustomview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView = (TextView)findViewById(R.id.tv);
textView.setText("Buhh");
btnNotify_all = (Button)findViewById(R.id.btnNotify_all);
btnNotify_all.setOnClickListener(this);
btnNotify_lights = (Button)findViewById(R.id.btnNotify_lights);
btnNotify_lights.setOnClickListener(this);
btnNotify_sound = (Button)findViewById(R.id.btnNotify_sound);
btnNotify_sound.setOnClickListener(this);
btnNotify_vibrate = (Button)findViewById(R.id.btnNotify_vibrate);
btnNotify_vibrate.setOnClickListener(this);
btnCancelAll = (Button)findViewById(R.id.btnCancelAll);
btnCancelAll.setOnClickListener(this);
btnCustomview = (Button)findViewById(R.id.btnCustomView);
btnCustomview.setOnClickListener(this);
}
private void showSimple(int id) {
// 第一步
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// 第二步
Notification notification = new Notification(R.drawable.icon, "new message",
System.currentTimeMillis());
notification.defaults = id;
// 第三步
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
// 第四步
notification.setLatestEventInfo(this, "message", "hellow", pendingIntent);
// 第五步
notificationManager.notify(R.layout.main, notification);
}
private void showLedLight() {
// 第一步
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// 第二步
Notification notification = new Notification(R.drawable.icon, "new message",
System.currentTimeMillis());
// notification.defaults = Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xffffff;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
// 第三步
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
// 第四步
notification.setLatestEventInfo(this, "message", "hellow", pendingIntent);
// 第五步
notificationManager.notify(R.layout.main, notification);
}
private void cancelAll() {
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancelAll();
}
private void customView() {
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "new message",
System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.mynotificationview);
contentView.setImageViewResource(R.id.ivShow, R.drawable.icon);
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, NotificationActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notificationManager.notify(0, notification);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNotify_all:
showSimple(Notification.DEFAULT_ALL);
break;
case R.id.btnNotify_lights:
showLedLight();
break;
case R.id.btnNotify_sound:
showSimple(Notification.DEFAULT_SOUND);
break;
case R.id.btnNotify_vibrate:
showSimple(Notification.DEFAULT_VIBRATE);
break;
case R.id.btnCustomView:
customView();
break;
case R.id.btnCancelAll:
cancelAll();
break;
default:
break;
}
}
}