android通知过滤关闭,选择通知时,在Android中取消动态通知

这个博客讨论了如何在Android应用中实现一个类似于SMS功能的系统,其中用户可以接收到具有动态ID的通知。当用户选择一个通知时,相应的消息活动会加载并自动关闭所选通知。然而,在测试过程中,遇到一个问题:连续选择通知时,总是显示第一个通知ID且无法正确取消。源代码展示了如何设置通知、使用PendingIntent以及处理点击事件。问题在于通知ID的管理和取消逻辑上存在错误。
摘要由CSDN通过智能技术生成

假设我正在创建一个类似于SMS应用程序的

Android应用程序.要求如下:

>用户可以接收多个通知,每个通知都具有int类型的动态ID.

>选择通知后,它会加载显示相应消息(SMS)的活动.

>应自动取消选中的单个通知.

我对如何处理这个问题的想法是使用putExtra将整数ID添加到intent中,然后可以从它加载的活动中的intent中访问它,然后它会关闭调用它的通知.

对于我的测试用例,这里是规格:

>通知最终会

从现在开始,从服务生成

他们正在测试时产生

用户按下按钮.

>选择通知时,

被称为活动的消息,

然后试图解雇

通知. (为了

能见度)

这是我的问题:

>第一次通知时

选中,这是对的.该

通知被驳回.

>每次连续通知时

选中,第一个通知

ID显示,没有

驳回.

>我是Java新手,比较习惯

脚本语言(如

Perl,PHP等):)

这是我的来源:

android:orientation = "vertical"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

>

android:id="@+id/create_notification"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:padding="10dp"

android:text = "Create new notification"

/>

package org.test.notifydemo;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

import java.util.Random;

public class aRunNotificationDemo extends Activity

{

private NotificationManager mNotificationManager;

@Override

public void onCreate( Bundle icicle )

{

super.onCreate( icicle );

setContentView( R.layout.run_notify_demo );

mNotificationManager = (NotificationManager) getSystemService( aRunNotificationDemo.NOTIFICATION_SERVICE );

int close_notify_id = getIntent().getIntExtra( "notification_id",0 );

if ( close_notify_id != 0 )

{

Toast.makeText( aRunNotificationDemo.this,"Dimissing this notification: " + Integer.toString(close_notify_id),Toast.LENGTH_SHORT ).show();

mNotificationManager.cancel( close_notify_id );

}

findViewById( R.id.create_notification ).setOnClickListener( new MyButtonListener() );

}

private class MyButtonListener implements Button.OnClickListener

{

public void onClick( View my_view )

{

Random randGen = new Random();

int notify_id = randGen.nextInt();

int icon = R.drawable.icon_notification_01;

CharSequence tickerText = Integer.toString(notify_id) + " New SMS!";

long when = System.currentTimeMillis();

Notification my_notification = new Notification(icon,tickerText,when);

Context context = getApplicationContext();

CharSequence contentTitle = Integer.toString(notify_id) + " New SMS Available!";

CharSequence contentText = Integer.toString(notify_id) + " There is a new SMS available.";

Intent notificationIntent = new Intent( aRunNotificationDemo.this,aRunNotificationDemo.class );

notificationIntent.putExtra( "notification_id",notify_id );

PendingIntent contentIntent = PendingIntent.getActivity( aRunNotificationDemo.this,notificationIntent,0 );

my_notification.setLatestEventInfo( context,contentTitle,contentText,contentIntent );

mNotificationManager.notify( notify_id,my_notification );

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值