我剛開始學習如何通過一些代碼示例創建簡單的android應用程序。試圖在後臺運行服務
我拿了一個音頻捕獲代碼的例子,只使用了一個活動,並希望添加一個服務。以便用戶從視圖中單擊一個按鈕,然後活動將開始/停止錄音服務。
目標是用戶將進入該應用程序,點擊「開始」按鈕,然後服務將開始,並將永遠保持在後臺,直到他返回到應用程序並點擊「停止」。
問題是,無論何時我離開應用程序並打開「最近使用的應用程序」並滑出我的應用程序,出於某種原因,我可以看到Congrats! MyService Created
和My Service Started
消息。我不知道爲什麼,當我滑出了最近使用的應用菜單的服務正在重新啓動,我希望它是繼續運行或接近..
record.java(活動):
package com.example.recordtocloud;
import java.io.File;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
public class record extends Activity {
/**
* Called when the activity is first created.
*/
private Button start,stop,play;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button)findViewById(R.id.button1);
stop = (Button)findViewById(R.id.button2);
play = (Button)findViewById(R.id.button3);
//stop.setEnabled(false);
//play.setEnabled(false);
}
public void start(View view){
startService(new Intent(this, recordbg.class));
//start.setEnabled(false);
//stop.setEnabled(true);
//Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
}
public void stop(View view){
stopService(new Intent(this, recordbg.class));
//stop.setEnabled(false);
//play.setEnabled(true);
//Toast.makeText(getApplicationContext(), "Audio recorded successfully",
//Toast.LENGTH_LONG).show();
}
public void play(View view) throws IllegalArgumentException,
SecurityException, IllegalStateException, IOException{
MediaPlayer m = new MediaPlayer();
m.setDataSource(Environment.getExternalStorageDirectory().
getAbsolutePath() + "/myrecording.3gp");
m.prepare();
m.start();
Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
}
}
recordbg.java(服務):
package com.example.recordtocloud;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.widget.Toast;
import android.util.Log;
import java.io.IOException;
public class recordbg extends Service {
private MediaRecorder myAudioRecorder;
private String outputFile = null;
private static final String TAG = "recordbg";
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
outputFile = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/myrecording.3gp";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
}
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onDestroy() {
Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;
}
}
你能告訴我,如果我上面列出的服務目前「前景」?此外,爲什麼當我從「最近使用的應用」菜單中刪除應用時,我看到了「已創建服務」和「已啓動服務」消息? – 2014-12-06 11:03:04
沒有您的服務不在前臺。也許操作系統正在查殺並重新啓動你的應用程序。另外你必須實現onStartCommand(),看看我的更新。 – greywolf82 2014-12-06 11:07:00