2 qq 22060245 qq_22060245 于 2015.07.27 22:37 提问

通知栏上按钮的点击事件为什么会被多次执行 2C

private void notify(String content) {
// notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new Builder(mContext);
RemoteViews mRemoteViews = new RemoteViews(mContext.getPackageName(),R.layout.notification);
mRemoteViews.setTextViewText(R.id.nf_text, mContent);
if(mType.equals("distributeFE")){
// CountdownHandler.obtainMessage(Start).sendToTarget();
mRemoteViews.setViewVisibility(R.id.nf_layout, View.VISIBLE);
mc.start();
}else if (mType.equals("distributeResp")) {
mIntent = new Intent(Broadcast.REFRESH_EVENT);
mContext.sendBroadcast(mIntent);//发送广播
}
//

//点击的事件处理
Intent buttonIntent = new Intent(Broadcast.RESPONSE_EVENT);
//接受 按钮
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PREV_ID);
//这里加了广播,所及INTENT的必须用getBroadcast方法
PendingIntent intent_prev = PendingIntent.getBroadcast(mContext, 1, buttonIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.nf_accept, intent_prev);
//延时 按钮
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID);
PendingIntent intent_paly = PendingIntent.getBroadcast(mContext, 2, buttonIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.nf_delayed, intent_paly);
//不接受 按钮

buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_NEXT_ID);
PendingIntent intent_next = PendingIntent.getBroadcast(mContext, 3, buttonIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.nf_reject, intent_next);

    mBuilder.setContent(mRemoteViews)
    .setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示
    .setTicker(mContent)
    .setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级
    .setOngoing(true)
     .setSmallIcon(R.drawable.ic_launcher)
    .setDefaults(Notification.DEFAULT_ALL);//铃声,振动,呼吸灯
    Notification notify = mBuilder.build();
    notify.flags = Notification.FLAG_ONGOING_EVENT;
    //会报错,还在找解决思路

// notify.contentView = mRemoteViews;
// notify.contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
mNotificationManager.notify(mLoanId, notify);
}

/** 带按钮的通知栏点击广播接收 */
public void initButtonReceiver(){
    bReceiver = new ButtonBroadcastReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Broadcast.RESPONSE_EVENT);
    mContext.getApplicationContext().registerReceiver(bReceiver, intentFilter);

}

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;
boolean isClciked = false;

/**
 *   广播监听按钮点击事件
 */
public class ButtonBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action.equals(Broadcast.RESPONSE_EVENT)){
            //通过传递过来的ID判断按钮点击属性或者通过getResultCode()获得相应点击事件
            int buttonId = intent.getIntExtra(INTENT_BUTTONID_TAG, 0);
            switch (buttonId) {
            case BUTTON_PREV_ID:
                mRespCode = 1;

// Upload();
System.out.println("接受");
mc.cancel();
// CountdownHandler.postDelayed(new Runnable() {
// public void run() {
// Intent mIntent = new Intent(Broadcast.REFRESH_EVENT);
// mContext.sendBroadcast(mIntent);//发送广播
// }
// }, 3000);
break;
case BUTTON_PALY_ID:
if(isClciked){
return;
}
// Globals.COUNT_DOWN += 5000;
System.out.println("延迟");
isClciked = true;
break;
case BUTTON_NEXT_ID:
mRespCode = 2;
// Upload();
System.out.println("不接受");
mc.cancel();
break;
default:
break;
}
}
}
}

1个回答

CSDNXIAON
CSDNXIAON   2015.07.27 22:47

通知栏,点击事件!
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片