Application "blinks" when speechRecognition is restarted - java

I'm developing an application that among other things , lets user interact with it by voice recognition (Speech to Text).For example user speaks the word "contacts" and the application can recognize and proceed to use logic to it to do something (i.e get the list of contacts and present them).
Now I have used a custom speech Recognizer , and not the one Google provides with the alert dialog.
I also have an animated gradient as a background with some colors.
From the RecognitionListener we get either an error as a response or we get results from the method onResults(Bundle results).In both cases after the response i need to restart the listener in order for the user to continue to interact with the application. However, when my speechRecognition is destroyed and created again, my application "blinks"(very quick black screen, but then the Ui is returned to normal).
I know that the problem is happening because i try to destroy my speechListener and then i re-create it(By calling the function restartSpeechListener), but i cannot seem to find a way to solve this "blinking".
Below is my code for the speechRecognizer initiation and restart :
public void startRecognising() {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mySpeechRecogniser listener = new mySpeechRecogniser();
Intent mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
"el");
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
mSpeechRecognizer.setRecognitionListener(listener);
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
startActivity(mSpeechRecognizerIntent);
}
// Restart speech recogniser
public void restartSpeechListener() {
mSpeechRecognizer.stopListening();
mSpeechRecognizer.destroy();
startRecognising();
}
Any suggestions or help would be very helpful !

Related

How to trigger KeygaurdManager to take fingerprint programmatically

As soon as I receive a push notification from my app I want to trigger the KeyguardManager to launch the fingerprint/pass code screen to open the phone from lock screen so that person can enter the phone and unlock the device.
I want to trigger this programmatically similar to when we click on any notification from lock screen we get the fingerprint/pass-code screen.
I did a lot of RnD but didn't find any solution, this is one of the challenging use case task given to me in class, I have been exploring a lot from quite few weeks with no success at all.
Did tried Broadcast receiver with BiometricManager and many things with no success, any lead will be very helpful.
As soon as you receive push message, onNotificationReceived() (or some other method if you use some 3rd party libs) method gets called as below. from there, you can launch your Main screen where you have written biometric/unlocking code.
class MyReceiver : PushReceiver {
override fun onNotificationReceived(message: Message) : Boolean {
//Launch your MainActivity where you can show Unlock screen.
return super.onNotificationReceived(message);
}
}

How to hide Calling UI and make calls with custom UI?

I have created a custom ui and making a call using telecom manager for direct dialing. But every time Device default Calling UI is opening.
Here is some code for making calls directly...
TelecomManager telecomManager=(TelecomManager)getSystemService(Context.TELECOM_SERVICE);
Uri uri = Uri.fromParts("tel", phonenumber, null);
Bundle extras = new Bundle();
extras.putBoolean(TelecomManager.METADATA_IN_CALL_SERVICE_UI, true);
extras.putBoolean(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,true);
try {
if (telecomManager!=null) {
telecomManager.showInCallScreen(true);
}
}catch(SecurityException incoming){
incoming.printStackTrace();
}
try {
if (telecomManager!=null) {
telecomManager.placeCall(uri, extras);
}
}catch(SecurityException unlikely){
unlikely.printStackTrace();
}
is there a way to hide the default calling ui and use our own calling ui.?
Any help will be appreciated.
See the API docs on how to use the placeCall API. METADATA_IN_CALL_SERVICE_UI should not be passed to placeCall, and EXTRA_PHONE_ACCOUNT_HANDLE is not a boolean extra. You need to pass in a PhoneAccountHandle. However, if you're just trying to place a regular mobile call, just omit that extra as the system will pick the right one.
showInCallScreen should also only really be used by the Dialer app to bring its UI to the foreground; its not going to have any impact in your case.
If you want to use your Dialer app as the in-call UI, you need to change it in the system settings.
System Settings --> Apps & Notifications --> Advanced --> Default Apps --> Phone App. Choose your app there.
To be a default phone app you need to handle the DIAL intent and implement the InCallService API.

Prevent app from running on two instances

I just recently added the capability of my app to check for updates on our local server (this app is not published in the Google Play store. It's going to be used internally and there is no internet connection where it will be used. Don't ask. That's how it is :) ). I keep track of updates by checking a certain table in SQL Server and if my app's version is lower than what is indicated in this table, I download the new APK from an internal website (LAN only) then install the APK. I also have another application in the device that listens for PACKAGE_ADDED broadcasts. I can capture the broadcast successfully.
The problem is, after installation, the broadcast receiver starts the app by calling the following.
public class PackageInstalledBroadcastReceiver extends BroadcastReceiver {
private final String MY_PACKAGE_NAME = "com.company.packagename";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
String packageName = intent.getData().getSchemeSpecificPart();
if (packageName.equalsIgnoreCase(MY_PACKAGE_NAME)) {
Intent i = new Intent();
i.setClassName(MY_PACKAGE_NAME, MY_PACKAGE_NAME + ".LoginActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
}
But it takes a long time for the app to start. The user might think that nothing is happening, so he/she can start the app manually by clicking the app's icon. If the user clicks the app's icon, the app starts immediately. After a while, the activity that the broadcast receiver started is also opened. So now, I end up with two instances of my app in the same activity (LoginActivity). I can say that this is the case, because if I press the Back key on the device from the LoginActivity, I still end up in another LoginActivity and if I press the Back key again, I end up on the device's desktop.
You have two problem in your question:
The fist, why your BroadcastReceiver take a long time to start your activity.
It have not enough information for stoving this.
The second your want to make your activity have a single instance.
Android provide a way to do that:
Step one: Visit your application androidmanifest file
Step two: Find your activity declaration.
Step there: Add the following property android:launchMode = "singleInstance"
The reference here:
P/s: If you could provide my some more information of your fist problem. Please create a new question. Hope you resolve it.

onStartListening when is it called?

I'm working on an android application which is basically a service to convert speech in to text as a background process, I'm getting help from How to register a custom speech recognition service? but I'm unable to know when onStartListening() is called.
Signature:
protected void onStartListening(Intent recognizerIntent, Callback listener)
Can any one can help or help me add more statements in the code given in that link so that I can recognize the speech in to text and show it in the toast?
In general, there are 2 kinds of apps:
apps that direct the user to convert audio into text, but don't actually implement the speech recognition part but just do the UI part (microphone button, VU visualizer) (let us call them app1);
apps that actually convert audio into text, either on the device or by calling a cloud service, and to make themselves available to app1, they extend RecognitionService (let us call them app2).
Since you mention "toast" (i.e. a UI element), I guess you want to implement app1?
app1 needs to do the following:
Intent intent = ...
SpeechRecognizer recognizer = ...
recognizer.setRecognitionListener(new RecognitionListener() { ... });
recognizer.startListening(intent)
Based on the specified intent and taking into account the user settings, Android finds a suitable app2 to service app1.
app2 extends the abstract class RecognitionService, i.e. it needs to implement a bunch of methods, among them onStartListening. The latter is called by Android when app1 calls startListening.
As the author of app1 you just need to provide the input details to the recognizer using the various intent extras, provide the handling of the output of the recognizer using the methods of the interface RecognitionListener, and start the app2 using startListening.

Starting an update checker thread, checking for new entries in a database

I need some suggestions for approaches to take...
Here's some background info:
Right now I have an Android app and a separate java program running on my server.
The java program continuously go out and gets information from different sites and stores them in 14 different entries in an SQL database on the server.
The Android app then queries the databases to retrieve the info to be displayed.
My goal:
I need suggestions on how to have the app handle checking for updates from the database, and then letting the user know that there is new information.
My first thought is that maybe I need to start a separate thread that queries the database for a time modified. Then if it finds updates, it would pop up on the screen that there is new information.
I'm not too well educated with the way threads or services work, so I guess I'm looking for how to implement this, or whether there is a completely different way to go about update checking that would be better.
Thanks in advance, I appreciate any feedback, input, or suggestions.
Hi Ryan I have also implemented a similar thing in my android app and surprisingly I also had 14 tables in my PostgreSQL Server. First of all, you would want to poll the server periodically even when the app is not in the foreground. For that you need to run a background Service - here you will have to manually create a thread in the service, because Service by default runs on the UI thread OR use an IntentService - you don't have to create a separate thread. Whatever code you write in the intent service will be handled in a different thread automatically
Now you have to make this service execute periodically. For that use an AlarmManager and use the setRepeating()function. In the arguments you have to give a PendingIntent to your Service or IntentService. But don't use an alarm manager if you are going to poll the server for every less than 1 minute. Because the battery will be wasted a lot.
Here is some code that might give you an idea :
function setalarm()
{
Intent intent = new Intent(getBaseContext(), Intent_Service.class);
PendingIntent sender = PendingIntent.getBroadcast(getBaseContext(), 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Random randomGenerator = new Random();
long interval=60000; //1 minute in milliseconds
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(),interval,sender);
}
This is Intent_Service of type IntentService :
public class BackService extends IntentService
{
Context context=this;
//public Timer t=null;
public BackService()
{
super("myintentservice");
}
#Override
protected void onHandleIntent(Intent intent)
{
try
{
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
//..CPU will remain on during this section..
//make our network connections, poll the server and retrive updates
//Provide a notification if you want
wl.release();//Release the powerlock
}
}
}
But if you want instantaneous updates, then use Google Cloud Messaging Services. To know more about how it works see this
Hope this helps you.

Categories

Resources