2017-07-17 51 views
1

我試圖做到這一點:非棄用方式的Android

Notification not = new Notification(idIcon, text, System.currentTimeMillis()); 
PendingIntent pInt = PendingIntent.getActivity(ctx, 0, new Intent(ctx, the_class_to_call), 0); 

not.setLatestEventInfo(ctx, app_name, text, pInt); 

但這被廢棄。

我試圖做到這一點:

NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.app_logo) 
        .setContentTitle("My notification") 
        .setContentText("Hello World!"); 

但是那是過時了。

現在,什麼是非棄用的方式來顯示android中的通知? 我發現的所有回覆都是這兩種方式。

的minSdkVersion:19

回答

0

最後我找到了這個問題的解決方案。

我正在使用兼容性庫。我使用appcompat v7。

appcompat v7具有NotificationCompat.Builder的實現。但是,當我試圖這樣做時:

NotificationCompat.Builder mBuilder = 
     new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.app_logo) 
       .setContentTitle("My notification") 
       .setContentText("Hello World!"); 

編譯器解釋我正在使用appcompat v4構建器。

我修復了它執行到appcompat v7生成器的轉換。

NotificationCompat.Builder mBuilder = 
      (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.app_logo) 
        .setContentTitle("My notification") 
        .setContentText("Hello World!");