2011-01-19 14 views
0

私は現在、ボタンが押された後、一定の期間が経過した後、ステータスバー通知が設定されるアプリケーションを持っています。通知を送信するだけでなく、AlarmManagerを起動する

ユーザーがアプリケーションを開いていないと、通知が表示されてもアプリケーションが再開しても、すべてがうまく動作します。これは私が起こりたいものではありません。通知を自分のものに表示したい(ユーザーがいる場合はどこでも)。私が使用して私のボタンを押してオン

// get a Calendar object with current time 
Calendar cal = Calendar.getInstance(); 
// add minutes to the calendar object 
cal.add(Calendar.SECOND, time); 
Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class); 
alarmintent.putExtras(bundle); 
// In reality, you would want to have a static variable for the request code instead of 192837 
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), HELLO_ID, alarmintent, 0); 
// Get the AlarmManager service 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 

これは私の通知クラスを呼び出すために、このコードを使用して、私のAlarmReceiver.classを呼び出します:

 Intent myIntent = new Intent(context, Note.class); 
    myIntent.putExtras(bundle2); 


    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    context.startActivity(myIntent); 

notification.class :

public class Note extends Activity { 




    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     String ns = Context.NOTIFICATION_SERVICE; 
     final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     int icon = R.drawable.launcher; 
     CharSequence tickerText = "Remind Me!"; 
     long when = System.currentTimeMillis(); 

     final Notification notification = new Notification(icon, tickerText, when); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; 

    final Context context = getApplicationContext(); 
    CharSequence contentTitle = "Remind Me!"; 
    CharSequence contentText = param1; 

    Intent notificationIntent = new Intent(context, Completed.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 


      mNotificationManager.notify(HELLO_ID, notification); 

      HELLO_ID++; 


     } 
} 

答えて

3

これは、AlarmReceiverクラスで作成したインテントが明示的にNoteアクティビティを起動するため、予期した結果です。

AlarmReceiverに通知を作成するのはなぜですか? (アクティビティを起動するのではなく)

+0

メモアクティビティを開始すると、通知が設定されているので、正常です。問題は、アプリが終了した場合に再びメインアクティビティのように見えることです。 –

+0

@Jez:いいえ。「startActivity」を呼び出すと、アクティビティが開始されます。 Daveの言うとおり、通知作成コードをAlarmReceiverに移動します。 –

+0

Ahh ok。あなたの助けを借りてくれてありがとう:-) –

関連する問題