【发布时间】:2015-03-20 21:04:35
【问题描述】:
我需要在意图服务的方法中显示通知,我将上下文修改为“getApplicationContext”但它没有显示任何内容。
我使用的是安卓示例:http://developer.android.com/guide/topics/ui/notifiers/notifications.html
private void notf_SinConexion()
{
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder( getApplicationContext() )
.setSmallIcon( R.drawable.ic_action_action_alarm_on )
.setContentTitle( "Title" )
.setContentText( "Message" );
Intent resultIntent = new Intent( getApplicationContext(), MainActivity.class );
TaskStackBuilder stackBuilder = TaskStackBuilder.create( getApplicationContext() );
stackBuilder.addParentStack( MainActivity.class );
stackBuilder.addNextIntent( resultIntent );
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent( resultPendingIntent );
NotificationManager mNotificationManager =
(NotificationManager) getApplicationContext().getSystemService( Context.NOTIFICATION_SERVICE );
// mId allows you to update the notification later on.
mNotificationManager.notify( 10, mBuilder.build() );
}
【问题讨论】:
-
你不需要
getApplicationContext()。将它们替换为this。这是一个示例应用程序,展示了来自IntentService的Notification:github.com/commonsguy/cw-omnibus/tree/master/Notifications/… -
非常感谢,使用
this一切正常!
标签: android