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

Slo*_*aum 7 notifications android android-intent

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

  1. 用户可以接收多个通知,每个通知都具有int类型的动态ID.
  2. 选择通知后,它会加载显示相应消息(SMS)的活动.
  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"
    />
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

plu*_*ind 16

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

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

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

my_notification.flags |= Notification.FLAG_AUTO_CANCEL;
Run Code Online (Sandbox Code Playgroud)

在创建时将代码放在上面Notification.