首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NotificationManager启动空活动

NotificationManager启动空活动
EN

Stack Overflow用户
提问于 2013-01-13 09:00:38
回答 1查看 596关注 0票数 1

我创建了一个通过AlarmManager启动的简单状态通知。一切正常(通知的内容、标题、点击时启动的活动等)。不幸的是,当通知被调用(即AlarmManager关闭)时,将启动并显示一个空活动。该活动只是在状态栏中有我的应用程序的名称及其图标。实际活动本身是空白的。同样,当通知关闭并第一次出现在状态栏中时,就会发生这种情况。当我再次点击通知时,它会转到正确的待处理活动。下面是我的代码:

下面的代码将AlarmManager设置为调用通知:

代码语言:javascript
复制
//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本身的实际代码(它在另一个活动中):

公共类DisplayNotification扩展了SherlockActivity {私有上下文Context = this;

代码语言:javascript
复制
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());

}

}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-13 09:18:31

您正在启动一个活动来发出通知,一开始您看到的是一个空的活动,这是有意义的-这正是您刚刚启动的。请改用BroadcastReceiver。

因此,当它接收到:

代码语言:javascript
复制
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());

   } 
}

您必须将接收者添加到清单中:

代码语言:javascript
复制
<receiver android:name="com.YouForgotWhat.FlightGear.Receiver" >
            <intent-filter>
                <action android:name="com.YouForgotWhat.FlightGear.DisplayNotification" />
            </intent-filter>
 </receiver>

最后,更改代码以启动接收器,如下所示

代码语言:javascript
复制
//PendingIntent to launch activity when the alarm triggers.                    
Intent intent = new Intent("com.YouForgotWhat.FlightGear.DisplayNotification");

PendingIntent displayIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);   
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14299608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档