2012-10-10 2 views
0

활동을 확장하여 알림을 만들고 알림이 나타날 때마다 내 앱이 계속 실행됩니다. 매번 같은 활동으로 시작하지 않습니다. 대신 내 앱에서 마지막으로 사용한 활동을 실행합니다. AlarmManager를 통해 특정 날짜/시간에 알림이 시작됩니다. 알림이 내 앱을 시작하지 못하게하려면 어떻게해야합니까? 사용자가 앱을 시작할 때 앱을 실행해야하는데 (그 부분은 정상적으로 작동합니다.) 상태 표시 줄에 처음 알림이 표시되면 앱을 시작하지 않아야합니다. 다음은 알림 코드입니다.Android 알림을 사용하면 알림을 탭하지 않아도 앱이 실행됩니다.

public class DisplayReminderNotification extends SherlockActivity { 

private Context context = this; 

//Grab a handle onto the database and store the name of the reminder. 
protected RemindersDAO remindersDAO; 

@SuppressWarnings("deprecation") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //Get the notification ID. 
    int notifID = getIntent().getExtras().getInt("Reminder_Primary_Key"); 

    String reminderName = getIntent().getExtras().getString("Reminder_Name"); 

    //PendingIntent stores the Activity that should be launched when the user taps the notification. 
    Intent i = new Intent(this, ViewLocalRemindersDetail.class); 
    i.putExtra("NotifID", notifID - randomInt); 
    i.putExtra("notification_tap", true); 

    displayIntent = PendingIntent.getActivity(this, notifID, i, 0); 

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Notification notif = new Notification(R.drawable.launcher_icon, reminderName, System.currentTimeMillis()); 

    CharSequence from = "Here's your reminder:"; 

    CharSequence message = reminderName;   
    notif.setLatestEventInfo(this, from, message, displayIntent); 

    //Fire up the notification. 
    nm.notify(notifID, notif); 

    //Destroy the activity/notification. 
    finish(); 

} 
} 

문제를 어떻게 해결할 수 있습니까?

답변

0

약간의 수정을 거친 후에 코드를 시험해 보았고 제 마지막에 잘 작동합니다.

public class DisplayReminderNotification extends Activity { 

    private Context context = this; 

    @SuppressWarnings("deprecation") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Get the notification ID. 
     int notifID = 1234; 

     String reminderName = "reminder"; 

     // PendingIntent stores the Activity that should be launched when the 
     // user taps the notification. 
     Intent i = new Intent(this, MainActivity.class); 
     i.putExtra("NotifID", 123); 
     i.putExtra("notification_tap", true); 

     PendingIntent displayIntent = PendingIntent.getActivity(this, notifID, 
       i, 0); 

     NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     Notification notif = new Notification(R.drawable.ic_launcher, 
       reminderName, System.currentTimeMillis()); 

     CharSequence from = "Here's your reminder:"; 

     CharSequence message = reminderName; 
     notif.setLatestEventInfo(this, from, message, displayIntent); 

     // Fire up the notification. 
     nm.notify(notifID, notif); 

     // Destroy the activity/notification. 
     finish(); 

    } 
} 

내가 메인 런처 활동으로이 만들어 내가이 활동을 시작할 때마다 그것은 단지 그것을 알림 표시 줄의 알림을 추가하고 다음 코드에 따라, 난 그냥 내 빈 활동이라고 MyActivity로 시작하는 활동을 변경 응용 프로그램을 시작하지 않습니다. 이 코드는 별도의 샘플 앱으로 사용해 보시기 바랍니다. 문제는 코드의 다른 부분에있을 수 있습니다.

관련 문제