Android TTS sound Leaked Service Connection and speak deprecated - java

I am using android tts in my class to just say a message like so:
Problem: I get leaked service connection
public class WorkTimerNotification extends ActionBarActivity implements TextToSpeech.OnInitListener {
TextToSpeech tts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
//line1
tts = new TextToSpeech(this,this);
#Override
public void onInit(int status) {
//line2
tts.setLanguage(Locale.US);
//line3
tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null);
tts.shutdown();
}
#Override
protected void onDestroy() {
super.onDestroy();
tts.shutdown();
}
Line 1 I get leaked serviceConnection with this logcat:
WorkTimerNotification has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection#41aee290 that was originally bound here
android.app.ServiceConnectionLeaked
I press on this and it still goes back to the hash for some reason. I do not know why...
int speak(CharSequence, int, Bundle, String)
I have also tried: But it does not work either.
#Override
protected void onDestroy() {
//Close the Text to Speech Library
if(tts!= null) {
tts.stop();
ttsRest.shutdown();
Log.d(TAG, "TTS Destroyed");
}
super.onDestroy();
}

You can not call speak until after onInit is called, move your speak code to onInit
public class WorkTimerNotification extends ActionBarActivity implements TextToSpeech.OnInitListener {
TextToSpeech tts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
//line1
}
#Override
public void onInit(int status) {
//line2
tts.setLanguage(Locale.US);
//line3
tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null);
}
#Override
protected void onStart()
{
super.onStart();
tts = new TextToSpeech(this,this);
}
#Override
protected void onStop()
{
super.onStop();
tts.shutdown();
}

Related

moving the code to the activity java class (making two java classes into one)

I have codes in two classes
First class is ExampleBroadcastReceiver:
public class ExampleBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
boolean noConnectivity = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false
);
if (noConnectivity) {
Toast.makeText(context, "Disconnected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Connected", Toast.LENGTH_SHORT).show();
}
}
}
}
Second class is MainActivity:
public class MainActivity extends AppCompatActivity {
ExampleBroadcastReceiver exampleBroadcastReceiver = new ExampleBroadcastReceiver();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(exampleBroadcastReceiver, filter);
}
#Override
protected void onStop() {
super.onStop();
unregisterReceiver(exampleBroadcastReceiver);
}
}
How can I make the two classes into one by passing the code from the ExampleBroadcastReceiver class to MainActivity? Is it possible? Please don't ask why. Thanks.
Use java interface to handle an event in MainActivity that occurs in ExampleBroadcastReceiver. This way you don't have to merge classes to share an event based data.
public class ExampleBroadcastReceiver extends BroadcastReceiver {
public interface ConnectivityMonitorCallback {
void onConnectivityChanged(boolean connectivity);
}
public ConnectivityMonitorCallback callback;
public ExampleBroadcastReceiver(#NonNull ConnectivityMonitorCallback eventCallback) {
callback = eventCallback;
}
#Override
public void onReceive(Context context, Intent intent) {
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
boolean noConnectivity = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false
);
callback.onConnectivityChanged(noConnectivity);
}
}
}
Finally in the MainActivity you handle the event.
public class MainActivity extends AppCompatActivity {
ExampleBroadcastReceiver exampleBroadcastReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Define event handling code here which occurs in ExampleBroadcastReceiver
exampleBroadcastReceiver = new ExampleBroadcastReceiver(new ExampleBroadcastReceiver.ConnectivityMonitorCallback {
#Override
void onConnectivityChanged(boolean connectivity) {
// Handle the event that occured in ExampleBroadcastReceiver
}
});
}
#Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(exampleBroadcastReceiver, filter);
}
#Override
protected void onStop() {
super.onStop();
unregisterReceiver(exampleBroadcastReceiver);
}
}

How to remove Manifest internal service error

I have declared my Service inside my Voice Activity's Voice.java that
public class Voice extends AppCompatActivity implements RecognitionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
startService(new Intent(Voice.this,task.class));
}
class task extends Service{
#Override
public void onCreate() {
//background code here
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
and my manifest showing
this error
Instantiatable attribute missing

android create ProgressDialog in constructor

I created Activity that includes ProgressDialog and its methods.
public class ProgressActivity extends AppCompatActivity implements Thread.UncaughtExceptionHandler {
public final ProgressDialog progressDialog;
public void dismissPD() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
public void setPDMessage(String msg) {
progressDialog.setMessage(msg);
}
public void showPD(String msg) {
ProgressDialog.show(this, "", msg);
}
#Override
public void uncaughtException(Thread thread, Throwable ex) {
ex.printStackTrace();
}
public ProgressActivity() {
progressDialog = new ProgressDialog(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
dismissPD();
}
}
Now I extend my new activity from it, but I get NullpointerException inside constructur on progressDialog = new ProgressDialog(this)
As I see, I have a wrong context when constructor runs. Is there a right approach to realize it in my way?
I solved the problem by overriding oncreate mothod of the main Activity:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = new ProgressDialog(this);
}

Background audio for one specific activity

I want to add my recorded audio in background of the activity in which I am explaining an example. Problem is that audio do not stop after ending the activity and it keeps on playing. I want audio only to be played when my activity is at front. Kindly put the service class in following code
public class exp extends Activity {
ImageView imview1,imview2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addexample);
imview2 = (ImageView) findViewById(R.id.imageView2);
imview2.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
imview2.setVisibility(View.VISIBLE);
}
}, 2000);
}
}
In onResume start your audio
#Override
protected void onResume() {
super.onResume();
//start your audio track here
}
Stop the same in onPause
#Override
protected void onPause() {
//Sop it over here
super.onPause();
}
Create the player instances in onCreate
public class exp extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addexample);
//player starting code goes here.
}
protected void onPause() {
//stop the music player here.
}
}
Try to stop your audio in onPause:
public class exp extends Activity {
ImageView imview1,imview2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addexample);
imview2 = (ImageView) findViewById(R.id.imageView2);
imview2.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
imview2.setVisibility(View.VISIBLE);
}
}, 2000);
}
#Override
public void onPause(){
super.onPause();
//Kill your audio
}
}
I think your solution is something like that. Try it and comment
Good look!

Android Studio Location API implementing class signature

Guys how Do I change the class signature to implement googleapiclient? If you click on image it shows the tutorial but doesn't show clearly what to do. I don't understand when it says implement it because I don't know how? Can anyone help?
Here is my code:
public class MainActivity extends ActionBarActivity {
//private LocationManager locationManager;
private Location currentLocation;
private TextView locationText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationText = (TextView) findViewById(R.id.textView1);
}
private void updateDisplay(){
if (currentLocation==null){
locationText.setText("determining your location...");
} else {
locationText.setText(
String.format("Your location:\n%2f, %.2f", currentLocation.getLatitude(),
currentLocation.getLongitude()));
}
}
private LocationListener mListener = new LocationListener(){
#Override
public void onLocationChanged(Location location){
currentLocation=location;
updateDisplay();
}
#Override
public void onProviderDisabled(String provider){
}
#Override
public void onProviderEnabled(String provider){
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras){
}
};
#Override
public void onResume(){
super.onResume();
//currentLocation = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
updateDisplay();
//locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0, 0, mListener);
}
#Override
public void onPause(){
super.onPause();
//locationManager.removeUpdates(mListener);
}
how Do I change the class signature to implement googleapiclient?
Add implements GoogleApiClient.ConnectionCallbacks to your class, and implement the methods that it requires.

Categories