在Android中,当通知被选中时取消动态通知

7
假设我正在创建一个类似短信应用的Android应用程序,要求如下:
  1. 用户可以收到多个通知,每个通知具有int类型的动态ID。
  2. 当选择通知时,它会加载一个活动,显示相应的消息(即短信)。
  3. 所选的单个通知应自动消失。
我的处理方法是使用putExtra将整数ID添加到意图中,然后可以从加载它的活动中的意图中访问它,该活动将解除调用它的通知。
对于我的测试案例,以下是规格:
  1. 通知最终将从服务生成,现在它们是在测试用户按下按钮时产生的。
  2. 当选择通知时,被调用的活动显示消息,然后尝试解除通知。 (为了更好地看到效果)
以下是我的问题:
  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 );
        }
    }

}
1个回答

16

当一个Activity被创建后,它的onCreate()方法会被调用。下一次显示该Activity时,该方法不一定会被调用。试着将移除通知的代码移到onResume()方法中。了解Activity生命周期。

顺便说一句,这比你想象的要容易:

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

my_notification.flags |= Notification.FLAG_AUTO_CANCEL;

创建 Notification 时,请将上述代码放置在其上方。


谢谢,这正是我在寻找的答案。对于像这样的功能,对于Java新手来说很难找到功能性文档,而不必阅读整个Android开发者网站...我们不知道该搜索什么。我熟悉生命周期作为一个过程,但我假设每个新意图都会重新实例化Activity。你知道他们所说的“假设” :) 再次感谢。 - Slobaum
请注意,您现在可以在Kotlin和Java中使用NotificationCompat.Builder(my_context, my_channel_id).setAutoCancel(true)来完成相同的操作。 - Luciano Brum

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接