Android Service no Inicia

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Android Service no Inicia



Hola amigos, estoy tratando de que mi App ejecute un servicio en
segundo plano, actualmente logro hacer que funcione pero al eliminar
la app desde las app abiertas se elimina y no logro hacer que ese
servicio se vuelva ejecutar. EL ejemplo con el que les pongo aqui
reproduce un audio aunque a futuro lo que haré será sustituirlo por un
metodo que realize salvas de la BD de mi app. Básicamente lo que
necesito es ayuda para saber que mi proceso se está reiniciando una
vez de cerrad mi app. aqui les muestro mi código y me digan porque no
logro hacer que reinicie nuevamente el servicio.



MI layout contiene esto.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">

<Button
android:id="@+id/iniciarServicio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Start Service" />

<Button
android:id="@+id/buttonStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Stop Service" />

</LinearLayout>

</RelativeLayout>



Mi clase Main_Activity con tiene esto


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

//button objects
private Button buttonStart;
private Button buttonStop;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//getting buttons from xml
buttonStart = (Button) findViewById(R.id.iniciarServicio);
buttonStop = (Button) findViewById(R.id.buttonStop);

//attaching onclicklistener to buttons
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (view == buttonStart) {
//starting service
startService(new Intent(this, MyService.class));
} else if (view == buttonStop) {
//stopping service
stopService(new Intent(this, MyService.class));
}
}
}



y por ultimo mi clase Service contiene esto.


public class MyService extends Service {
//creating a mediaplayer object
private MediaPlayer player;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//getting systems default ringtone
player = MediaPlayer.create(this,
Settings.System.DEFAULT_RINGTONE_URI);
//setting loop play to true
//this will make the ringtone continuously playing
player.setLooping(true);

//staring the player
player.start();

if (intent==null){
Toast.makeText(this, "Se inicio nuevamente el servicio", Toast.LENGTH_SHORT).show();

//getting systems default ringtone
player = MediaPlayer.create(this,
Settings.System.DEFAULT_RINGTONE_URI);
//setting loop play to true
//this will make the ringtone continuously playing
player.setLooping(true);

//staring the player
player.start();
}

//we have some options for service
//start sticky means service will be explicity started and stopped
return START_NOT_STICKY ;
}
}



Les agradezco su ayuda. Gracias









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived