2015-09-02 31 views
47

Tôi đang làm việc trên Thông báo và tôi phải sử dụng setLatestEventInfo. Tuy nhiên, Android Studio hiển thị thông báo lỗi sau:Không thể giải quyết Phương thức setLatestEventInfo

không thể giải quyết phương pháp setLatestEventinfo

Dưới đây là đoạn mã của tôi:

private void createNotification(Context context, String registrationID) { 
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.icon,"Registration Successfull",System.currentTimeMillis()); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    Intent intent = new Intent(context,RegistrationResultActivity.class); 
    intent.putExtra("registration_ID",registrationID); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0); 
    notification.setLatestEventInfo(context,"Registration","Successfully Registered",pendingIntent); 
} 

Hoặc nếu họ là một cách khác để làm như vậy, vui lòng đề nghị tôi điều đó.

+10

'setLatestEventInfo' là 'phản đối' làm nó sử dụng' NotificationCompat.Builder' –

Trả lời

66

Dưới đây là một ví dụ đơn giản về cách làm việc với Thông báo, hãy thực hiện nó, hy vọng điều đó sẽ hữu ích!

MainActivity.java

public class MainActivity extends ActionBarActivity { 

    Button btnShow, btnClear; 
    NotificationManager manager; 
    Notification myNotication; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     initialise(); 

     manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     btnShow.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       //API level 11 
       Intent intent = new Intent("com.rj.notitfications.SECACTIVITY"); 

       PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0); 

       Notification.Builder builder = new Notification.Builder(MainActivity.this); 

       builder.setAutoCancel(false); 
       builder.setTicker("this is ticker text"); 
       builder.setContentTitle("WhatsApp Notification");    
       builder.setContentText("You have a new message"); 
       builder.setSmallIcon(R.drawable.ic_launcher); 
       builder.setContentIntent(pendingIntent); 
       builder.setOngoing(true); 
       builder.setSubText("This is subtext..."); //API level 16 
       builder.setNumber(100); 
       builder.build(); 

       myNotication = builder.getNotification(); 
       manager.notify(11, myNotication); 

       /* 
       //API level 8 
       Notification myNotification8 = new Notification(R.drawable.ic_launcher, "this is ticker text 8", System.currentTimeMillis()); 

       Intent intent2 = new Intent(MainActivity.this, SecActivity.class); 
       PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 2, intent2, 0); 
       myNotification8.setLatestEventInfo(getApplicationContext(), "API level 8", "this is api 8 msg", pendingIntent2); 
       manager.notify(11, myNotification8); 
       */ 

      } 
     }); 

     btnClear.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       manager.cancel(11); 
      } 
     }); 
    } 

    private void initialise() { 
     btnShow = (Button) findViewById(R.id.btnShowNotification); 
     btnClear = (Button) findViewById(R.id.btnClearNotification);   
    } 
} 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

    <Button 
     android:id="@+id/btnShowNotification" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Show Notification" /> 

    <Button 
     android:id="@+id/btnClearNotification" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Clear Notification" /> 

</LinearLayout> 

Và hoạt động này sẽ được khai trương vào bấm vào thông báo,

public class SecActivity extends Activity { 

} 
+0

Cảm ơn bạn rất nhiều !! –

+0

Bạn có thể xem câu hỏi của tôi không? http://stackoverflow.com/questions/35570767/setlatesteventinfo-method-cannot-be-resolved –

+0

PendingIntent.getActivity (MainActivity.this, 1, intent, 0); trong phương pháp thứ tư param này là để vượt qua cờ. Điều đó nghĩa là gì nếu bạn vượt qua 0? –

22

Bạn viết cho bạn để sử dụng setLatestEventInfo. Điều đó có nghĩa là bạn đã sẵn sàng để ứng dụng của mình không tương thích với các phiên bản Android mới hơn? Tôi khuyên bạn nên sử dụng support library v4 có chứa lớp NotificationCompat cho ứng dụng sử dụng API 4 trở lên.

Nếu bạn thực sự không muốn sử dụng thư viện hỗ trợ (ngay cả khi tối ưu Proguard, sử dụng NotificationCompat sẽ thêm 100Ko vào ứng dụng cuối cùng), một cách khác là sử dụng sự phản chiếu. Nếu bạn triển khai ứng dụng của mình trên phiên bản Android vẫn có phiên bản không được chấp nhận setLatestEventInfo, trước hết bạn nên kiểm tra xem bạn có đang ở trong môi trường như vậy không và sau đó bạn sử dụng phản ánh để truy cập phương thức.

Bằng cách này, Android Studio hoặc trình biên dịch sẽ không phàn nàn, vì phương pháp này được truy cập vào thời gian chạy và không phải lúc biên dịch. Ví dụ:

Notification notification = null; 

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 
    notification = new Notification(); 
    notification.icon = R.mipmap.ic_launcher; 
    try { 
     Method deprecatedMethod = notification.getClass().getMethod("setLatestEventInfo", Context.class, CharSequence.class, CharSequence.class, PendingIntent.class); 
     deprecatedMethod.invoke(notification, context, contentTitle, null, pendingIntent); 
    } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException 
      | InvocationTargetException e) { 
     Log.w(TAG, "Method not found", e); 
    } 
} else { 
    // Use new API 
    Notification.Builder builder = new Notification.Builder(context) 
      .setContentIntent(pendingIntent) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle(contentTitle); 
    notification = builder.build(); 
} 
+0

Bạn đã nhận rằng "phải" nghĩa đen, kudo cho sự thông suốt. IMO OP chỉ đọc tài liệu hướng dẫn sử dụng phương thức đó. Ví dụ [Chạy một dịch vụ ở nền trước] (https://developer.android.com/guide/components/services.html#Foreground) vẫn có tham chiếu đến phương thức đó. – TWiStErRob

+0

Cuối cùng, thứ gì đó hoạt động để tôi có thể cập nhật ứng dụng của mình bằng các tệp mở rộng thành API 23+ Cảm ơn bạn rất nhiều !! –

-2

Tới dự án -> properties và thiết lập mục tiêu android-21

+0

Có lẽ điều này là chính xác (tôi không biết), nhưng bạn không đưa ra bất kỳ gợi ý nào để giải thích tại sao đây là một giải pháp. –

+0

Lý do hoạt động là vì API có sẵn trên Android dưới 23 và không được dùng nữa bắt đầu với API Cấp 23. – dvallejo

+0

hoạt động như một nét duyên dáng ... hoàn hảo –

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