首页 > 解决方案 > 我需要 TextToSpeech 在后台工作,我该怎么做?

问题描述

我在我的 Android 应用程序中实现了一个非常基本的 TextToSpeech,当应用程序在前台运行时它似乎运行良好,但在后台运行时它不会播放。它一直等到应用程序返回前台并播放队列中的内容。如何为我的 GPS 跟踪应用程序实现这一点,该应用程序需要在显示屏关闭时提供跟踪反馈?提前致谢。

public class MainActivity extends AppCompatActivity implements LocationListener, GpsStatus.Listener {
    LocationManager locationManager;
    TextToSpeech textToVoice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Locale spanish = new Locale("es", "ES");

        textToVoice = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    textToVoice.setLanguage(spanish);
                }
            }
        });

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            if (locationManager != null) {
                locationManager.addGpsStatusListener(this);
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, this);
            }

            this.onLocationChanged(null);

        }else{
            this.finish();
        }

    }


    @Override
    public void onLocationChanged(Location location) {
        TextView view  = this.findViewById(R.id.dashView);
        TextView actual = this.findViewById(R.id.actualView);

        if(location == null) {
            view.setText("0.0 Km/h");
        } else {
            //location.setAccuracy(1);
            float speedInMs = location.getSpeed();

            double currentSpeed = ( speedInMs * 3.6 );
            DecimalFormat df = new DecimalFormat("#.##");
            df.format(currentSpeed);
            String speed = df.format(currentSpeed) + "Km/h";
            view.setText(speed);
            actual.setText(speedInMs + "m/s");


            textToVoice(speed);


            Log.d("GPS", "Location Change:" + location.getLongitude() + "," + location.getLatitude());
        }

    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onGpsStatusChanged(int i) {

    }

    public void textToVoice( String message){
        textToVoice.speak(message,
                TextToSpeech.QUEUE_FLUSH, null,null);
    }

    @Override
    protected void onDestroy() {
        if (textToVoice != null) {
            textToVoice.stop();
            textToVoice.shutdown();
        }
        if(locationManager !=null){
            locationManager.removeUpdates(this);
        }
        super.onDestroy();

    }
}

标签: androidtext-to-speechandroid-gps

解决方案


推荐阅读