2012-07-07 18 views
5

Đây là mã của tôi:Tại sao Eclipse không nhận ra phương thức build() từ lớp Notification.Builder để trả về một đối tượng Notification?


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); 

Nó hoạt động tìm kiếm, nhưng sử dụng Notification notification = builder.getNotification(); bị phản đối. như tôi nên làm Notification notification = builder.build();. Vấn đề là Eclipse không nhận ra nó như vậy, có nghĩa là nó sẽ không cho phép tôi biên dịch. Các tài liệu rõ ràng là build() tồn tại và phương pháp ưa thích của nó, nhưng nó không hoạt động trên đầu của tôi. Tôi muốn sử dụng mã không được dùng nữa, vì vậy mọi trợ giúp sẽ được đánh giá cao.

nhập khẩu


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; 

Hãy nhận biết rằng import android.app.Notification.Builder; đang nói nó không được sử dụng.

+0

Vâng thưa bạn. Tôi vừa cập nhật – Andy

+1

Bạn có đang nhắm mục tiêu API 16 không? 'build()' là mới trong Jelly Bean. – Eric

+1

Bạn đang sử dụng API nào? – Sam

Trả lời

2

Nếu bạn muốn phát triển cho phiên bản SDK thấp hơn 11, bạn có thể sử dụng mã này để thay thế android.app.Notification.Builder lớp để thông báo tạo:

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);  
} 

Bạn có thể hủy thông báo này trong YourActivity như sau:

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); 
    } 
    } 
} 
+0

Ký hiệu đường ống và dấu bằng có nghĩa là '| ='. Có java cụ thể không. Nhưng cảm ơn. – Andy

+0

Có. Đó là một trong những toán tử logic trong Java. Toán tử ** OR (OR) bao gồm bitwise thực hiện phép toán HOẶC bao gồm bitwise trên mỗi cặp bit song song của hai toán hạng. Trong mỗi cặp, kết quả là 1, nếu bit đầu tiên hoặc bit thứ hai là 1 (hoặc cả hai là 1). Nếu không, kết quả là 0. Khi được sử dụng với câu lệnh ghép như ** x | = y **, nó sẽ được đọc là ** x = x | y ** – anivaler

1

Điều này chỉ xảy ra khi, Bạn đang biên dịch mã ứng dụng của mình dựa trên phiên bản không yêu cầu phương thức/api (trong trường hợp của bạn là build()).

Nếu bản dựng không có sẵn trong phiên bản bạn đang biên dịch, tôi sẽ đề nghị bạn biên dịch với phiên bản Android cao hơn, Bạn luôn có phiên bản sd2 tối thiểu phiên bản trong tệp kê khai để tương thích ngược.

Cảm ơn

+0

Nhưng phiên bản sdk của tôi là ICS, cần có điều này. Mặc dù tôi thấy những gì bạn đang nói. – Andy

Các vấn đề liên quan