Why does Eclipse not recognize the build() method from the Notification.Builder class to return a Notification object?

StackOverflow https://stackoverflow.com/questions/11372055

Вопрос

This is my code:


NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(ns);

    //Instantiate the notification

    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();
    Notification.Builder builder = new Notification.Builder(c)
                                .setTicker(tickerText)
                                .setWhen(when)
                                .setContentTitle("Test Notification")
                                .setContentText(arg1.getStringExtra("info"))
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setAutoCancel(true);
    Notification notification = builder.getNotification();
    mNotificationManager.notify(88, notification);

It works find, but using Notification notification = builder.getNotification(); is deprecated. as I should be doing Notification notification = builder.build();. Problem is Eclipse isn't recognizing it as such, meaning it won't let me compile. The doc is clear that build() exists and its the preferred method, but its not working on my end. I would like to use non-deprecated code, so any help will be much appreciated.

imports


import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

Be aware that import android.app.Notification.Builder; is saying its not being used.

Это было полезно?

Решение

If you want develop for version SDK lower than 11, you can use this code instead android.app.Notification.Builder class for creation notification:

private void createNotification(){
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, "Hello", System.currentTimeMillis());
    Intent notificationIntent = new Intent(this, YourActivity.class);

    Random r = new Random();
    int notificationId = r.nextInt();
    notificationIntent.putExtra("n_id", notificationId);
    PendingIntent contentIntent = PendingIntent.getActivity(this, notificationId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.setLatestEventInfo(this, "Party", "Welcome!", contentIntent);
    mNotificationManager.notify(notificationId, notification);      
}

You can cancel this notification in YourActivity like this:

public class YourActivity extends Activity{ 

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity);

    int notificationId = getIntent().getIntExtra("n_id", -1);

    if (notificationId!=-1) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(notificationId);
    }
  }
}

Другие советы

This only happens when, You are compiling your app code against the version which does not hold required method/api (in your case it is build()).

If build is not available in the version you are compiling with, I will suggest you to compile with higher version of Android, You always have minimum sdk version in manifest for backward compatibility.

Thanks

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top