Sunday, February 6, 2011

Re: [android-developers] Why a canceled notification displayed later again?

Oh, I have found a cause of the problem. It is a logic problem in my code but it is not displayed above codes.

 
在 2011年2月7日 下午12:26,San Zhang <dahua007.zs@gmail.com>写道:

Below codes is notifications behavior no problem:
=================================================
public class AlarmService extends Service
{
    @Override
    public void onCreate()
    {
   .
   .
   .
        showNotification(sAlarmKeyName);
     Intent i = new Intent(this, AlarmWindow.class);
        i.putExtra("AlarmKeyName", sAlarmKeyName);
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_USER_ACTION);
     startActivity(i);
   .
   .
   .
    }

    private void showNotification(String AlarmKeyName)
    {
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        CharSequence text = AlarmKeyName;

        Notification notification = new Notification(R.drawable.alarmclock, text, System.currentTimeMillis());

     Intent i = new Intent("a.b.c.ALARM", Uri.parse(AlarmKeyName+1), this, AlarmWindow.class);
     Bundle bundle = new Bundle();
     bundle.putString("AlarmKeyName", AlarmKeyName);
     bundle.putInt("AlarmState", 1);  
     i.putExtras(bundle);
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);

        notification.setLatestEventInfo(this, getString(R.string.alarm), settings.getString(AlarmKeyName, " "), contentIntent);
        mNM.notify(Integer.parseInt(AlarmKeyName.substring(5)), notification);
    }
}

public class AlarmWindow extends Activity
{
 @Override
 protected void onCreate(Bundle savedInstanceState)
        {
         super.onCreate(savedInstanceState);
   .
   .
   .

         Button DismissBtn = (Button)findViewById(R.id.dismiss_btn);
         DismissBtn.setOnClickListener(new Button.OnClickListener()
         {
            public void onClick(View v) 
            {
      .
      .
      .
             NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
             mNM.cancel(Integer.parseInt(sAlarmKeyName.substring(5)));
             finish();
            }
         });
 }
}
=================================================


Below codes is notifications behavior having problem:
=================================================
public class AlarmService extends Service
{
 @Override
 public int onStartCommand(Intent intent, int flags, int startId)
 {
         String AlarmKeyName = intent.getStringExtra("AlarmKeyName");

  String actionName = intent.getAction();
  if(actionName!=null)
  {
   if(actionName.equals(CS_ALARM_START))
    AlarmStart(AlarmKeyName);
   if(actionName.equals(CS_ALARM_STOP))
    AlarmStop(AlarmKeyName);
   .
   .
   .
  }
      return START_STICKY; 
 }   

 private void AlarmStop(String AlarmKeyName)
 {
   .
   .
   .
   NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
   mNM.cancel(Integer.parseInt(AlarmKeyName.substring(5)));
 }
 
 private void AlarmStart(String AlarmKeyName)
 {
   .
   .
   .
         showNotification(AlarmKeyName, CS_ALARM_START);
  IntentAlarmWindow(CS_ALARM_START, AlarmKeyName);
     
   .
   .
   .
 }

     private void IntentAlarmWindow(String action, String AlarmKeyName)
     {
  Intent intent = new Intent(action, Uri.parse(action), mContext, AlarmWindow.class);
  intent.putExtra("AlarmKeyName", AlarmKeyName);
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
     startActivity(intent);
     }
   
 private void showNotification(String AlarmKeyName, String action)
     {
  NotificationManager mNM1;
         mNM1 = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
         CharSequence text = AlarmKeyName;

         Notification notification = new Notification(R.drawable.alarmclock, text, System.currentTimeMillis());
         String AlarmTitle = settings.getString(AlarmKeyName, " ");
         String sNotiText = AlarmTitle;
         PendingIntent contentIntent=null;
         Class<?> cls = AlarmService.class;
         if(action.equals(CS_ALARM_START))
          cls = AlarmWindow.class;
      Intent i = new Intent(action+AlarmKeyName, Uri.parse(action+AlarmKeyName), this, cls);
      i.putExtra("AlarmKeyName", AlarmKeyName);
         Calendar calendar = Calendar.getInstance();
         calendar.setTimeInMillis(System.currentTimeMillis());
         calendar.add(Calendar.MILLISECOND, SNOOZE_TIME);
         if(action.equals(CS_ALARM_START))
         {
          i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP); //FLAG_ACTIVITY_NO_HISTORY   Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
              contentIntent = PendingIntent.getActivity(this, 0, i, 0);
              sNotiText = AlarmTitle;
         }
         else
         {
   .
   .
   .
         }
      notification.setLatestEventInfo(this, getString(R.string.alarm), sNotiText, contentIntent);
         mNM1.notify(Integer.parseInt(AlarmKeyName.substring(5)), notification);
    }
}


public class AlarmWindow extends Activity
{
 @Override
 protected void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         Intent intent = getIntent();
          String action = intent.getAction();
         setContentView(R.layout.alarm_window);

         Button DismissBtn = (Button)findViewById(R.id.dismiss_btn);

         sAlarmKeyName = intent.getStringExtra("AlarmKeyName");
   .
   .
   .
         DismissBtn.setOnClickListener(new Button.OnClickListener()
         {
          public void onClick(View v) 
              {
                   Context context = getApplicationContext();
                    Intent i = new Intent(AlarmService.CS_ALARM_STOP, Uri.parse(AlarmService.CS_ALARM_STOP), context, AlarmService.class );
                    i.putExtra("AlarmKeyName", sAlarmKeyName);
                   context.startService(i);
                    finish();
              }
         });
   .
   .
   .


     }

     protected void onNewIntent(Intent intent)
     {
      setIntent(intent);
      String action = intent.getAction();
   .
   .
   .
      if(action.contains(AlarmService.CS_ALARM_START))
      {
              sAlarmKeyName = intent.getStringExtra("AlarmKeyName");
              TextView AlsrmTitle = (TextView)findViewById(R.id.alarmtitle);
              AlsrmTitle.setText(settings.getString(sAlarmKeyName, sAlarmKeyName));
      }
   .
   .
   .
     }  
}
=================================================


在 2011年2月7日 上午10:16,San Zhang <dahua007.zs@gmail.com>写道:

I don't understand how to use AlarmManager to post notifications. I think that all notifications is managered by NotificationManager but not AlarmManager, although they are started and canceled according with alarms life cycle. 
 
In fact, The behavior of notifications were right, no the problem, before I changed some thing in my code. If need, I can write these difference code snippet to here.    

 
2011/2/7 TreKing <trekingapp@gmail.com>

On Sun, Feb 6, 2011 at 7:21 AM, San Zhang <dahua007.zs@gmail.com> wrote:
The results are same: The notification can be deleted from status bar, but it would be displayed twice (and only twice) when another alarm notification (with different value of noti_id) be displayed. I don't understand what is happened. I have searched internet by google but not answer for this.

Are you using AlarmManager to post these notifications? Is so, have you set a repeating alarm? Is so, are you canceling your alarm registered with AlarmManager when the notification is canceled?

-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en



--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment