2015-02-23 37 views
-2

你好,有什麼辦法可以改變這個代碼來使用較低的API?目前即時通訊使用10和Android工作室告訴我這個代碼需要11個和16更低API的通知?

public class NotificationBro extends Service { 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 


    @Override 
    public void onCreate() { 

     NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class); 
     PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0); 

     Notification mNotify = new Notification.Builder(this) 
       .setContentTitle("NotificationExample!") 
       .setContentText("zzzzz") 
       .setSmallIcon(R.drawable.mem1) 
       .setContentIntent(pIntent) 
       .build(); 

     mNM.notify(1, mNotify); 
    } 
} 
+1

之前要求至少你應該讀作Notification.Builder'文檔.... – Selvin 2015-02-23 17:05:57

+0

** ** 1 API級別10是時下** **沒用(8時仍支持)。 ** 2 **如果這樣確定,請使用支持庫及其對象和方法('NotificationCompat')。 – 2015-02-23 17:16:55

+0

@DerGolem我可以在該代碼中使用.build()的instad? – zzz 2015-02-23 17:22:05

回答

0

您可以使用此方法:

在此方法中使用的一些方法是棄用

private void showNotification(Context context, int id, 
      String notifyText, String title, String content) { 

     Notification notification = new Notification(R.drawable.ic_launcher, 
       notifyText, System.currentTimeMillis()); 

     Uri soundUri = RingtoneManager 
       .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     notification.sound = soundUri; 

     Intent notificationIntent = new Intent(context, MainActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
       notificationIntent, 0); 

     notification.setLatestEventInfo(context.getApplicationContext(), title, 
       content, contentIntent); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     NotificationManager mNotificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     mNotificationManager.notify(id, notification); 
    } 
+0

你會回答這個問題嗎?http://stackoverflow.com/q/ 3671748分之41692300 – 2017-01-18 08:07:23