2016-06-26 42 views
-1

我實現一個例子的PendingIntent Android Studio中..但NoticationClass似乎有點問題符號..無法解析setLatestEventInfo

public void onClick(View v) { 

       NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
       Notification notify=new Notification(R.drawable.noti,tittle,System.currentTimeMillis()); 

       notify.setLatestEventInfo(getApplicationContext(),subject,body,pending); 

      } 

這是我的搖籃

apply plugin: 'com.android.application' 

android { 

    compileSdkVersion 23 
    buildToolsVersion "23.0.1" 

    defaultConfig { 
     applicationId "testproject.example.com.testproject" 
     minSdkVersion 22 
     targetSdkVersion 22 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.2.0' 
    compile 'com.android.support:design:23.2.0' 
    compile 'com.google.android.gms:play-services:6.5.87' 

} 

我有一個建議使用NotificationBuilder,但這是替代NotificationCompat類,它不在我的代碼中。

我看到這個系統的多個解決方案,但沒能實現它的任何

setLatestEventInfo的其他選擇是什麼。

任何建議將是有益的

+0

什麼是你的'CompileSdkVersion'? – Ironman

+0

我更新了我的Gradle,CompileSdkVersion是23 –

+0

等待我發佈了答案。 – Ironman

回答

1

setLatestEventInfoApi 23被刪除。

欲瞭解更多詳情,請訪問:https://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.html

要支持Api 23 and upper你必須像下面的方式實現。

聲明這個全球

int currentapiVersion = android.os.Build.VERSION.SDK_INT; 

,然後檢查這樣

if (currentapiVersion > LOLLIPOP_MR1) { 

      notification = new Notification(icon, text, time); 
      notification.setLatestEventInfo(this, title, text, contentIntent); // This method is removed from the Android 6.0 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 
      mNM.notify(NOTIFICATION, notification); 
     } else { 
      NotificationCompat.Builder builder = new NotificationCompat.Builder(
        this); 
      notification = builder.setContentIntent(contentIntent) 
        .setSmallIcon(icon).setTicker(text).setWhen(time) 
        .setAutoCancel(true).setContentTitle(title) 
        .setContentText(text).build(); 

      mNM.notify(NOTIFICATION, notification); 
     } 

中的其他部分,你必須實現NotificationCompat.Builder這是唯一的辦法。

UpDate:

工作示例這種方式。

Intent intent = new Intent(ctx, HomeActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder b = new NotificationCompat.Builder(ctx); 

    b.setAutoCancel(true) 
    .setDefaults(Notification.DEFAULT_ALL) 
    .setWhen(System.currentTimeMillis())   
    .setSmallIcon(R.drawable.ic_launcher) 
    .setTicker("Hearty365")    
    .setContentTitle("Default notification") 
    .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.") 
    .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND) 
    .setContentIntent(contentIntent) 
    .setContentInfo("Info"); 


    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(1, b.build()); 
+0

我覺得這個有點不完整,請您發表完整的Pending Intent示例嗎? –

+0

@NeerajVerma我更新它看到我的更新答案。 – Ironman

+0

爲什麼要爲'android.os.Build.VERSION.SDK_INT'聲明一個變量?它已經是一個全球變量......所以,你所做的只是爲OP做一個別名 –