change view of an Activity from another class

Asked

Viewed 302 times

0

I’m making an audio recording app and my Mediarecorder instance starts running from a service, so it runs even in the background, now I want to implement a timer to mark and save the recorded time, but how to activate the chronometer in my Service class and display the chronometer counter in my Activity? Or do you have a better way to start the clock? I chose not to leave running from Activity pq on Activity it would not mark the time if the app is in the background.

below follows my code: my service class

package com.example.administrador.myapplication;

import android.app.IntentService;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;

import java.io.IOException;

/**
 * Created by Administrador on 27/06/2017.
 */

public class TesteService extends Service{
    MediaRecorder mediaRecorder;
    PathFile pathFile;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        pathFile = new PathFile();
        String file = pathFile.generateFileName();

        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/"+file+".aac");
    }

    @Override
    public int onStartCommand(Intent intent,int flags, int startId) {
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
       return START_STICKY;
    }

    @Override
    public void onDestroy() {
        if(mediaRecorder != null){
            mediaRecorder.stop();
            mediaRecorder.release();

        }
        super.onDestroy();
    }

    public void onComplete(){
        if(mediaRecorder != null){
            mediaRecorder.stop();
            mediaRecorder.release();

        }
    }
}

and want to show a chronometer in the layout of this my Ragment:

package com.example.administrador.myapplication.fragments;


import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.administrador.myapplication.MainActivity;
import com.example.administrador.myapplication.R;
import com.example.administrador.myapplication.TesteService;

import java.io.IOException;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    MediaPlayer mediaPlayer;
    Button start,stop,play,stopPlay;
    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
         View rootView = inflater.inflate(R.layout.fragment_home, container, false);
pathFile = new PathFile();
    String file = pathFile.generateFileName();


        start = (Button) rootView.findViewById(R.id.start);
        stop = (Button) rootView.findViewById(R.id.stop);
        play = (Button) rootView.findViewById(R.id.play);
        stopPlay = (Button) rootView.findViewById(R.id.stopPlay);


        Toast.makeText(getActivity(), Environment.getExternalStorageDirectory().getAbsolutePath(), Toast.LENGTH_SHORT).show();

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getActivity(),TesteService.class);
                getActivity().startService(intent);
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(getActivity(), "Recording Complete", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getActivity(),TesteService.class);
                getActivity().stopService(intent);
            }
        });

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                mediaPlayer = new MediaPlayer();

                try{
                    mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath()
                            + "/"+file+".aac");
                    mediaPlayer.prepare();
                }catch (IOException e){
                    e.printStackTrace();
                }
                mediaPlayer.start();
                Toast.makeText(getActivity(), "Reproduzindo Musica", Toast.LENGTH_SHORT).show();
            }
        });

        stopPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if(mediaPlayer != null){
                    mediaPlayer.stop();
                    mediaPlayer.release();

                }
            }
        });
return rootView;
    }

}

I know that to add a chronometer just insert it in the.xml layout and in my Ragment create an instance of it, associate with findViewById(); and give the start and stop. but I wanted the chronometer to continue even in the background, so I thought I’d let my service take care of it, but how can I insert it into the Fragment view from the service?

  • David, ideal serial you show your code, what you have started and what you intend to do and where, if not the guys will vote to close the question, will negatively and will not explain you the reason. Welcome to the Sopt.

  • Ready, put my code, thank you for alerting me

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.