2013-09-25 95 views
21

我在我的應用程序中設置onStart方法時遇到問題。它總是有一個刪除線,說:「這個方法在API層面5.已被否決,我需要在onStart,不onStartCommand。在Android中,如何避免onStart方法被棄用?

我怎樣才能解決這個問題?

MyNotificationService.java

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.widget.Toast; 

public class MyNotificationService extends Service { 

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

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     Toast.makeText(this, "OnCreate()", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     Toast.makeText(this, "OnDestroy()", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    @Deprecated 
    public void onStart(Intent intent, int startId) { 
     // TODO Auto-generated method stub 
     super.onStart(intent, startId); 
    } 



} 

Reminder_2。 java的

import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.Window; 
import android.widget.DatePicker; 
import android.widget.ImageButton; 

public class Reminder_2 extends Activity { 
String message; 
DatePicker datepicker; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_reminder_2); 
     datepicker=(DatePicker)findViewById(R.id.datePicker1); 
     Home(); 
     Next(); 
     Save(); 
    } 
    private void Next() { 
     final MediaPlayer button_tone = MediaPlayer.create(this, R.raw.button_sound); 
     ImageButton Button = (ImageButton) findViewById(R.id.imageButton1); 
     View.OnClickListener myListener = new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        button_tone.start(); 
        finish(); 
      } 
     }; 
     Button.setOnClickListener(myListener); 
    } 
    private void Save() { 
     final MediaPlayer button_tone = MediaPlayer.create(this, R.raw.button_sound); 
     ImageButton Button = (ImageButton) findViewById(R.id.imageButton3); 
     View.OnClickListener myListener = new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        button_tone.start(); 
        Intent intent = new Intent(); 
        intent.setClass(getApplicationContext(), MyNotificationService.class); 
        startService(intent); 
      } 
     }; 
     Button.setOnClickListener(myListener); 
    } 
    private void Home() { 
     final MediaPlayer button_tone = MediaPlayer.create(this, R.raw.button_sound); 
     ImageButton Button = (ImageButton) findViewById(R.id.imageButton2); 
     View.OnClickListener myListener = new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       button_tone.start(); 
       Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent); 
      } 
     }; 
     Button.setOnClickListener(myListener); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.reminder, menu); 
     return true; 
    } 

} 
+1

使用'onStartCommand()',它會做同樣的事情'onStart()'可以 –

+0

它的工作!謝謝一堆!我在返回super.onStartCommand(intent,flags,startId)後添加了一個toast消息;所以起初,我認爲onStartCommand是不同的。我在此之前添加了它,並且運行良好。 –

回答

54

使用onStartCommand()

它你想知道更多關於他們如何改變它,請參考下面的谷歌文檔。

// This is the old onStart method that will be called on the pre-2.0 
// platform. On 2.0 or later we override onStartCommand() so this 
// method will not be called. 
@Override 
public void onStart(Intent intent, int startId) { 
    handleStart(intent, startId); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    handleStart(intent, startId); 
    return START_NOT_STICKY; 
} 
+0

謝謝! onStartCommand現在可以正常工作。 –

+2

handleStart是什麼?我有OnStart(intent,startId);在onStartCommand裏面...如何修改它? – Elizabeth

+2

當onStart或onStartCommand被調用時,它是您想要執行的任何佔位符。 – Shriken

相關問題