构造函数去precated [英] The constructor is deprecated
问题描述
我做一个机器人application.In我的code在我的Java类中,我得到了一些消息:构造通知(INT,CharSequence中,长期)是pcated德$ P $。一切都确定了应用程序,我没有问题,当我尝试运行应用程序。
我只是想知道为什么这个消息被显示出来。
在我的Java类我的code是:
公共类通知扩展活动{ @覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.notifications); 按钮B =(按钮)findViewById(R.id.bNotifications);
b.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){ NotificationManager纳米=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
通知通知=新的通知(
android.R.drawable.stat_notify_more,
这是重要的,返回System.currentTimeMillis());
上下文的背景下= Notifications.this;
CharSequence的标题=您已经接到通知;
CharSequence的细节=继续跟你做什么;
意向意图=新的Intent();
的PendingIntent未决= PendingIntent.getActivity(上下文,0,
意图,0);
notify.setLatestEventInfo(背景下,标题,细节待定);
nm.notify(0,通知); }
});
}}
在看一看<一个href=\"http://developer.android.com/reference/android/app/Notification.html#Notification%28int,%20java.lang.CharSequence,%20long%29\">the文档:
公开的通知(INT图标,CharSequence的tickerText,时长)
在API级别1
此构造是德$ P $在API级别11 pcated。
使用<一个href=\"http://developer.android.com/reference/android/app/Notification.Builder.html\">Notification.Builder来代替。
块引用>据我所知,这将是相应的呼叫
Notification.Builder
:上下文的背景下= Notifications.this;
通知通知=新Notification.Builder(上下文)
.setTicker(这是重要的)
.setSmallIcon(android.R.drawable.stat_notify_more)
.setWhen(System.currentTimeMillis的())
。建立();正如你所看到的,在Notification.Builder设置各种通知性能提供了更大的灵活性,并提高code可读性,这可能是为什么通知构造是德precated的原因。
I make one android application.In my code in my java class I get some message: "The constructor Notification(int, CharSequence, long) is deprecated". Everything is ok with Application I don't have problem when I try to run the Application. I just want to know why this message is showing up. My code in my java class is:
public class Notifications extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.notifications); Button b = (Button) findViewById(R.id.bNotifications); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notify = new Notification( android.R.drawable.stat_notify_more, "This is important", System.currentTimeMillis()); Context context = Notifications.this; CharSequence title = "You have been notified"; CharSequence details = "Continue with what you have doing"; Intent intent = new Intent(); PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0); notify.setLatestEventInfo(context, title, details, pending); nm.notify(0, notify); } }); } }
解决方案Have a look at the documentation:
public Notification (int icon, CharSequence tickerText, long when)
Added in API level 1This constructor was deprecated in API level 11.
Use Notification.Builder instead.As far as I can tell, this would be the corresponding call to
Notification.Builder
:Context context = Notifications.this; Notification notify = new Notification.Builder(context) .setTicker("This is important") .setSmallIcon(android.R.drawable.stat_notify_more) .setWhen(System.currentTimeMillis()) .build();
As you can see, Notification.Builder offers more flexibility in setting the various notification properties and improves code readability, which might be the reason why the Notification constructor was deprecated.
这篇关于构造函数去precated的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!