2

我正在尝试使用按钮进行通知,但不推荐使用 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"

什么是替代方案?

4

4 回答 4

8

1. 构造函数在 api 级别 11 中被弃用。所以你应该使用Notification.Builder.

例如

Notification notification = new Notification.Builder(mContext)
     .setContentTitle("New mail from " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_mail)
     .setLargeIcon(aBitmap)
     .build();

2.在您的代码中,您传递的是意图而不是在 setLatestEventInfo 中挂起

....
Intent intent = new Intent(context,MainActivity.class);
        PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
        notify.setLatestEventInfo(context, title, details, pending);
        ns.notify(0,notify);
....
于 2013-03-19T07:32:10.137 回答
1
notificationManager =
    (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
   myNotification = new Notification(R.drawable.icon,
     "Notification!",
     System.currentTimeMillis());
   Context context = getApplicationContext();
   String notificationTitle = "Exercise of Notification!";
   String notificationText = "http://niravranpara.blogspot.com/";
   Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
   PendingIntent pendingIntent
     = PendingIntent.getActivity(AndroidNotification.this,
       0, myIntent,
       Intent.FLAG_ACTIVITY_NEW_TASK);
   myNotification.defaults |= Notification.DEFAULT_SOUND;
   myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
   myNotification.setLatestEventInfo(context,
      notificationTitle,
      notificationText,
      pendingIntent);
   notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

  }
于 2013-03-19T07:32:08.763 回答
1

这些构造函数和方法已被弃用。因此您应该改用通知生成器。

Notification noti = new Notification.Builder(mContext) .setContentTitle("New mail from " + sender.toString()) .setContentText(subject)
.setSmallIcon(R.drawable.new_mail) .setLargeIcon(aBitmap) .build();

于 2013-03-19T07:37:49.020 回答
0

而不是System.currentTimeMillis() 尝试java.lang.System.currentTimeMillis()

于 2017-06-13T09:40:15.267 回答