【问题标题】:In Android, How can I avoid the onStart method from being deprecated?在 Android 中,如何避免 onStart 方法被弃用?
【发布时间】:2013-09-25 09:34:46
【问题描述】:

我在我的应用程序中设置 onStart 方法时遇到问题。它总是有一个删除线,表示“此方法在 API 级别 5 中已弃用。我需要 onStart,而不是 onStartCommand。

我该如何解决这个问题?

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

}

【问题讨论】:

  • 使用onStartCommand(),不管onStart() 可以做什么
  • 成功了!非常感谢!我在 return super.onStartCommand(intent, flags, startId); 之后添加了一条 toast 消息所以起初,我认为 onStartCommand 是不同的。我之前加过,效果不错。
  • 一个警告,它必须返回 START_NOT_STICKY 否则这两个方法都会被调用。

标签: android service notifications deprecated onstart


【解决方案1】:

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

【讨论】:

  • handleStart 是什么?我有 OnStart(intent, startId);在 onStartCommand 里面...如何修改它?
  • 它是一个占位符,用于在调用 onStart 或 onStartCommand 时执行任何操作。
  • 谢谢。返回START_NOT_STICKY 导致onStart 不被调用。否则,两个方法都会被调用。
【解决方案2】:

您可以像这样使用 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    相关资源
    最近更新 更多