2015-11-20 85 views
1

這裏是我的代碼:setLatestEventInfo的方法是刪除在API 23中的Android

public static void generateNotification(Context context, String message) { 

    int icon = R.mipmap.ic_launcher; 

    long when = System.currentTimeMillis(); 

    NotificationManager notificationManager = (NotificationManager) 

      context.getSystemService(Context.NOTIFICATION_SERVICE); 

    Notification notification = new Notification(icon, message, when); 

    String title = context.getString(R.string.app_name); 

    Intent notificationIntent = new Intent(context, NotificationView.class); 
    notificationIntent.putExtra("msg", message); 

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 

      Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    PendingIntent intent = 

      PendingIntent.getActivity(context, 0, notificationIntent, 0); 

    notification.setLatestEventInfo(context, title, message, intent); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    notification.defaults |= Notification.DEFAULT_SOUND; 


    notification.defaults |= Notification.DEFAULT_VIBRATE; 

    notificationManager.notify(0, notification); 

} 

如何解決setLatestEventInfo方法在此代碼API 23?

回答

0

我遇到了同樣的問題。這是你需要做的工作和編譯的代碼。

public class AlertReceiver extends BroadcastReceiver{ 

// Called when a broadcast is made targeting this class 
@Override 
public void onReceive(Context context, Intent intent) { 

    createNotification(context, "Times Up", "5 Seconds Has Passed", "Alert"); 

} 

public void createNotification(Context context, String msg, String msgText, String msgAlert){ 

    // Define an Intent and an action to perform with it by another application 
    PendingIntent notificIntent = PendingIntent.getActivity(context, 0, 
      new Intent(context, MainActivity.class), 0); 

    // Builds a notification 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(context) 
        .setSmallIcon(R.drawable.ntt_logo_24_24) 
        .setContentTitle(msg) 
        .setTicker(msgAlert) 
        .setContentText(msgText); 

    // Defines the Intent to fire when the notification is clicked 
    mBuilder.setContentIntent(notificIntent); 

    // Set the default notification option 
    // DEFAULT_SOUND : Make sound 
    // DEFAULT_VIBRATE : Vibrate 
    // DEFAULT_LIGHTS : Use the default light notification 
    mBuilder.setDefaults(Notification.DEFAULT_SOUND); 

    // Auto cancels the notification when clicked on in the task bar 
    mBuilder.setAutoCancel(true); 

    // Gets a NotificationManager which is used to notify the user of the background event 
    NotificationManager mNotificationManager = 
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    // Post the notification 
    mNotificationManager.notify(1, mBuilder.build()); 

    } 

.setLatestEventInfo在API 11中折舊。希望這有助於。

相關問題