正如问题提到的那样,我在重新使用相同的Intent对象,在使用startService之前使用不同的额外数据重新加载它。这个可以吗?或者我是否需要在每次启动服务之前执行“新的Intent(context,ServiceClass)”?
我正在使用服务进行连续的处理流程,所以如果每次需要将数据发送到服务时都需要创建新的Intent,我会有点担心。
问题是目前,服务似乎只执行一次。所有后续的startService调用似乎都不会触发服务。可能是什么原因?
编辑:添加代码
这是主要活动:
public class Measurement extends Activity{
AudioRecord recorder;
int iAudioBufferSize;
boolean bRecording;
int iBytesRead;
// UI stuff
Button bt_Measure;
Button bt_Stop;
RadioButton rd_Live;
RadioButton rd_DataCollection;
RadioButton rd_SampleCollection;
Thread recordThread;
Intent sampleDataIntent;
@Override
public void onDestroy() {
super.onDestroy();
if (recorder != null)
recorder.release();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt_Measure = (Button) findViewById(R.id.bt_measure);
bt_Measure.setEnabled(true);
bt_Stop = (Button) findViewById(R.id.bt_stop);
rd_Live = (RadioButton) findViewById(R.id.rd_live);
rd_DataCollection = (RadioButton) findViewById(R.id.rd_datacollection);
rd_SampleCollection = (RadioButton) findViewById(R.id.rd_samplecollection);
rd_Live.setChecked(true);
bRecording = false;
int iSampleRate = AudioTrack
.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
iAudioBufferSize = AudioRecord.getMinBufferSize(iSampleRate,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
recorder = new AudioRecord(AudioSource.MIC, iSampleRate,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
iAudioBufferSize);
sampleDataIntent = new Intent(this, DetectionService.class);
bt_Measure.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (rd_Live.isChecked()){ // radio button for Live processing
if (!bRecording) {
bt_Measure.setEnabled(false);
bt_Stop.setEnabled(true);
try {
recorder.startRecording();
bRecording = true;
recordThread = new Thread() {
@Override
public void run() {
byte[] buffer = new byte[iAudioBufferSize];
int iBufferReadResult;
int iBytesToRead = iAudioBufferSize;
iBytesRead = 0;
while (!interrupted()) {
iBufferReadResult = recorder.read(buffer,
0, iBytesToRead);
sampleDataIntent.removeExtra("THE_DATA");
sampleDataIntent.putExtra("THE_DATA", buffer);
sampleDataIntent.putExtra("FRAME_SIZE", iBufferReadResult);
startService(sampleDataIntent);
iBytesRead = iBytesRead + iBufferReadResult;
}
}
};
recordThread.start();
} catch (Exception e) {
tv_Speed.setText(e.getLocalizedMessage());
}
}
}
}
});
}
}
这是检测服务:
public class DetectionService extends IntentService {
ByteArrayOutputStream baos;
byte[] sampleBuffer;
byte[] lastSampleBuffer;
PeakFilter peakFilter1;
PeakFilter peakFilter2;
PeakFilter peakFilter3;
PeakFilter peakFilter4;
float fSum = 0;
short sAvgAmpl = 0;
short sMaxAvg = 0;
short sPeakAmpl = 0;
public DetectionService() {
super("DetectionService");
baos = new ByteArrayOutputStream();
peakFilter1 = new PeakFilter(260, 20, 44100);
peakFilter2 = new PeakFilter(260, 20, 44100);
peakFilter3 = new PeakFilter(260, 20, 44100);
peakFilter4 = new PeakFilter(260, 20, 44100);
}
@Override
protected void onHandleIntent(Intent sampleDataIntent) {
try {
baos.write(sampleDataIntent.getByteArrayExtra("THE_DATA"));
} catch (IOException e1) {
e1.printStackTrace();
}
if (baos.size() > 44100 * 5) // 5 second frame
sampleBuffer = baos.toByteArray();
else
return;
try {
// NON THREAD IMPEMENTATION
// Threaded implementation has the same Try..Catch executed in
// separate thread. Neither seems to be running more than once.
short[] preFilterBuffer = new short[sampleBuffer.length/2];
int iSample = 0;
for (int i = 0, j = 0; i < preFilterBuffer.length; i++, j+=2){
preFilterBuffer[iSample] = (short) (sampleBuffer[j+1] << 8 | sampleBuffer[j]);
iSample++;
}
short[] FilteredSamples = new short[preFilterBuffer.length];
// Filters are not spawning threads. Purely Math processing.
peakFilter1.filter(FilteredSamples, preFilterBuffer, preFilterBuffer.length);
peakFilter1.amplify(FilteredSamples, FilteredSamples, 3);
peakFilter2.filter(FilteredSamples, FilteredSamples, FilteredSamples.length);
peakFilter2.amplify(FilteredSamples, FilteredSamples, 3);
peakFilter3.filter(FilteredSamples, FilteredSamples, FilteredSamples.length);
peakFilter3.amplify(FilteredSamples, FilteredSamples, 2);
peakFilter4.filter(FilteredSamples, FilteredSamples, FilteredSamples.length);
// LOT OF MATH on filtered samples
// ......
//
} catch (Exception e) {
e.printStackTrace(); }
}
答案 0 :(得分:0)
您可以为您的服务添加方法,例如putData(...)
...启动服务后,如果它是......活着的,我认为您只是将它用作对象。