1

AlarmManagerを介して起動される簡単なステータス通知を作成しました。すべてが正常に機能します(通知の内容、タイトル、タップされたときに開始されるアクティビティなど)。残念ながら、通知が呼び出されると(つまり、AlarmManagerがオフになると)、空のアクティビティが起動されて表示されます。アクティビティには、ステータスバーにアプリの名前とそのアイコンが表示されます。実際のアクティビティ自体は空白です。繰り返しになりますが、これは通知がオフになり、ステータスバーに初めて表示されたときに発生します。通知をもう一度タップすると、正しい保留中のアクティビティに移動します。これが私のコードです:

通知を呼び出すようにAlarmManagerを設定するコードは次のとおりです。

//Use AlarmManager to trigger the notification/alarm.
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

//PendingIntent to launch activity when the alarm triggers.                    
Intent intent = new Intent("com.YouForgotWhat.FlightGear.DisplayNotification");

PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), 0, intent, 0);   

//Set an alarm to go off at 30 minutes before fuel runs out.
alarmManager.set(AlarmManager.RTC_WAKEUP, endTimeInMillis - (30*60*1000), displayIntent);

そして、これが通知自体の実際のコードです(別のアクティビティにあります):

public class DisplayNotification extends SherlockActivity {private Context context = this;

public void onCreate(Bundle paramBundle) {
    super.onCreate(paramBundle);

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

    mBuilder.setContentTitle("Your fuel may be running low.")
            .setContentText("There are 30 mins left on your timer. Check your fuel gauges.")
            .setSmallIcon(R.drawable.ic_launcher);

    Intent intent = new Intent(this, FuelTimer.class);

    PendingIntent in = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
    mBuilder.setContentIntent(in);

    mNotificationManager.notify(0, mBuilder.build());

}

}

この問題を解決するにはどうすればよいですか?ありがとう!

4

1 に答える 1

2

通知を行うためにアクティビティを起動しています。最初は空のアクティビティが表示されます。これは、起動したばかりのアクティビティです。代わりにBroadcastReceiverを使用してください。

それでそれが受け取るとき:

public class Receiver extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent)
  {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);

    mBuilder.setContentTitle("Your fuel may be running low.")
      .setContentText("There are 30 mins left on your timer. Check your fuel gauges.")
      .setSmallIcon(R.drawable.ic_launcher);

    Intent intent = new Intent(context, FuelTimer.class);

    PendingIntent in = PendingIntent.getActivity(context, 0, intent, 0);
    mBuilder.setContentIntent(in);

    mNotificationManager.notify(0, mBuilder.build());

   } 
}

マニフェストにReceiverを追加する必要があります。

<receiver android:name="com.YouForgotWhat.FlightGear.Receiver" >
            <intent-filter>
                <action android:name="com.YouForgotWhat.FlightGear.DisplayNotification" />
            </intent-filter>
 </receiver>

最後に、レシーバーを起動するようにコードを変更します。

//PendingIntent to launch activity when the alarm triggers.                    
Intent intent = new Intent("com.YouForgotWhat.FlightGear.DisplayNotification");

PendingIntent displayIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);   
于 2013-01-13T01:18:30.583 に答える