发出状态栏通知,下拉状态栏,点击下载更新文件
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
boolean flag = false;
//通知栏消息
private int messageNotificationID = 1000;
private Notification messageNotification = null;
private NotificationManager messageNotificatioManager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uri uri = Uri.parse("http://android.d.cn/rm/2/1100/147182/?http://down.androidgame-store.com/201307121000/079D2048C0545D7DE6B4D077B12DAADD/new/game1/39/109139/txsjgj_1372383471296.apk");
Intent intent =new Intent(Intent.ACTION_VIEW, uri);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
//更新通知栏
messageNotification = new Notification.Builder(this)
.setContentTitle("新消息")
.setContentText("点我就下载!")
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setTicker("有新版本升级了,快去看看!")
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentIntent(pIntent)
.build();
messageNotificatioManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
messageNotificatioManager.notify(messageNotificationID, messageNotification);
//每次通知完,通知ID递增一下,避免消息覆盖掉
messageNotificationID++;
}
}
其中icon的尺寸是有规定的,按规定来,能显示的完美些:
尺寸参考:http://developer.android.com/design/style/iconography.html
小图标:
display density dp units * scale = px units
ldpi ~120 dpi 24x24 dp * .75 = 18x18 px
mdpi ~160 dpi 24x24 dp * 1.0 = 24x24 px
hdpi ~240 dpi 24x24 dp * 1.5 = 36x36 px
xhdpi ~320 dpi 24x24 dp * 2.0 = 48x48 px
大图标:
ldpi: 48x48 px
mdpi: 64x64 px
hdpi: 96x96 px
xhdpi: 128x128 px
具体定制化分格参考官方文档:
http://developer.android.com/design/patterns/notifications.html
比如:通知栏扩展布局:
RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.expanded_view);
Notification notification = mBuilder.setContent(expandedView).
build();
//notification.bigContentView = expandedView;
不过这样就把原本的系统布局覆盖掉了,自定义的布局高度也是有限制的,所以你设置的超过限制的value也是无效的。
直到Android 4.1 (JELLY_BEAN
) 这个版本才支持expandable notifications.