Android Open Source - AndroidTextToSpeech Main Activity






From Project

Back to project page AndroidTextToSpeech.

License

The source code is released under:

MIT License

If you think the Android project AndroidTextToSpeech listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.knoxguru.texttospeech;
/*from  ww w  . j av  a2s  . c  o  m*/
import com.knoxguru.texttospeech.Receivers.SMSReceiver;

import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {

  public static boolean SERVICE_STATUS;
  public static boolean AUTO_PLAY = false;
  public static final String PREFS_NAME = "ttsPrefsFile";
  SharedPreferences settings;

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

    View view = getWindow().getDecorView().findViewById(
        android.R.id.content);
    disableBroadcastReceiver(view);
    enableBroadcastReceiver(view);
    settings = getSharedPreferences(PREFS_NAME, 0);
    toggleAutoStop(settings.getBoolean("AUTO_PLAY", false));

    Button b = (Button) findViewById(R.id.end_btn);

    if (SERVICE_STATUS == false)
      b.setText("Start Service");

    b.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        Button b = (Button) v.findViewById(R.id.end_btn);
        if (b.getText().toString() == "End Service") {
          disableBroadcastReceiver(v);
          b.setText("Start Service");
          Toast.makeText(getApplicationContext(), "Service Stopped",
              Toast.LENGTH_SHORT).show();
        } else {
          enableBroadcastReceiver(v);
          b.setText("End Service");
          Toast.makeText(getApplicationContext(), "Service Started",
              Toast.LENGTH_SHORT).show();
        }
      }
    });

    CheckBox cb = (CheckBox) findViewById(R.id.btn_auto_play);
    cb.setChecked(settings.getBoolean("AUTO_PLAY", false));

    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

      @Override
      public void onCheckedChanged(CompoundButton buttonView,
          boolean isChecked) {
        toggleAutoStop(isChecked);
      }
    });
    
    CheckBox btBtn = (CheckBox) findViewById(R.id.disable_bt_btn);
    btBtn.setChecked(settings.getBoolean("BT_ON", false));

    btBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {

      @Override
      public void onCheckedChanged(CompoundButton buttonView,
          boolean isChecked) {
        toggleBluetooth(isChecked);
      }
    });
  }
  
  public void toggleBluetooth(boolean a) {
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("BT_ON", a);
    editor.commit();
  }

  public void toggleAutoStop(boolean a) {
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("AUTO_PLAY", a);
    editor.commit();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    return true;
  }

  public void enableBroadcastReceiver(View view) {
    SERVICE_STATUS = true;
    ComponentName receiver = new ComponentName(this, SMSReceiver.class);
    PackageManager pm = this.getPackageManager();

    pm.setComponentEnabledSetting(receiver,
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
        PackageManager.DONT_KILL_APP);
  }

  public void disableBroadcastReceiver(View view) {
    SERVICE_STATUS = false;
    try {
      ComponentName receiver = new ComponentName(this, SMSReceiver.class);
      PackageManager pm = this.getPackageManager();
      pm.setComponentEnabledSetting(receiver,
          PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
          PackageManager.DONT_KILL_APP);
    } catch (Exception e) {
      return;
    }
  }

}




Java Source Code List

com.knoxguru.texttospeech.MainActivity.java
com.knoxguru.texttospeech.ReadActivity.java
com.knoxguru.texttospeech.Receivers.SMSReceiver.java