gpt4 book ai didi

java - 如何将数据从java类传递到fragment

转载 作者:行者123 更新时间:2023-12-02 11:15:35 25 4
gpt4 key购买 nike

我想将数据(字符串mFilePath)从我的Java类RecordingService.java传递到RecordingFragment.java。这是我的 RecordingService.java

    public class RecordingService extends Service {

private static final String LOG_TAG = "RecordingService";

private String mFileName = null;
private String mFilePath = null;

private MediaRecorder mRecorder = null;

private DBHelper mDatabase;

private long mStartingTimeMillis = 0;
private long mElapsedMillis = 0;
private int mElapsedSeconds = 0;
private OnTimerChangedListener onTimerChangedListener = null;
private static final SimpleDateFormat mTimerFormat = new SimpleDateFormat("mm:ss", Locale.getDefault());

private Timer mTimer = null;
private TimerTask mIncrementTimerTask = null;

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

public interface OnTimerChangedListener {
void onTimerChanged(int seconds);
}

@Override
public void onCreate() {
super.onCreate();
mDatabase = new DBHelper(getApplicationContext());
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startRecording();
return START_STICKY;
}

@Override
public void onDestroy() {
if (mRecorder != null) {
stopRecording();
}

super.onDestroy();
}

public void startRecording() {
setFileNameAndPath();

mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setOutputFile(mFilePath);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setAudioChannels(1);
if (MySharedPreferences.getPrefHighQuality(this)) {
mRecorder.setAudioSamplingRate(44100);
mRecorder.setAudioEncodingBitRate(192000);
}

try {
mRecorder.prepare();
mRecorder.start();
mStartingTimeMillis = System.currentTimeMillis();

//startTimer();
//startForeground(1, createNotification());

} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}

public void setFileNameAndPath(){
int count = 0;
File f;

do{
count++;

mFileName = getString(R.string.default_file_name)
+ "_" + (mDatabase.getCount() + count) + ".mp4";
mFilePath = Environment.getExternalStorageDirectory().getAbsolutePath();
mFilePath += "/SoundRecorder/" + mFileName;

f = new File(mFilePath);
}while (f.exists() && !f.isDirectory());
}

public String getFileNameAndPath(){
return mFilePath;
}

public void stopRecording() {
mRecorder.stop();
mElapsedMillis = (System.currentTimeMillis() - mStartingTimeMillis);
mRecorder.release();
Toast.makeText(this, getString(R.string.toast_recording_finish) + " " + mFilePath, Toast.LENGTH_LONG).show();

//remove notification
if (mIncrementTimerTask != null) {
mIncrementTimerTask.cancel();
mIncrementTimerTask = null;
}

mRecorder = null;

try {
mDatabase.addRecording(mFileName, mFilePath, mElapsedMillis);
Log.v("RecordingService", mFilePath);
Bundle bundle = new Bundle();
RecordFragment recordFragment = new RecordFragment();
bundle.putString("filepath", mFilePath);
recordFragment.setArguments(bundle);
} catch (Exception e){
Log.e(LOG_TAG, "exception", e);
}
}

private void startTimer() {
mTimer = new Timer();
mIncrementTimerTask = new TimerTask() {
@Override
public void run() {
mElapsedSeconds++;
if (onTimerChangedListener != null)
onTimerChangedListener.onTimerChanged(mElapsedSeconds);
NotificationManager mgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mgr.notify(1, createNotification());
}
};
mTimer.scheduleAtFixedRate(mIncrementTimerTask, 1000, 1000);
}

//TODO:
private Notification createNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_mic_white_36dp)
.setContentTitle(getString(R.string.notification_recording))
.setContentText(mTimerFormat.format(mElapsedSeconds * 1000))
.setOngoing(true);

mBuilder.setContentIntent(PendingIntent.getActivities(getApplicationContext(), 0,
new Intent[]{new Intent(getApplicationContext(), MainActivity.class)}, 0));

return mBuilder.build();
}
}

这是我的 RecordingFragment.java:

    public class RecordFragment extends Fragment {
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_POSITION = "position";
private static final String LOG_TAG = RecordFragment.class.getSimpleName();

private int position;

//Recording controls
private FloatingActionButton mRecordButton = null;

// evaluation button
private FloatingActionButton mEvalButton = null;

private Button mPauseButton = null;

private TextView mRecordingPrompt;
private int mRecordPromptCount = 0;

private boolean mStartRecording = true;
private boolean mPauseRecording = true;

private Chronometer mChronometer = null;
long timeWhenPaused = 0; //stores time when user clicks pause button

String filePath;

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment Record_Fragment.
*/
public static RecordFragment newInstance(int position) {
RecordFragment f = new RecordFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);

return f;
}



public RecordFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt(ARG_POSITION);
Log.d("Position Nilai", "" + position);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View recordView = inflater.inflate(R.layout.fragment_record, container, false);
if (getArguments() != null) {
filePath = getArguments().getString("filepath");
}

mChronometer = (Chronometer) recordView.findViewById(R.id.chronometer);
//update recording prompt text
mRecordingPrompt = (TextView) recordView.findViewById(R.id.recording_status_text);

mRecordButton = (FloatingActionButton) recordView.findViewById(R.id.btnRecord);
mRecordButton.setColorNormal(getResources().getColor(R.color.primary));
mRecordButton.setColorPressed(getResources().getColor(R.color.primary_dark));
mRecordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onRecord(mStartRecording);
mStartRecording = !mStartRecording;
}
});

mEvalButton = (FloatingActionButton) recordView.findViewById(R.id.btnEval);
mEvalButton.setColorNormal(getResources().getColor(R.color.primary));
mEvalButton.setColorPressed(getResources().getColor(R.color.primary_dark));
mEvalButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
evaluation();
}
});

mPauseButton = (Button) recordView.findViewById(R.id.btnPause);
mPauseButton.setVisibility(View.GONE); //hide pause button before recording starts
mPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPauseRecord(mPauseRecording);
mPauseRecording = !mPauseRecording;
}
});

return recordView;
}

// Recording Start/Stop
//TODO: recording pause
private void onRecord(boolean start){

Intent intent = new Intent(getActivity(), RecordingService.class);

if (start) {
// start recording
mRecordButton.setImageResource(R.drawable.ic_media_stop);
//mPauseButton.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(),R.string.toast_recording_start,Toast.LENGTH_SHORT).show();
File folder = new File(Environment.getExternalStorageDirectory() + "/SoundRecorder");
if (!folder.exists()) {
//folder /SoundRecorder doesn't exist, create the folder
folder.mkdir();
}

//start Chronometer
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.start();
mChronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
if (mRecordPromptCount == 0) {
mRecordingPrompt.setText(getString(R.string.record_in_progress) + ".");
} else if (mRecordPromptCount == 1) {
mRecordingPrompt.setText(getString(R.string.record_in_progress) + "..");
} else if (mRecordPromptCount == 2) {
mRecordingPrompt.setText(getString(R.string.record_in_progress) + "...");
mRecordPromptCount = -1;
}

mRecordPromptCount++;
}
});

//start RecordingService
getActivity().startService(intent);
//keep screen on while recording
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

mRecordingPrompt.setText(getString(R.string.record_in_progress) + ".");
mRecordPromptCount++;

} else {
//stop recording
mRecordButton.setImageResource(R.drawable.ic_mic_white_36dp);
//mPauseButton.setVisibility(View.GONE);
mChronometer.stop();
mChronometer.setBase(SystemClock.elapsedRealtime());
timeWhenPaused = 0;
mRecordingPrompt.setText(getString(R.string.record_prompt));

getActivity().stopService(intent);
//allow the screen to turn off again once recording is finished
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}

//TODO: implement pause recording
private void onPauseRecord(boolean pause) {
if (pause) {
//pause recording
mPauseButton.setCompoundDrawablesWithIntrinsicBounds
(R.drawable.ic_media_play ,0 ,0 ,0);
mRecordingPrompt.setText((String)getString(R.string.resume_recording_button).toUpperCase());
timeWhenPaused = mChronometer.getBase() - SystemClock.elapsedRealtime();
mChronometer.stop();
} else {
//resume recording
mPauseButton.setCompoundDrawablesWithIntrinsicBounds
(R.drawable.ic_media_pause ,0 ,0 ,0);
mRecordingPrompt.setText((String)getString(R.string.pause_recording_button).toUpperCase());
mChronometer.setBase(SystemClock.elapsedRealtime() + timeWhenPaused);
mChronometer.start();
}
}

private void evaluation(){
Toast.makeText(getActivity(), "Filepath " + filePath, Toast.LENGTH_SHORT).show();
}
}

我想要将数据(stopRecording 方法内的字符串mFilePath)从RecordingService 传递到RecordingFragment。当我尝试从(onCreateView方法和evaluation方法)RecordingFragment访问时,结果是NPE,但是当我登录内部时(stopRecording 方法) RecordingService 类的结果就是我想要的你可以看到下面带有红色边框的图片。 String mFilePath from RecordingService

我看到 this 的另一个问题。说实话,我还是不太明白而且我也不使用BroadCastReceiver。我还是很困惑。

我的问题是:这个问题的解决方案是什么?

谢谢

最佳答案

为此,您可以使用BroadcastReceiver在您的 Activity 中。

Here is a great example I use使用 BroadcastReceiversService <> Activity <> Fragment 之间连续通信。

Here is another great example Service <> Activity 之间的通信。它使用 MessengerIncomingHandler

广播接收器

我将为您的案例做一个简单的例子。

这是您的 Activity 的广播接收器。它将接收您的字符串:

//Your activity will respond to this action String
public static final String RECEIVE_PATH = "com.your.package.RECEIVE_JSON";

private BroadcastReceiver bReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(RECEIVE_PATH)) {
String path = intent.getStringExtra("path");
//Do something with the string
}
}
};
LocalBroadcastManager bManager;

在 fragment 的 onCreateView()

bManager = LocalBroadcastManager.getInstance(getActivity());
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RECEIVE_PATH);
bManager.registerReceiver(bReceiver, intentFilter);

在 fragment 的 onDestroyView() 中,确保取消注册广播接收器。

bManager.unregisterReceiver(bReceiver);

最后,在您的 Service onStart() 中,执行以下操作:

System.out.println("intent Received");
Intent RTReturn = new Intent(YourActivity.RECEIVE_PATH);
RTReturn.putExtra("path", yourPath);
LocalBroadcastManager.getInstance(this).sendBroadcast(RTReturn);

并且您的 fragment 将收到包含该路径的 Intent 作为额外内容。

关于java - 如何将数据从java类传递到fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50308500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com