如题
/** * function: 自定义通知栏 * Created by wiky on 2016/6/27. */
public class MyNotification {
private static final String TAG = "ProgressNotification";
public final static String INTENT_BUTTONID_TAG = "ButtonId";
/** * 通知栏按钮点击事件对应的ACTION(标识广播) */
public final static String ACTION_BUTTON = "com.notification.intent.action.ButtonClick";
/** * 标识按钮状态:是否在播放 */
public boolean isPlay = false;
/** * 通知栏按钮广播 */
public ButtonBroadcastReceiver receiver;
/** * 播放/暂停 按钮点击 ID */
public final static int BUTTON_PALY_ID = 1;
private final int NOTIFICATION_ID = 0xa01;
private final int REQUEST_CODE = 0xb01;
private Context context;
private NotificationManager notificationManager;
private RemoteViews contentView;
private Notification notification;
public MyNotification(Context context) {
this.context = context;
}
/** * 初始化通知栏信息,并显示 * * @param appName 下载的应用图标 * @param networkSpeed 网速 * @param currentSize 当前下载进度(包大小) * @param totalSize 总的包大小 */
public void initAndNotify(String appName, String networkSpeed, String currentSize, String totalSize) {
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
// 此处设置的图标仅用于显示新提醒时候出现在设备的通知栏
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
notification = mBuilder.build();
// 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局
contentView = new RemoteViews(context.getPackageName(), R.layout.progress_bar_layout);
//设置通知栏信息
contentView.setTextViewText(R.id.txt_appName, appName);//应用名称
contentView.setTextViewText(R.id.txt_network_speed, networkSpeed + "kb/s");//当前网速
StringBuffer stringBuffer = new StringBuffer(currentSize);
stringBuffer.append("/");
stringBuffer.append(totalSize);
stringBuffer.append("M");
contentView.setTextViewText(R.id.txt_size_progress, stringBuffer.toString());//当前下载进度
//如果版本号低于(3.0),那么不显示按钮
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
contentView.setViewVisibility(R.id.btn_download, View.GONE);
} else {
//注册广播
receiver = new ButtonBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_BUTTON);
context.registerReceiver(receiver, intentFilter);
//设置按钮
contentView.setImageViewResource(R.id.btn_download, R.drawable.btn_notification_play);
//设置点击的事件
Intent buttonIntent = new Intent(ACTION_BUTTON);
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID);
PendingIntent intent_paly = PendingIntent.getBroadcast(context, BUTTON_PALY_ID, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
contentView.setOnClickPendingIntent(R.id.btn_download, intent_paly);
}
notification.contentView = contentView;
// 点击notification自动消失
notification.flags = Notification.FLAG_AUTO_CANCEL;
// 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
Intent intent = new Intent(context, MainActivity.class);
// 当用户点击通知栏的Notification时候,切换回TaskDefineActivity。
PendingIntent pi = PendingIntent.getActivity(context, REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = pi;
// 发送到手机的通知栏
notificationManager.notify(NOTIFICATION_ID, notification);
}
/** * (通知栏中的点击事件是通过广播来通知的,所以在需要处理点击事件的地方注册广播即可) * 广播监听按钮点击事件 */
public class ButtonBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_BUTTON)) {
//通过传递过来的ID判断按钮点击属性或者通过getResultCode()获得相应点击事件
int buttonId = intent.getIntExtra(INTENT_BUTTONID_TAG, 0);
switch (buttonId) {
case BUTTON_PALY_ID:
Log.d(TAG, "点击播放/暂停按钮");
onDownLoadBtnClick();
break;
default:
break;
}
}
}
}
private void onDownLoadBtnClick() {
if (isPlay) {
//当前是进行中,则暂停
isPlay = false;
contentView.setImageViewResource(R.id.btn_download, R.drawable.btn_notification_pause);
}else {
//当前暂停,则开始
isPlay = true;
contentView.setImageViewResource(R.id.btn_download, R.drawable.btn_notification_play);
}
notificationManager.notify(NOTIFICATION_ID, notification);
}
/** * 关闭通知 */
public void cancel() {
if (receiver != null) {
context.unregisterReceiver(receiver);
}
if (notificationManager != null) {
notificationManager.cancel(NOTIFICATION_ID);
}
}
}
<?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:background="#00000000" android:orientation="horizontal" android:padding="5dp">
<ImageView android:id="@+id/iv_appIcon" android:layout_width="50dp" android:layout_height="50dp" android:layout_margin="4dp" android:background="@mipmap/ic_launcher"/>
<LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" android:layout_marginLeft="4dp" android:layout_marginStart="4dp" android:orientation="vertical">
<TextView android:id="@+id/txt_appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" />
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:orientation="horizontal">
<TextView android:id="@+id/txt_network_speed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp" />
<TextView android:id="@+id/txt_size_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginStart="4dp" android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<ImageView android:id="@+id/btn_download" android:layout_width="48dp" android:layout_height="48dp" android:layout_margin="4dp" />
</LinearLayout>