Answer the question
In order to leave comments, you need to log in
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() {
}
}
}
<service android:enabled="true" android:name=".BackgroundSoundService" />
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question