android - 选择通知时取消Android中的动态通知

标签 android notifications android-intent

假设我正在创建一个类似于 SMS 应用程序的 Android 应用程序。要求如下:

  1. 用户可以收到多个通知,每个通知都有一个 int 类型的动态 ID。
  2. 选择通知后,它会加载显示相应消息(SMS)的 Activity 。
  3. 选择的单个通知应自动关闭。

关于如何处理这个问题,我的想法是使用 putExtra 将整数 ID 添加到 Intent 中,然后可以从它加载的 Activity 中的 Intent 访问它,然后关闭调用它的通知。

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

  1. 通知最终会 从服务生成,现在 他们在测试时产生 用户按下按钮。
  2. 选择通知后, 称为 Activity toast 消息, 然后试图驳回 通知。 (为了 能见度)

这是我的问题:

  1. 当第一个通知是 选了,就对了。这 通知被驳回。
  2. 当每个连续的通知是 选中,第一个通知的 显示ID,什么都没有 被解雇。
  3. 我是Java新手,比较习惯 脚本语言(例如 Perl、PHP 等) :)

这是我的来源:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:orientation = "vertical"
    android:layout_width = "fill_parent"
    android:layout_height = "fill_parent"
>
    <Button
        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, 0, notificationIntent, 0 );

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

            mNotificationManager.notify( notify_id, my_notification );
        }
    }

}

最佳答案

一旦创建 Activity ,就会调用其 onCreate() 方法。下次显示时不一定调用该方法。尝试将删除通知的代码移动到 onResume() 方法。熟悉 Activity 生命周期。

顺便说一下,它比您想象的要容易:

http://developer.android.com/reference/android/app/Notification.html#FLAG_AUTO_CANCEL

my_notification.flags |= Notification.FLAG_AUTO_CANCEL;

在创建Notification时把上面的代码。

关于android - 选择通知时取消Android中的动态通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3551938/

相关文章:

ios - 如何更新通知角标(Badge)号码?

ios守护进程背景

c# - 使用 MonoDroid C# 显示状态栏通知

android - 如何从其他 Android 应用程序获取信息?

android - 如何从 firebase 中的值中获取键

java - Android 中的 SparseArray 迭代和删除

安卓 SetTextColor 错误 : Screen switching during mySQL call

android - 在实际设备上运行时 Android 监视器中的控制台错误 "error opening trace file: No such file or directory (2)"

android - 如何将 Intent 从库模块中的 Activity 发送到主应用程序的 Activity

android - 如何通过 Android 中的 Intent 过滤器通过浏览器将 URL 共享到我们的应用程序?