APP

 Reciever BackGroundListening.java

package com.assist;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.util.Log;

import java.util.ArrayList;
import java.util.Locale;

public class BackGroundListening extends BroadcastReceiver{


private static final int REQUEST_CODE_SPEECH_INPUT = 1000;
private TextToSpeech mTextToSpeech;
SpeechRecognizer mSpeechRecognizer;
Intent mSpeechRecognizerIntent;
String FinalRecognizedText;

@Override
public void onReceive(Context context, Intent intent2) {

mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(context);

mTextToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS)
{
int result = mTextToSpeech.setLanguage(Locale.ENGLISH);

if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
{
Log.e("TTS", "Language not Supported");
}
}
else
{
Log.e("TTS", "INITIALIZATION FAILED");
}
}
});

while (true){
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
Listen();
}
}

private void Speak(String speech){
mTextToSpeech.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}

public void Listen(){

mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {

}

@Override
public void onBeginningOfSpeech() {

}

@Override
public void onRmsChanged(float rmsdB) {

}

@Override
public void onBufferReceived(byte[] buffer) {

}

@Override
public void onEndOfSpeech() {

}

@Override
public void onError(int error) {

}

@Override
public void onResults(Bundle results) {
ArrayList<String> recognizedText = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

if(recognizedText!=null){
FinalRecognizedText = (recognizedText.get(0)).toLowerCase();

if(FinalRecognizedText.contains("hello"))
{
Speak("Hello sir");
}

}
}

@Override
public void onPartialResults(Bundle partialResults) {

}

@Override
public void onEvent(int eventType, Bundle params) {

}
});
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.assist">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".BackGroundListening"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="BackGroundListening"/>
</intent-filter>
</receiver>


</application>

</manifest>

Main Activity

package com.assist;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import static android.widget.Toast.LENGTH_SHORT;

public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_SPEECH_INPUT = 1000;
private TextToSpeech mTextToSpeech;
SpeechRecognizer mSpeechRcognizer;
Intent mSpeechRecognizerIntent;
String FinalRecognizedText;

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

checkPermission();

Intent intent1 = new Intent(this, BackGroundListening.class);
intent1.setAction("BackGroundListening");

PendingIntent pendingIntent;
pendingIntent = PendingIntent.getBroadcast(this, 0,intent1,0);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,0,10,pendingIntent);

finish();

}
private void checkPermission() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if(!(ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED))
{
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:"+ getPackageName()));
startActivity(intent);
finish();
}
}
}


}


Comments

Popular posts from this blog

PyCharm Project

Android Eclipse Project L.O.R.A