web-dev-qa-db-fra.com

Sous Android, comment éviter que la méthode onStart soit déconseillée?

J'ai un problème avec la définition de la méthode onStart dans mon application. Il est toujours barré, indiquant "Cette méthode est déconseillée au niveau 5 de l'API. J'ai besoin de onStart, pas de onStartCommand.

Comment puis-je résoudre ça?

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;
    }

}
28
Christopher Treanor

Utilisez onStartCommand().

Si vous souhaitez en savoir plus sur la façon dont ils le modifient, reportez-vous à la documentation Google comme ci-dessous.

// 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;
}
61
Lazy Ninja

Vous pouvez utiliser comme ceci avec onStartCommand ().

package htin.linnzaw.service;

import Android.app.Service;
import Android.content.Intent;
import Android.media.MediaPlayer;
import Android.os.IBinder;
import Android.widget.Toast;

public class MyService extends Service
{
    private MediaPlayer mediaplayer;
    public MyService()
    {

    }

    @Override
    public IBinder onBind(Intent intent)
    {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate()
    {
        Toast.makeText(this, "Service created", Toast.LENGTH_SHORT).show();
        mediaplayer = MediaPlayer.create(this, R.raw.eventually);
        mediaplayer.setLooping(false);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startid)
    {
        Toast.makeText(this, "Service  Started", Toast.LENGTH_SHORT).show();
        mediaplayer.start();
        return startid;
    }

    @Override
    public void onDestroy()
    {
        Toast.makeText(this, "Service stopped", Toast.LENGTH_SHORT).show();
        mediaplayer.stop();
    }
}
1
Htin Linn Zaw