在Android中创建通知应用程序? [英] Create Notification App in Android?
问题描述
我想创建一个示例项目上显示按钮,然后在用户选择它在我的应用程序打开该活动或打开that.I选择一个网址的点击通知做了一些事,但我没能完成功能。
I want to create a sample project to show notification on click of button then when the user selects it open that activity in my app or open a url on selection of that.I have done something but i am not able to complete the functionality .
首先我得到使用这个错误的 @燮pressLint(NewApi)
First i am getting error to use this @SuppressLint("NewApi")
如果我不使用此我得到有关这里的误差
If i am not using this i am getting the error on here
Notification noti = new Notification.Builder(this)
活动code
public class NotificationExample extends Activity implements OnClickListener{
private Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_example);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(this);
}
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setTicker("Ticker Title")
.setContentTitle("Content Title")
.setContentText("Notification content.")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).getNotification();
noti.flags=Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
}
}
为什么我必须使用这样的:
why i have to use this :
`@SuppressLint("NewApi")`
在我的code。
我没有得到通知sound.Please建议我什么样的变化,我有我的code键使。
in my code . I am not getting the notification sound.Please suggest me what changes i have to make in my code.
推荐答案
要声音添加到您的通知,请使用以下简称code的。
To add sound to your notification, use following short of code.
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(notificationSound);
展开code
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
0);
Builder builder = new NotificationCompat.Builder(context)
.setTicker("Ticker Title").setContentTitle("Content Title")
.setContentText("Notification content.")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(notificationSound);
Notification noti = builder.build();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
背景
是应用程序上下文
获得使用 getApplicationContext()
。
修改
要使用通知,请使用以下code打开任何链接到浏览器中。
To open any link to Browser using Notification, use following code.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));
通过这个意图,你的的PendingIntent
这篇关于在Android中创建通知应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!