H
H
HelpMePlease22021-11-02 11:39:12
Java
HelpMePlease2, 2021-11-02 11:39:12

Why is the application service not starting?

In short, I started learning Java for Android, wrote a couple of programs, and then I wanted to write a player, a simple media player.

I looked at a couple of examples, and then a problem arose, the background process does not start. I thought that the problem was somewhere in the player code, but no, I created a new project, and pulled everything needed for the test into it. But the result is the same.

And here is the code:

package com.wartortle.netplay

import android.app.*;
import android.os.*;
import android.media.MediaPlayer;
import android.content.Intent;
import android.net.Uri;
import android.widget.*;

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    Toast.makeText(this,"Проверка того, что makeToast() вообще работает",Toast.LENGTH_SHORT).show(); // Тупо проверка
    Intent svc = new Intent(this, BackgroundSoundService.class);
    startService(svc); // Стартуем!
    Toast.makeText(getApplicationContext(),"Сервис должен запуститься",Toast.LENGTH_SHORT).show();
    }
  
  public class BackgroundSoundService extends Service {
    private static final String TAG = null;
    MediaPlayer player;
  
    public IBinder onBind(Intent arg0) {
      Toast.makeText(getApplicationContext(),"OnBind",Toast.LENGTH_LONG).show();
      return null;
    }
    @Override
    public void onCreate() {
      super.onCreate();
      Toast.makeText(getApplicationContext(),"OnCreate",Toast.LENGTH_LONG).show();
      player = MediaPlayer.create(this,Uri.parse("https://ruc.hotmo.org/get/music/20190521/K-391_-_Summertime_64379726.mp3"));
      player.setLooping(false); // Set looping
      player.setVolume(100,100);
    }
    public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(getApplicationContext(),"OnStartCommand",Toast.LENGTH_LONG).show();
      player.start();
      return 1;
    }

    public void onStart(Intent intent, int startId) {
      Toast.makeText(getApplicationContext(),"OnStart",Toast.LENGTH_LONG).show();
    }
    public IBinder onUnBind(Intent arg0) {
      // TO DO Auto-generated method
      return null;
    }

    public void onStop() {

    }
    public void onPause() {

    }
    @Override
    public void onDestroy() {
      Toast.makeText(getApplicationContext(),"OnDestroy",Toast.LENGTH_LONG).show();
      player.stop();
      player.release();
    }

    @Override
    public void onLowMemory() {

    }
  }
}


I looked at StackOverflow, and from there I found out that I need to add this line to the manifest:
<service android:enabled="true" android:name=".BackgroundSoundService" />


But this did not change the situation, the application starts, throws 2 Toasts and that's it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2021-11-02
@402d

add permission start foreground.
services without notification are beaten in a few seconds.
a toast from a background process will result in a crash.
find an example of a working player on the git.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question