我正在尝试使用按钮进行通知,但不推荐使用 Notification 和 setLatestEventInfo。
两个错误:
1.构造函数 Notification(int, CharSequence, long) 被弃用Notification notify = new Notification(android.R.drawable.stat_notify_more, "Hello all", System.currentTimeMillis());
2.NotificationsetLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent)
类型中的方法不适用于参数(Context, CharSequence, CharSequence, Intent) notify.setLatestEventInfo(context, title, details, intent);
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationManager ns = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(android.R.drawable.stat_notify_more, "Hello all", System.currentTimeMillis());
Context context = MainActivity.this;
CharSequence title ="you have be notified";
CharSequence details = "Continue your work";
Intent intent = new Intent(context,MainActivity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
notify.setLatestEventInfo(context, title, details, intent);
ns.notify(0,notify);
}
});
}
API级别:
android:minSdkVersion="11"
android:targetSdkVersion="17"
什么是替代方案?