【问题标题】:NotificationManager launches empty ActivityNotificationManager 启动空 Activity
【发布时间】:2013-01-13 09:18:30
【问题描述】:

我创建了一个通过 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);

这是 Notification 本身的实际代码(它在另一个 Activity 中):

公共类 DisplayNotification 扩展 SherlockActivity { 私有上下文上下文 = 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());

}

}

我该怎么做才能解决这个问题?谢谢!

【问题讨论】:

    标签: android notifications alarmmanager notificationmanager


    【解决方案1】:

    您正在启动一个 Activity 来发出通知,起初您会看到一个空的 Activity 是有道理的 - 这正是您刚刚启动的。请改用 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);   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多