Home > Net >  When I press the button I want it to tell me my location (text to speech), how is this done? Android
When I press the button I want it to tell me my location (text to speech), how is this done? Android

Time:09-06

I have a button and when I press it it show me in 2 TextView my location(in textview3 the X, and textview21 the Y) and I want to listen my location with text to speech. The problem is: The text to speech is not working right and it say my location in loop. It's read right the textviews but the text to speech say/repeats the Location all the time. It will stop the repeat when I close the program. I don't know how to fix it.

The code in MainActivity2 for Location

ImageButtonLoc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ActivityCompat.checkSelfPermission(MainActivity2.this, Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity2.this,
                            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                            REQ_LOC_CODE);

                } else {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, MainActivity2.this);
                    //locationManager.removeUpdates(MainActivity.this);
                }
                //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                //   0, 0, MainActivity.this);

                //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER., 0, 0 , MainActivity.this);

            }
        });
    }
    //energopoietai apo to kleisimou tou dangerous permissions
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQ_LOC_CODE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, MainActivity2.this);
        }
    }
 @Override
    public void onLocationChanged(@NonNull Location location) {
        //sintetagmenes sto text
        x=location.getLatitude();
        y=location.getLongitude();
        
        textView3.setText(String.format("Your current location  is:X=%.2f",x));
        textView21.setText(String.format(" and Y=%.2f",y));
    
        textspeech.speak("Your current location is:" "X=" String.format("%.2f",x) "," "\n" "Y=" String.format("%.2f",y));


    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(@NonNull String provider) {

    }

    @Override
    public void onProviderDisabled(@NonNull String provider) {

    }

And the activity:textspeech

public class textspeech {
    private TextToSpeech tts;
    TextToSpeech.OnInitListener initListener=new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status==TextToSpeech.SUCCESS){
                tts.setLanguage(Locale.US);
            }
        }
    };
    //prepei na ftiaksume constructor moni mas gt h mixani omilias dn
    // mporei na energopoithi moni ths h klasi prepei na energopoiithoi apo contex
    public  textspeech(Context context){
        tts=new TextToSpeech(context, initListener);
    }
    //methodos gia na tn kalume
    public void speak(String message){
        tts.speak(message,TextToSpeech.QUEUE_ADD, null,null);
    }

}

CodePudding user response:

As I understand it, you want to wait for a while before reading the second address after the first address is read, right? If you want to do this, you can read the first address and then wait for a while with the Handler and then start reading the second address.

new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            start Voice 2
                        }
                    }, 1000);

CodePudding user response:

Answer: locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, MainActivity2.this);

  • Related