0

按下开始按钮时音频服务可以正常启动,但按下停止按钮时不会停止。这是java活动。我按照一个在线示例创建了一个播放音频的服务。

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Ship extends Activity implements View.OnClickListener {
private static final String TAG = "ShipService";
public static final Integer[] TIME_IN_MINUTES = { 30, 45, 60, 180, 360 };
public MediaPlayer mediaPlayer;
public Handler handler = new Handler();
public Button button2;
public Spinner spinner2;

// Initialize the activity
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // wl.acquire();
    setContentView(R.layout.ship);
    button2 = (Button) findViewById(R.id.btn2);
    button2.setOnClickListener(this);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
            android.R.layout.simple_spinner_item, TIME_IN_MINUTES);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter);

}

// Handle button callback
@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn2:
        Log.d(TAG, "onClick: starting service");
        startService(new Intent(this, Shipservice.class));
        break;
    case R.id.stop:
        Log.d(TAG, "onClick: stopping srvice");
        stopService(new Intent(this, Shipservice.class));
        break;
    }
}
}
4

2 回答 2

0

Please see this code-

public class ServicesDemo extends Activity implements OnClickListener {
 private static final String TAG = "ServicesDemo";
 Button buttonStart, buttonStop,buttonNext;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  buttonStart = (Button) findViewById(R.id.buttonStart);
  buttonStop = (Button) findViewById(R.id.buttonStop);
  buttonNext = (Button) findViewById(R.id.buttonNext);

  buttonStart.setOnClickListener(this);
  buttonStop.setOnClickListener(this);
  buttonNext.setOnClickListener(this);
 }

 public void onClick(View src) {
  switch (src.getId()) {
  case R.id.buttonStart:
   Log.d(TAG, "onClick: starting srvice");
   startService(new Intent(this, MyService.class));
   break;
  case R.id.buttonStop:
   Log.d(TAG, "onClick: stopping srvice");
   stopService(new Intent(this, MyService.class));
   break;
  case R.id.buttonNext:
   Log.d(TAG, "onClick: next Page");
   Intent intent=new Intent(this,NextPage.class);
   startActivity(intent);
   break;
  }
 }
}

Read more: http://www.androidhub4you.com/2013/03/how-to-create-services-in-android.html#ixzz2jk9fU7AJ

于 2013-11-05T04:48:32.580 回答
0

您尚未在 onCreate() 方法中设置停止按钮的单击侦听器。

创建停止按钮的对象

public Button button2,stopBtn;

更新您的 onCreate() 方法,如下所示

 button2 = (Button) findViewById(R.id.btn2);
 button2.setOnClickListener(this);

// 停止按钮

 stopBtn= (Button) findViewById(R.id.stop);
 stopBtn.setOnClickListener(this);
于 2013-11-05T04:52:10.970 回答