通知栏有两种开启方式
一、通过NotifyManager
“`
NotificationManger notificationManger=(NotificationManger)getSystemService(Context.NOTIFICAT_SERVCE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//自定义样式需要使用RemoteViews
views = new RemoteViews(getPackageName(), R.layout.layout_download);
builder.setContent(views);
views.setProgressBar(R.id.pb, 100, 50, true);
builder.setSmallIcon(R.mipmap.ic_logo);//需要设置通知栏的logo,注:在android5.0,应用目标版本在21之后的,logo图片只支持白色和透明色两种颜色,否则会显示白框。
notification = builder.build();
//发送一个通知
notificationManger.notify(id,Notification);
二、在server中通过startForegroud
在服务中通过startForeground(1,notification)来抛出一个通知,可以用来提高进程的优先级。服务执行该方法后,通知在状态栏无法通过滑动关闭
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//自定义样式需要使用RemoteViews
views = new RemoteViews(getPackageName(), R.layout.layout_download);
builder.setContent(views);
views.setProgressBar(R.id.pb, 100, 50, true);
builder.setSmallIcon(R.mipmap.ic_logo);//需要设置通知栏的logo,注:在android5.0,应用目标版本在21之后的,logo图片只支持白色和透明色两种颜色,否则会显示白框。
notification = builder.build();
//开启前台通知
startForeground(1000, notification);
通知栏事件设置
Intent notificationIntent = new Intent(getApplicationContext(), DownloadReceiver.class);//这是发送一个广播
notificationIntent.setAction(ACTION_REPTY);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notificationManger.notify(id,Notification);//可以通过再次发送同样id通知来更新通知的内容或者通知进度条的进度
注:如果要开启一个activity的话,必须给intent设置 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
事例
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toRightOf="@id/img"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="正在下载"
android:id="@+id/txt"/>
<ProgressBar
android:id="@+id/pb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
</LinearLayout>
</RelativeLayout>
在服务中发送一个通知
public class DownloadService extends Service {
private Notification notification;
private DownloadReceiver receiver;
private final String ACTION_REPTY = "com.zhibo.jpm.action_repty";
private MyApplication application;
private String url;
private RemoteViews views;
private File file;
public DownloadService() {
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
url = intent.getStringExtra(CommonDefine.INTENT_URL);
receiver = new DownloadReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_REPTY);
registerReceiver(receiver, filter);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
views = new RemoteViews(getPackageName(), R.layout.layout_download);
builder.setContent(views);
views.setProgressBar(R.id.pb, 100, 50, true);
builder.setSmallIcon(R.mipmap.ic_logo);
notification = builder.build();
startForeground(1000, notification);
file = new File(Environment.getExternalStorageDirectory(), "zhibo.apk");
application = (MyApplication) getApplication();
download();
return super.onStartCommand(intent, flags, startId);
}
private void download() {
application.getUserAction().downLoadApk(url, file, new HttpClient.DownLoadListener() {
private double time;
@Override
public void onError(String msg) {
views.setTextViewText(R.id.txt, "下载失败请重试");
Intent notificationIntent = new Intent(getApplicationContext(), DownloadReceiver.class);
notificationIntent.setAction(ACTION_REPTY);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
startForeground(1000, notification);
}
@Override
public void inProgress(float progress, long total) {
long l = System.currentTimeMillis();
if (l - time > 2000) {
views.setTextViewText(R.id.txt, "正在下载(" + (int) (progress * 100) + "%)");
views.setProgressBar(R.id.pb, 100, (int) progress * 100, true);
startForeground(1000, notification);
time = l;
}
}
@Override
public void downloadOver() {
views.setTextViewText(R.id.txt, "下载完成");
startForeground(1000, notification);
//安装应用
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
stopSelf();
application.finishAll();
}
});
}
private class DownloadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case ACTION_REPTY:
download();
break;
default:
break;
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}