标签:
这篇博客讲解一下在Android中使用Notification提示消息给用户,Notification是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification。在本篇博客中,将介绍Notification的常规使用,以及自定义方式的使用,对于每种不同的方式,都提供示例展示效果。 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(0); btnBigViewNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap btm = BitmapFactory.decodeResource(getResources(),
R.drawable.msg);
Intent intent = new Intent(MainActivity.this,
ResultActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
MainActivity.this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Notification noti = new NotificationCompat.Builder(
MainActivity.this)
.setSmallIcon(R.drawable.msg)
.setLargeIcon(btm)
.setNumber(13)
.setContentIntent(pendingIntent)
.setStyle(
new NotificationCompat.InboxStyle()
.addLine(
"M.Twain (Google+) Haiku is more than a cert...")
.addLine("M.Twain Reminder")
.addLine("M.Twain Lunch?")
.addLine("M.Twain Revised Specs")
.addLine("M.Twain ")
.addLine(
"Google Play Celebrate 25 billion apps with Goo..")
.addLine(
"Stack Exchange StackOverflow weekly Newsl...")
.setBigContentTitle("6 new message")
.setSummaryText("mtwain@android.com"))
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, noti);
}
}); btnProgreNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Picture Download")
.setContentText("Download in progress");
builder.setAutoCancel(true);
//通过一个子线程,动态增加进度条刻度
new Thread(new Runnable() {
@Override
public void run() {
int incr;
for (incr = 0; incr <= 100; incr += 5) {
builder.setProgress(100, incr, false);
manager.notify(0, builder.build());
try {
Thread.sleep(300);
} catch (InterruptedException e) {
Log.i(TAG, "sleep failure");
}
}
builder.setContentText("Download complete")
.setProgress(0, 0, false);
manager.notify(0, builder.build());
}
}).start();
}
}); btnProNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Picture Download")
.setContentText("Download in progress");
builder.setProgress(0, 0, true);//设置为true,表示流动
manager.notify(0, builder.build());
//5秒之后还停止流动
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
builder.setProgress(100, 100, false);//设置为true,表示刻度
manager.notify(0, builder.build());
}
}).start();
}
}); <RELATIVELAYOUT
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<IMAGEVIEW
android:id="@+id/imageNo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" />
<TEXTVIEW
android:id="@+id/titleNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/imageNo" />
<TEXTVIEW
android:id="@+id/textNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/titleNo"
android:layout_toRightOf="@id/imageNo" /> btnCustomNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RemoteViews contentViews = new RemoteViews(getPackageName(),
R.layout.custom_notification);
//通过控件的Id设置属性
contentViews
.setImageViewResource(R.id.imageNo, R.drawable.btm1);
contentViews.setTextViewText(R.id.titleNo, "自定义通知标题");
contentViews.setTextViewText(R.id.textNo, "自定义通知内容");
Intent intent = new Intent(MainActivity.this,
ResultActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
MainActivity.this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
MainActivity.this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setTicker("new message");
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setContent(contentViews);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(10, mBuilder.build());
}
});int argb, int onMs, int offMs
):设定前置LED灯的闪烁速率,持续毫秒数,停顿毫秒数。 <!-- 闪光灯权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<!-- 振动器权限 -->
<uses-permission android:name="android.permission.VIBRATE"/>标签:
原文地址:http://blog.csdn.net/u013224148/article/details/43370033