android上的Notification Manager问题

时间:2013-03-19 07:21:08

标签: android notifications notify notificationmanager

我尝试使用按钮进行通知,但不推荐使用Notification和setLatestEventInfo。

两个错误:

1.建议者Notification(int,CharSequence,long)已弃用Notification notify = new Notification(android.R.drawable.stat_notify_more, "Hello all", System.currentTimeMillis());

2.通知类型中的方法setLatestEventInfo(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 个答案:

答案 0 :(得分: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。在您的代码中,您传递的是intent而不是在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);
....

答案 1 :(得分: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);

  }

答案 2 :(得分:1)

这些构造函数和方法已被弃用。因此,您应该使用通知构建器。

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

答案 3 :(得分:0)

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