如何在android中停止播放服务

时间:2017-05-04 05:27:33

标签: java android android-layout

我在android中有一个关于服务和BroadCast的问题。我创建了一个使用Alarm Reciver类(它的扩展类BroadCast)的闹钟页面,用于转到RingtonePlaying Service类来启动音乐。现在我想在点击turn_off按钮时停止它,但我不能这样做。请帮我 感谢

AlarmClock类:

 private Button turn_on;
public Button turn_off;
private TextView update_text;

Context context;
AlarmManager alarm_manager;
TimePicker time_picker;
PendingIntent pending_intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm_clock);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    onButtonClickAlarm();

}

private void onButtonClickAlarm() {
    this.context=this;

    turn_on=(Button)findViewById(R.id.alarm_on);
    turn_off=(Button)findViewById(R.id.alarm_off);
    update_text=(TextView)findViewById(R.id.update_text);
    alarm_manager=(AlarmManager)getSystemService(ALARM_SERVICE);
    time_picker=(TimePicker)findViewById(R.id.timePicker);
    final Calendar calendar=Calendar.getInstance();
    final Intent my_intent=new Intent(this,AlarmReciever.class);

    turn_on.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            update_text.setText("Alarm Turn On");

            calendar.set(Calendar.HOUR_OF_DAY,time_picker.getCurrentHour());
            pending_intent= PendingIntent.getBroadcast(AlarmClock.this,0,my_intent,PendingIntent.FLAG_UPDATE_CURRENT);
            alarm_manager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pending_intent);

        }
    });

 final RingtonPlayingService rington=new RingtonPlayingService();
    turn_off.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            update_text.setText("Alarm Turn Off");
        }
    });
}}

AlarmReciver类:

public class AlarmReciever extends BroadcastReceiver {
AlarmClock alarmClock=new AlarmClock();

@Override
public void onReceive(Context context, Intent intent) {
    Log.e("AlarmReciever","Alarm Reciever Error");
    Intent intent_service=new Intent(context,RingtonPlayingService.class);
    context.startService(intent_service);
}}

RingtonPlayingService:

public class RingtonPlayingService extends Service {
public static MediaPlayer mediaPlayer;


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

@Override
public int onStartCommand(Intent intent,int flags , int startId) {



    if(mediaPlayer==null)
    {
        mediaPlayer = MediaPlayer.create(this, R.raw.relax);
        mediaPlayer.start();
    }
    return START_NOT_STICKY;
}

@Override
public boolean onUnbind(Intent intent) {
    mediaPlayer.stop();

    return super.onUnbind(intent);
}

@Override
public void onDestroy() {
    Toast.makeText(this, "onDestroy is Called", Toast.LENGTH_SHORT).show();
    mediaPlayer.stop();
}}

2 个答案:

答案 0 :(得分:0)

尝试此操作以停止服务

Intent inten = new Intent(getApplicationContext(), YourService.class);
                stopService(inten);

答案 1 :(得分:0)

如果要在启动服务之前取消警报,请使用以下代码:

Intent alarmIntent = new Intent(this, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
        manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        manager.cancel(pendingIntent);

如果您想停止服务,请使用以下代码:

Intent intent_service=new Intent(context,RingtonPlayingService.class);
    stopService(intent_service);