In Android, How can I avoid the onStart method from being deprecated?
Asked Answered
M

2

30

I am having a problem with setting the onStart method in my app. It always has a strikethrough, saying "This method was deprecated in API level 5. I need onStart, not onStartCommand.

How can I resolve this?

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

}
Marked answered 25/9, 2013 at 9:34 Comment(3)
Use onStartCommand(), it will do the same whatever onStart() canParthenon
It worked! Thanks a bunch! I added a toast message after return super.onStartCommand(intent, flags, startId); so at first, I thought onStartCommand was different. I added it before that, and it worked well.Marked
One caveat, it must return START_NOT_STICKY or else both methods will get called.Lucubration
P
66

Use onStartCommand().

It you want to know more about how they change it, refer to google documentation like below.

// 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;
}
Pub answered 25/9, 2013 at 9:38 Comment(3)
what is handleStart? i have OnStart(intent, startId); inside the onStartCommand...how to modify that?Differentiable
It's a placeholder for whatever you want to do when onStart or onStartCommand is called.Hali
Thanks. Returning START_NOT_STICKY causes onStart to not being called. Otherwise, both methods gets called.Lucubration
T
3

You can use like this with 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();
    }
}
Truffle answered 19/12, 2019 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.