java: 找不到符號 符號: 方法 setLatestEventInfoentInfo

Error:(38, 25) 錯誤: 找不到符號 
符號: 方法 setLatestEventInfo(MyService,String,String,PendingIntent) 
位置: 類型爲Notification的變量 notification
javascript

解決: 
若是編譯的SDK版本在API23以上,就會出現這個問題,由於setLatestEventInfo方法在api23中被刪掉了
java

在api4以上的版本用notification能夠用support v4中的NotificationCompat.Builder。android文檔中給了一個例子:android

Notification noti = new Notification.Builder(mContext)
         .setContentTitle("New mail from " + sender.toString())
         .setContentText(subject)
         .setSmallIcon(R.drawable.new_mail)
         .setLargeIcon(aBitmap)
         .build();
把上面"Notification.Builder"替換成"NotificationCompat.Builder" 
alter+enter 把supportv4中的相關的庫import進來
 
import android.support.v4.app.NotificationCompat;

因而代碼編程了這樣編程

Notification notification;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            notification = new  NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)//必需要先setSmallIcon,不然會顯示默認的通知,不顯示自定義通知
                    .setTicker("顯示於屏幕頂端狀態欄的文本")
                    .setContentTitle("this ics content title")
                    .setContentText("this is content text")
                    .setContentIntent(pendingIntent)
                    .build();
        } else {
            notification = new Notification.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)//必需要先setSmallIcon,不然會顯示默認的通知,不顯示自定義通知
                    .setTicker("顯示於屏幕頂端狀態欄的文本")
                    .setContentTitle("this is content title")
                    .setContentText("this is content text")
                    .setContentIntent(pendingIntent)
                    .build();
        }
        startForeground(2,notification);
注意:!!!上面這段代碼是前臺服務的notification,經過startForeground讓服務變成前臺服務並顯示通知。這時必需要setSmallIcon,不然會顯示默認的通知,不顯示自定義通知!!!若是是正常的用NotificationManager顯示通知則無所謂順序
相關文章
相關標籤/搜索