у меня возникла проблема... не выходит реализовать сервис(а именно звук alarm) после того как отсчет достигнет конца то-есть 00:00.
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText time;
private TextView introduction;
private long Mtime;
private CountDownTimer mCountDownTimer;
private Button cleartimer, accepttimer, start, reset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
time = findViewById(R.id.et_set_time_off_device);
accepttimer = findViewById(R.id.b_first_button);
introduction = findViewById(R.id.tv_introduction);
cleartimer = findViewById(R.id.v_second_button);
start = accepttimer;
reset = cleartimer;
start.setOnClickListener(this);
reset.setOnClickListener(this);
}
private void starttimer(){
Mtime = Integer.parseInt(time.getText().toString());
mCountDownTimer = new CountDownTimer(Mtime * 60000, 1000) { // adjust the milli seconds here
@Override
public void onTick(long millisUntilFinished) {
time.setText("" + String.format(" %02d minutes, %02d seconds ",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)-
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
time.setText("Конец");
}
}.start();
}
@Override
public void onClick(View view) {
if(view == start) {
starttimer();
startService(new Intent(this, CustomService.class));
}
if(view == reset) {
mCountDownTimer.cancel();
time.setText("");
stopService(new Intent(this, CustomService.class));
}
}
}
Код сервиса
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.Settings;
import androidx.annotation.Nullable;
public class CustomService extends Service {
private MediaPlayer player;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
player = MediaPlayer.create(this, (Settings.System.DEFAULT_ALARM_ALERT_URI));
player.setLooping(true);
player.start();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
player.stop();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}