qq_22060245 2015-07-27 14:37 采纳率: 0%
浏览 2082
已结题

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

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;
}
}
}
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
    • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
    • ¥15 彩灯控制电路,会的加我QQ1482956179
    • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
    • ¥15 (关键词-电路设计)
    • ¥15 如何解决MIPS计算是否溢出
    • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
    • ¥15 操作系统相关算法中while();的含义
    • ¥15 CNVcaller安装后无法找到文件
    • ¥15 visual studio2022中文乱码无法解决