2017-03-11 29 views
-4

与措施的通知我想知道如何在Android中创建一个通知与操作图标,让我在打电话的主要活动的方法。如何创建的Android

就像一个在这个形象:Notification icon exemple

+0

你能详细说明你想要做什么吗? – SFAH

+0

我想在android中创建一个通知,就像图像中的显示一样 –

+0

您在询问之前是否研究过这个问题? –

回答

3

首先欢迎计算器。我想提醒你,这不是一个网站,不是学习如何编程的网站,而是一个网站,用能够帮助社区的实际问题提出问题。您的问题必须详细且具体,以及代码或尝试以及错误日志。

话虽这么说,这里是创建一个通知的最佳方式:

第1步 - 创建通知生成器

第一步是创建一个使用NotificationCompat.Builder.build通知制造商() 。您可以使用通知生成器设置各种通知的属性(小图标,大图标,标题,重点等)

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 

第2步 - 设置通知属性

一旦你的Builder对象,你可以设置其通知属性使用Builder对象根据您的要求。但是,这是必须要至少设置以下 -

  • 一个小图标,通过setSmallIcon()
  • 一个标题组,由setContentTitle()
  • 详细的文字设定,通过setContentText()

    mBuilder.setSmallIcon(R.drawable.notification_icon); 
    
    mBuilder.setContentTitle("I'm a notification alert, Click Me!"); 
    
    mBuilder.setContentText("Hi, This is Android Notification Detail!"); 
    
设置

第3步 - 安装操作

这是可选的,并且只有当您要附加通知的操作时才需要。一个动作将允许用户直接从通知到应用程序中的Activity(他们可以查看一个或多个事件或进行进一步的工作)。

动作由包含在应用程序中启动一个活动的意图的PendingIntent限定。要将PendingIntent与手势相关联,请调用NotificationCompat.Builder的适当方法。

例如,如果你想在用户单击通知抽屉通知文本开始的活动,您可以通过调用setContentIntent添加的PendingIntent()。

一个PendingIntent对象可以帮助您以您的名义申请的,执行一个动作,经常在以后的时间,无论你的应用程序是否正在运行。

而且还有的stackBuilder对象将包含在活动开始的人工回堆栈。这确保了从Activity导航到您的应用程序导航到主屏幕。

Intent resultIntent = new Intent(this, ResultActivity.class); 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
stackBuilder.addParentStack(ResultActivity.class); 

// Adds the Intent that starts the Activity to the top of the stack 
stackBuilder.addNextIntent(resultIntent); 
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.setContentIntent(resultPendingIntent); 

第4步 - 发出通知

最后,通过调用NotificationManager.notify()发送您的通知传递的通知对象的系统。确保您在通知构建器对象之前调用NotificationCompat.Builder.build()方法。此方法将所有已设置的选项组合并返回一个新的Notification对象。

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

// notificationID allows you to update the notification later on. 
mNotificationManager.notify(notificationID, mBuilder.build()); 

我希望这能回答你的问题。

+1

什么是“ResultActivity.class”? – hamena314