Pregunta

I created a simple Status Notification that is launched via AlarmManager. Everything works fine (the content of the notification, the title, the activity being launched when it is tapped, etc.). Unfortunately, when the notification is called (ie, AlarmManager goes off), an empty activity is launched and displayed. The activity simply has the name of my app and its icon in the status bar. The actual activity itself is blank. Again, this happens when the notification goes off and appears in the status bar for the first time. When I tap on the notification again, it goes to the correct pending activity. Here's my code:

Here's the code that sets AlarmManager to call the Notification:

//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);

And here's the actual code of the Notification itself (it's in another Activity):

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());

}

}

What do I do to fix this problem? Thanks!

¿Fue útil?

Solución

You're launching an Activity to make the notification, it makes sense that at first you see an empty Activity - that's exactly what you just launched. Use a BroadcastReceiver instead.

So when it receives:

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());

   } 
}

You will have to add the Receiver to the manifest:

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

Lastly, change you code to launch the receiver, so it is like

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

PendingIntent displayIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);   
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top