2016-10-04 67 views
1

我做鬧鐘應用和MyAlarmService有下面的代碼。如何避免`getNotification`棄用?

public class MyAlarmService extends Service 
{ 
    private NotificationManager mManager; 

    @Override 
    public IBinder onBind(Intent arg0) 
    { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() 
    { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
    } 

    @SuppressWarnings("static-access") 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) 
    { 
     mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE); 
     Notification.Builder notification = new Notification.Builder(MyAlarmService.this); 
     Intent intent1 = new Intent(this.getApplicationContext(),ReminderPage.class); 
     PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(),0, intent1,0); 
     notification.setSmallIcon(R.drawable. notification_template_icon_bg) 
       .setContentTitle("Music player") 
       .setContentIntent(pendingNotificationIntent); 
     NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     Notification notification1 = notification.getNotification(); 
     notificationManager.notify(R.drawable.notification_template_icon_bg, notification1); 
     mManager.notify(0, notification1); 
     return super.onStartCommand(intent,flags, startId); 
    } 

    @Override 
    public void onDestroy() 
    { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
    } 
  1. 在OnStart方法,它總是具有刪除線。後一派,我改變onStart方法onStartCommand(),但不知道這是修改的正確方法。
  2. getNotification已被棄用。

AndroidMainfest

<uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="10" /> 

感謝。

回答

1

你可以試試這個,

NotificationCompat.Builder builder = new NotificationCompat.Builder(
       context); 
     notification = builder.setContentIntent(intent) 
       .setSmallIcon(R.drawable.push_icon).setTicker(title).setWhen(when) 
       .setAutoCancel(true).setContentTitle(title) 
       .setContentText(message).build(); 
     notificationManager.notify(requestID, notification); 
+0

應該我需要'返回super.onStartCommand(意向,旗幟,startId);'? –

+0

是的,你只需can.It工作只是改變一些方法 – Saveen

+0

什麼是請求ID,當.... –