2012-10-24 6 views
1

알림 표시 줄에 버튼을 눌러 메시지를 생성하는 간단한 앱을 만들고 있는데, 새 메시지가 표시됩니다. Activity입니다. Activity을 뒤로 밀고 다시 실행하면 단추 누르기가 아무 것도하지 않습니다. 두 번째 작업의 onCreate 메서드에서 알림 표시 줄의 메시지 아이콘을 자동으로 제거하는 싱글 톤 NotificationManager도 만들었습니다. 내 싱글 톤 클래스가 남아 있고 새로운 Object을 만들 수없는 것 같습니다. 나는 그 활동을 끝내야 할 필요가 있으며 나는 그때에만 나의 싱글 톤 클래스가 공개 될 것이라고 믿는다. 는 내가 manifest.xml에 activitied 모두 android:noHistory="true" 설정을 시도하고 또한 MainActivity에이 만든 :다른 활동으로 이동할 때 활동을 닫는 방법?

public void onContextMenuClosed(Menu menu) { 
     super.onContextMenuClosed(menu); 
     this.finish(); 
    } 

그리고 MessageViewer 활동이 시도 :

public class NotifyManager { 
    private static NotificationManager notificationManager=null; 

    public static NotificationManager getInstance() { 
     if(notificationManager==null) { 
      throw new NullPointerException("NotificationManager has not been set yet."); 
     } 
     else { 
      return notificationManager; 
     } 
    } 

    public static void setNotificationManager(NotificationManager nm) throws NotificationManagerException { 
     if(notificationManager==null) { 
      notificationManager = nm; 
     } 
     else { 
      throw new NotificationManagerException("NotificationManager has already been set."); 
     } 
    } 
} 



public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button b = (Button) findViewById(R.id.button1); 
     b.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       try { 
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
        NotifyManager.setNotificationManager(nm); 
        Notification notify = new Notification(
          R.drawable.icon, 
          "New message", System.currentTimeMillis()); 
        Context context = MainActivity.this; 
        CharSequence title = "You have a new message"; 
        CharSequence details = ""; 
        Intent intent = new Intent(context, MessageViewer.class); 
        PendingIntent pending = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); 
        notify.setLatestEventInfo(context, title, details, pending); 
        nm.notify(0, notify); 
       } 
       catch(Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
: 이것은 내 코드입니다

public void onBackPressed() { 
     super.onBackPressed(); 
     this.finish(); 
    } 

그건 그렇고, 내 두 번째 활동 int는 현재 알림을 닫는 것입니다 :

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_message_viewer); 

     NotifyManager.getInstance().cancel(0); 
    } 

답변

0

글쎄, 다른 ID로 다른 알림을 만들어야합니다. nm.notify(0, notify);에서 모든 알림에 동일한 ID를 부여 할 수 없습니다. 이렇게하면 모든 알림에 대해 다른 알림 ID를 부여하려고합니다. 잘하면 작동 될 것입니다

+0

빠른 답변을 주셔서 감사합니다. 아이디어는 응용 프로그램을 닫고 다시 시작하는 것이 었습니다. 그런 식으로 리소스가 해제되고 그렇게 할 수 있습니다. 강제로 앱을 닫으면 제대로 작동합니다. – Tsikon

관련 문제