title: How to unlock my phone using my voice?
body: <p>i want my app to unlock my phone using my voice i already implemented a method that converts speech to text and i made a broadcast reciever to listen for "screen on event" to call my recognize method in main activity but when i say the password nothing happens. </p>
  
  <p><strong>this is my mainactivity class:-</strong></p>
  
  <p>public class MainActivity extends AppCompatActivity {</p>
  
  <pre><code>private final int REQ_CODE_SPEECH_OUTPUT = 143;
  private Button btnSave, btnTry;
  private ImageButton btnRecord;
  private TextView tvResult;
  private SharedPreferences userData;
  private String password;
  private Context context;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      context = this;
      btnSave = findViewById(R.id.btn_save);
      btnTry = findViewById(R.id.btn_try_again);
      tvResult = findViewById(R.id.tv_word_entered);
      btnRecord =findViewById(R.id.btn_record);
      btnTry.setVisibility(View.INVISIBLE);
      btnSave.setVisibility(View.INVISIBLE);
      tvResult.setVisibility(View.INVISIBLE);
  
  
  
  
      userData = this.getSharedPreferences(getString(R.string.shared_prefrence_name), MODE_PRIVATE);
  
      btnTry.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
              recognize('s' ,i);
          }
      });
  
  
  
      //register the receiver
      BroadcastReceiver receiver = new MYBROADCAST();
      IntentFilter filter = new IntentFilter();
      filter.addAction(Intent.ACTION_SCREEN_ON);
      registerReceiver(receiver,filter);
  
  
  }//end of onCreate
  
  //start listening to the user
  public void startRecording(View view) {
      //recognize the voice to be stored
      Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
      recognize('s' , i);
  } //end of startRecording
  
  public void recognize(char c , Intent intent) {
  
      //intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.EXTRA_LANGUAGE_MODEL);
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
  
      if(c == 'u')
          intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Say the password to unlock your device");
      else
          intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Say the password you want to add");
  
  
      try{
          startActivityForResult(intent,REQ_CODE_SPEECH_OUTPUT);
      }
      catch(ActivityNotFoundException e){
          Log.d("voice","error in taking speech to text");
      }
  
  } //end of on recognize
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode,Intent data){
  
      //speeach to Text is done here
      super.onActivityResult(requestCode,resultCode,data);
  
      switch (requestCode){
          case REQ_CODE_SPEECH_OUTPUT: {
              if(resultCode == RESULT_OK &amp;&amp; data != null){
                  ArrayList&lt;String&gt; voiceInText = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                  btnTry.setVisibility(View.VISIBLE);
                  btnSave.setVisibility(View.VISIBLE);
                  tvResult.setVisibility(View.VISIBLE);
                  btnRecord.setVisibility(View.INVISIBLE);
                  password = voiceInText.get(0);
                  tvResult.setText(voiceInText.get(0));
              }
              break;
          }
      }
  
  }// end of on Activity result
  
  
  //check user credentials
  public void CheckCredentials(Intent intent){
      int count = 0;
      for(int i = 0 ; i&lt; count;i++){
          recognize('u' , intent);  //app crashes here
          if( password == userData.getString("user_pass",null)){
              //unlock the phone
              KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
              final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
              kl.disableKeyguard();
              PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
              PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                      | PowerManager.ACQUIRE_CAUSES_WAKEUP
                      | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
              wakeLock.acquire();
          }
  
          else{
              count++;
          }
      }
  } // end of CheckCredentials
  
  
  //saving user Credentials
  public void SaveCredentials(View view) {
      //save the user cedintial in a shared by refrence
      SharedPreferences.Editor editor = userData.edit();
      editor.putString(getString(R.string.user_password), password);
      editor.commit();
      Toast.makeText(context, "Password Set", Toast.LENGTH_SHORT).show();
  }//end of save credintials
  </code></pre>
  
  <p>}//end of class MainActivity</p>
  
  <p><strong>and here is my broadcast reciever class :-</strong></p>
  
  <pre><code>public class MYBROADCAST extends BroadcastReceiver {
  
  private static final String TAG = "unlocked";
  private MainActivity instance;
  
  @Override
  public void onReceive(Context context, Intent intent) {
  
      instance = new MainActivity();
      if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
          Log.d(TAG, "Phone unlocked");
          //Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
          Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
          instance.CheckCredentials(i);
      }
  }
  </code></pre>
  
  <p>}</p>