알림 표시 줄에 버튼을 눌러 메시지를 생성하는 간단한 앱을 만들고 있는데, 새 메시지가 표시됩니다. 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);
}
빠른 답변을 주셔서 감사합니다. 아이디어는 응용 프로그램을 닫고 다시 시작하는 것이 었습니다. 그런 식으로 리소스가 해제되고 그렇게 할 수 있습니다. 강제로 앱을 닫으면 제대로 작동합니다. – Tsikon