Reputation: 302
I want to pass data (String mFilePath
) from my Java class RecordingService.java
to RecordingFragment.java
. This is my 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();
}
}
And this is my 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();
}
}
I want pass data (String mFilePath
inside the stopRecording
method) from RecordingService
to RecordingFragment
. The result is NPE while I trying access from (onCreateView
method and evaluation
method) RecordingFragment
, but when I log inside (stopRecording
method) RecordingService
class the result is what I want you can see the picture below with the red border.
I see the another question from this. To be honest, I still don't understand And I don't use BroadCastReceiver. I am still confusing.
My question is: what is the solution for this problem?
Thank you
Upvotes: 0
Views: 112
Reputation: 58934
For this, you can use a BroadcastReceiver
in your Activity
.
Here is a great example I use to communicate continuously between Service
<> Activity
<> Fragment
using BroadcastReceivers
.
Here is another great example of communication between Service <> Activity. It uses Messenger
and IncomingHandler
.
I will make a quick example for your case.
This is your BroadcastReceiver for your Activity. It is going to receive your String:
//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;
In your onCreateView()
of the Fragment
bManager = LocalBroadcastManager.getInstance(getActivity());
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RECEIVE_PATH);
bManager.registerReceiver(bReceiver, intentFilter);
In your onDestroyView()
of the fragment, make sure you unregister the broadcastReceiver.
bManager.unregisterReceiver(bReceiver);
And finally, in your Service onStart()
, do this:
System.out.println("intent Received");
Intent RTReturn = new Intent(YourActivity.RECEIVE_PATH);
RTReturn.putExtra("path", yourPath);
LocalBroadcastManager.getInstance(this).sendBroadcast(RTReturn);
and your Fragment will receive the intent with that path in it as an extra.
Upvotes: 1