效果图:
利用广播来实现通知栏按钮的点击事件,并且动态设置按钮,下面是所有的代码。
MainActivity:
package com.example.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
public NotificationManager mNotificationManager;
private RemoteViews mRemoteViews;
private Button start;
private boolean isPlay = false;
/** TAG */
private final static String TAG = "CustomActivity";
/** Notification 的ID */
int notifyId = 101;
/** NotificationCompat 构造器 */
NotificationCompat.Builder mBuilder;
/** 通知栏按钮广播 */
public ButtonBroadcastReceiver bReceiver;
/** 通知栏按钮点击事件对应的ACTION */
public final static String ACTION_BUTTON = "com.notifications.intent.action.ButtonClick";
// 标识
public final static String INTENT_BUTTONID_TAG = "ButtonId";
/** 上一首 按钮点击 ID */
public final static int BUTTON_PREV_ID = 1;
/** 播放/暂停 按钮点击 ID */
public final static int BUTTON_PALY_ID = 2;
/** 下一首 按钮点击 ID */
public final static int BUTTON_NEXT_ID = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
start.setOnClickListener(this);
// 注册广播
bReceiver = new ButtonBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_BUTTON);
registerReceiver(bReceiver, intentFilter);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
/**
* 带按钮的通知栏
*/
public void showButtonNotify() {
mBuilder = new NotificationCompat.Builder(this);
mRemoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout);
mRemoteViews.setImageViewResource(R.id.custom_song_icon, R.drawable.app_img_music);
mRemoteViews.setTextViewText(R.id.tv_custom_song_singer, "歌名");
mRemoteViews.setTextViewText(R.id.tv_custom_song_name, "作家");
// API3.0 以上的时候显示按钮,否则消失
// 如果版本号低于(3。0),那么不显示按钮
if (BaseTools.getSystemVersion() <= 9) {
mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.GONE);
} else {
mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.VISIBLE);
if (isPlay) {
mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_pause);
} else {
mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_play);
}
}
// 点击的事件处理
Intent buttonIntent = new Intent(ACTION_BUTTON);
/* 上一首按钮 */
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PREV_ID);
// 这里加了广播,所及INTENT的必须用getBroadcast方法
PendingIntent intent_prev = PendingIntent.getBroadcast(this, 1, buttonIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_prev, intent_prev);
/* 播放/暂停 按钮 */
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID);
PendingIntent intent_paly = PendingIntent.getBroadcast(this, 2, buttonIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_play, intent_paly);
/* 下一首 按钮 */
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_NEXT_ID);
PendingIntent intent_next = PendingIntent.getBroadcast(this, 3, buttonIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_next, intent_next);
mBuilder.setContent(mRemoteViews).setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示
.setTicker("正在播放").setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级
.setOngoing(true).setSmallIcon(R.drawable.app_img_music);
Notification notify = mBuilder.build();
notify.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(200, notify);
}
/**
* 广播监听按钮点击事件
*/
public class ButtonBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (action.equals(ACTION_BUTTON)) {
// 通过传递过来的ID判断按钮点击属性或者通过getResultCode()获得相应点击事件
int buttonId = intent.getIntExtra(INTENT_BUTTONID_TAG, 0);
switch (buttonId) {
case BUTTON_PREV_ID:
Log.d(TAG, "上一首");
Toast.makeText(MainActivity.this, "上一首", Toast.LENGTH_SHORT).show();
break;
case BUTTON_PALY_ID:
isPlay = !isPlay;
if (isPlay) {
Log.e("tag", "notifi>>>>>>>设置为播放");
Toast.makeText(MainActivity.this, "播放", Toast.LENGTH_SHORT).show();
} else {
Log.e("tag", "notifi>>>>>>>设置为暂停");
Toast.makeText(MainActivity.this, "暂停", Toast.LENGTH_SHORT).show();
}
showButtonNotify();
break;
case BUTTON_NEXT_ID:
Log.d(TAG, "下一首");
Toast.makeText(MainActivity.this, "下一首", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.start) {
// 开启notifi
showButtonNotify();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if (bReceiver != null) {
unregisterReceiver(bReceiver);
}
super.onDestroy();
}
}
清除notification的方法:
mNotificationManager.cancelAll();
上面代码中会用到的一个计算sdk版本的工具类BaseTools:
package com.example.notification;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
public class BaseTools {
public static String getAppVersion(Context context) throws Exception {
PackageManager packageManager = context.getPackageManager();
PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(),0);
String versionName = packInfo.versionName;
return versionName;
}
public static int getSystemVersion(){
int version= android.os.Build.VERSION.SDK_INT;
return version;
}
}
notification的布局notification_layout.xml,其中的样式就是些高度宽度什么的:
<?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"
android:orientation="vertical" >
<ImageView
android:id="@+id/custom_song_icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/app_img_music" />
<LinearLayout
android:id="@+id/ll_custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dip"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/btn_custom_prev"
style="@style/btn_custom_style"
android:src="@drawable/btn_prev" />
<ImageButton
android:id="@+id/btn_custom_play"
style="@style/btn_custom_style"
android:contentDescription="播放"
android:src="@drawable/btn_play" />
<ImageButton
android:id="@+id/btn_custom_next"
style="@style/btn_custom_style"
android:contentDescription="下一首"
android:src="@drawable/btn_next" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dip"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:layout_toLeftOf="@id/ll_custom_button"
android:layout_toRightOf="@id/custom_song_icon"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_custom_song_singer"
style="@style/NotificationTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="title"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_custom_song_name"
style="@style/NotificationContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="content"
android:textSize="12sp" />
</RelativeLayout>
</RelativeLayout>