Java 在Android中,如何在服务onDestroy之后保持对象变量的活动状态?
我的Java 在Android中,如何在服务onDestroy之后保持对象变量的活动状态?,java,android,intentservice,mediarecorder,recording,Java,Android,Intentservice,Mediarecorder,Recording,我的IntentService课程中有一个MediaRecorder 我想 运行IntentService开始录制音频 运行IntentService停止录制音频 我使用IntentService,因为我希望即使在手机屏幕关闭的情况下也能进行音频录制。服务在后台运行,所以这是一个很好的方法,对吗 无论如何,我在步骤1的IntentService中启动了我的MediaRecorder。 在步骤2,我的实例化的MediaRecorder为空。似乎IntentService中的所有变量值都被重置,因为
IntentService
课程中有一个MediaRecorder
我想
IntentService
开始录制音频IntentService
停止录制音频IntentService
,因为我希望即使在手机屏幕关闭的情况下也能进行音频录制。服务在后台运行,所以这是一个很好的方法,对吗
无论如何,我在步骤1的IntentService
中启动了我的MediaRecorder
。
在步骤2,我的实例化的MediaRecorder
为空。似乎IntentService
中的所有变量值都被重置,因为服务结束并调用ondestory
我该怎么办?如何保留对我的MediaRecorder
的引用
更新:粘贴我的IntentService
class
package com.dan190.enregistreur.BackgroundService;
import android.app.IntentService;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import com.dan190.enregistreur.RecordedFilesActivity;
import com.dan190.enregistreur.Util.RecordingStatus;
import java.io.IOException;
import java.util.Calendar;
/**
* Created by Dan on 17/03/2017.
*/
public class RecorderService extends IntentService {
private static final String TAG = RecorderService.class.getName();
private MediaRecorder mMediaRecorder;
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public RecorderService(String name) {
super(name);
}
public RecorderService() {
super("RecorderService");
}
/**
* Handles intent.
* Unwraps intent to find out if we need to start, pause, or stop
* @param intent
*/
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Log.d(TAG, "onHandleIntent");
Bundle bundle = intent.getExtras();
int status = bundle.getInt(RecordingStatus.RECORDING_STATUS_KEY);
switch (status) {
case RecordingStatus.STANDBY:
//do nothing
break;
case RecordingStatus.RECORDING:
//if mediaPlayer is null, initiate
//set datasource
//prepare
//record
Log.d(TAG, "start recording");
startRecording();
break;
case RecordingStatus.PAUSED:
//pause
Log.d(TAG, "pause recording");
break;
case RecordingStatus.STOPPED:
//stop
Log.d(TAG, "stop recording");
stopRecording();
Intent newIntent = new Intent(this, RecordedFilesActivity.class);
startActivity(newIntent);
break;
case RecordingStatus.OPENDIR:
//open recorded files
Log.d(TAG, "open directory");
getFileName();
Intent newIntent2 = new Intent(this, RecordedFilesActivity.class);
startActivity(newIntent2);
break;
default:
break;
}
}
/**
* File stuff
*/
private static String pathName,
fileName;
public static String getPath() {
return pathName;
}
public static String getFilePath() {
return fileName;
}
private String getFileName() {
Calendar calendar = Calendar.getInstance();
PackageManager m = getPackageManager();
pathName = getPackageName();
PackageInfo p = null;
try {
p = m.getPackageInfo(pathName, 0);
} catch(PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
pathName = p.applicationInfo.dataDir;
// fileName = Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/Recordings";
fileName = pathName;
fileName += String.format("/%d_%d_%d_%d_%d_%d.3gp", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY) + 1, calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
return fileName;
}
/**
* Recorder stuff
*/
private void startRecording() {
String fileName = getFileName();
Log.d(TAG, String.format("file name is %s", fileName));
if (mMediaRecorder == null) mMediaRecorder = new MediaRecorder();
//NOTE that mediaRecorder cannot pause for API < 24, which is Android 7.0
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setOutputFile(getFileName());
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mMediaRecorder.prepare();
} catch(IOException e) {
Log.e(TAG, "prepare() failed");
e.printStackTrace();
}
try {
mMediaRecorder.start();
} catch(IllegalStateException e) {
Log.e(TAG, e.getStackTrace().toString());
}
}
private void stopRecording() {
try {
mMediaRecorder.stop();
mMediaRecorder.reset();
} catch(RuntimeException e) {
e.printStackTrace();
}
releaseMediaRecorder();
}
private void releaseMediaRecorder() {
if (mMediaRecorder != null) {
mMediaRecorder.release();
mMediaRecorder = null;
} else {
Log.w(TAG, "mediaRecroder is already Null");
}
}
/**
* Lifecycle stuff
*/
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
}
package com.dan190.enregistreur.BackgroundService;
导入android.app.IntentService;
导入android.content.Intent;
导入android.content.pm.PackageInfo;
导入android.content.pm.PackageManager;
导入android.media.MediaRecorder;
导入android.os.Bundle;
导入android.support.annotation.Nullable;
导入android.util.Log;
导入com.dan190.enregistreur.RecordedFilesActivity;
导入com.dan190.enregistreur.Util.RecordingStatus;
导入java.io.IOException;
导入java.util.Calendar;
/**
*由Dan于2017年3月17日创建。
*/
公共类RecorderService扩展了IntentService{
私有静态最终字符串标记=RecorderService.class.getName();
专用媒体记录器;
/**
*创建一个IntentService。由子类的构造函数调用。
*
*@param name用于命名工作线程,仅对调试很重要。
*/
公共记录器服务(字符串名称){
超级(姓名);
}
公共录音服务(){
超级(“录音服务”);
}
/**
*处理意图。
*展开意图,了解我们是否需要开始、暂停或停止
*@param意图
*/
@凌驾
受保护的无效onHandleIntent(@Nullable Intent){
Log.d(标签“onHandleIntent”);
Bundle=intent.getExtras();
int status=bundle.getInt(RecordingStatus.RECORDING_status_KEY);
开关(状态){
案例记录status.STANDBY:
//无所事事
打破
案例记录status.RECORDING:
//如果mediaPlayer为空,则启动
//设置数据源
//预备
//记录
Log.d(标签“开始记录”);
startRecording();
打破
案例记录status.PAUSED:
//停顿
Log.d(标签“暂停记录”);
打破
案例记录status.STOPPED:
//停止
日志d(标签“停止记录”);
停止录制();
Intent newIntent=newIntent(这是RecordedFilesActivity.class);
星触觉(新意图);
打破
案例记录status.OPENDIR:
//打开录制的文件
Log.d(标记为“打开目录”);
getFileName();
Intent newIntent2=newintent(这是RecordedFilesActivity.class);
startActivity(newIntent2);
打破
违约:
打破
}
}
/**
*档案材料
*/
私有静态字符串路径名,
文件名;
公共静态字符串getPath(){
返回路径名;
}
公共静态字符串getFilePath(){
返回文件名;
}
私有字符串getFileName(){
日历=Calendar.getInstance();
PackageManager m=getPackageManager();
路径名=getPackageName();
PackageInfo p=null;
试一试{
p=m.getPackageInfo(路径名,0);
}捕获(PackageManager.NameNotFounde异常){
e、 printStackTrace();
}
路径名=p.applicationInfo.dataDir;
//fileName=Environment.getExternalStorageDirectory()+“/”+Environment.DIRECTORY\u DCIM+“/Recordings”;
fileName=路径名;
fileName+=String.format(“/%d_%d_%d_%d_%d_%d.3gp”,calendar.get(calendar.YEAR),calendar.get(calendar.MONTH)+1,calendar.get(calendar.DAY/u月),calendar.get(calendar.HOUR/u日)+1,calendar.get(calendar.MINUTE),calendar.get(calendar.secondary));
返回文件名;
}
/**
*录音材料
*/
私有无效开始记录(){
字符串fileName=getFileName();
Log.d(TAG,String.format(“文件名为%s”,文件名));
如果(mMediaRecorder==null)mMediaRecorder=new MediaRecorder();
//请注意,mediaRecorder无法暂停API<24,即Android 7.0
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
setOutputFile(getFileName());
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
试一试{
mMediaRecorder.prepare();
}捕获(IOE异常){
Log.e(标记“prepare()failed”);
e、 printStackTrace();
}
试一试{
mMediaRecorder.start();
}捕获(非法状态){
Log.e(标记,e.getStackTrace().toString());
}
}
私有void stopRecording(){
试一试{
mMediaRecorder.stop();
mMediaRecorder.reset();
}捕获(运行时异常e){
e、 printStackTrace();
}
释放MediaRecorder();
}
私有void releaseMediaRecorder(){
如果(mMediaRecorder!=null){
mMediaRecorder.release();
mMediaRecorder=null;
}否则{
w(标记“MediaRecoder已为空”);
}
}
/**
*生命周期的东西
*/
@凌驾
public void onCreate(){
苏