Search using voice command - java

I got this simple problem whenever I say "search music click" its searching for 'search music click' too it's supposed to search my music file name click. I tried to put different variables but only boolean is the one that not causing an error.
if(voice_result.indexOf("search music") != -1){
voice_result.contains(voice_result)
String a = voice_result;
Intent intent = new Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, MediaStore.Audio.Media.ENTRY_CONTENT_TYPE);
intent.putExtra(SearchManager.QUERY, a);
startActivity(intent);}
the voice_result here is the process result of my voice and if ever I try to search a music from voice command it always search "search music risky" instead of "risky".

You should write a parser/filter for whatever returned from the voice recognition api first. For example, you say "search music risk":
Remove unnecessary or confusing strings like "search" "music" from the raw data. I suggest you use regex matcher and map the with a set of commands with values.
After filtering the raw data (now only has "risk"), use the result for next job

Related

Android direct shared

I am trying to share a link from my app with direct share. The share dialog must be like the image below with the most used contacts from messaging apps, like WhatsApp contacts.
This is the Intent structure which I am using for share the link:
Intent shareIntent = ShareCompat.IntentBuilder
.from(getActivity())
.setType("text/plain")
.setText(sTitle+ "\n" + urlPost)
.getIntent();
if (shareIntent.resolveActivity(
getActivity().getPackageManager()) != null)
startActivity(shareIntent);
And this is what my app shows:
Any idea how to achieve that?
You should use .createChooserIntent() instead of .getIntent()
Like this code below, you can use Intent.createChooser
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("file://" + filePath);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
You should use .createChooserIntent() instead of .getIntent()
Docs: This uses the ACTION_CHOOSER intent, which shows
an activity chooser, allowing the user to pick what they want to before proceeding. This can be used as an alternative to the standard activity picker that is displayed by the system when you try to start an activity with multiple possible matches, with these differences in behavior:
You can specify the title that will appear in the activity chooser.
The user does not have the option to make one of the matching activities a preferred activity, and all possible activities will
always be shown even if one of them is currently marked as the
preferred activity.
This action should be used when the user will naturally expect to
select an activity in order to proceed. An example if when not to use
it is when the user clicks on a "mailto:" link. They would naturally
expect to go directly to their mail app, so startActivity() should be
called directly: it will either launch the current preferred app, or
put up a dialog allowing the user to pick an app to use and optionally
marking that as preferred.
In contrast, if the user is selecting a menu item to send a picture
they are viewing to someone else, there are many different things they
may want to do at this point: send it through e-mail, upload it to a
web service, etc. In this case the CHOOSER action should be used, to
always present to the user a list of the things they can do, with a
nice title given by the caller such as "Send this photo with:".

Android Studio - URL string

So I'm pretty new to the whole Android Studio thing and I've been using the internet to help me with a lot of the things I am doing and needed help on something.
I'm not sure if it's possible to connect this to either a string or an SQL database but I have a Main Layouts with a bunch of buttons that allow me to click on them and choose what external player I would like to use to watch the video. In my MainActivity java class, this is how it finds the button.
case R.id.button3:
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("http://videoname.mp4"), "video/*");
startActivity(Intent.createChooser(intent, "Choose an External Player"));
break;"
I wanted to know if "http://videoname.mp4" url can be connected to like a string where I can always update or change the URL instead of manually going to find the URL in the MainActivity java and changing it. As of now I have to manually do it but a different way to do it would be helpful.
I'm sorry if it's all confusing, but if you know, please let me know as soon as.
Thank you.
you just need write your string URL :
open your directory folder /res/values/strings.xml -> write your string : <string name="yourStringName>yourStringURL</string>. to use your string do this
getContext().getString(R.string.yourStringName) in fragment
getString(R.string.yourStringName) in Activity
or you can write directly on your code, put your cursor on your string, then press key alt + enter choose extract string resource fill the resource name with your string name
wherever you need to use it, just do point 1 or 2. also in your Intent
hope this help you, never stop learning!
Add it to strings.xml in your project. That is the recommended way of using strings anyway.

Cannot play music from local filesystem, but can play from SD card

I am developing a test app that does nothing else than play music. The idea behind this app is that normally you have a music player that takes ages to load and has all those extra gadgets and gizmos that nobody, or at least not you, uses. This app only plays music, period. And it loads almost instantly.
But I got something funny. I need to find a way for the user to select a song from the filesystem, but I haven't got that working yet, so I am using a fixed song URI to play it. When I put this song on the local filesystem(i.e. /storage/emulated/0/Music/SongName.mp3), the app crashes upon pressing the play button. But when I put the song on the SD card(i.e. /storage/extSdCard/Music/SongName.mp3) it works fine.
Well, I got it to work, but I don't know how or why it works. Normally I would be surprised and not touch that piece of code again so that it keeps working, but this time I am learning programming on Android and I want to know why it works.
This is the code for playing from the SD card(works):
p = new MediaPlayer();
p.setAudioStreamType(AudioManager.STREAM_MUSIC);
p.setDataSource(getApplicationContext(), Uri.fromFile(new File("/storage/extSdCard/Tests/Take Back The Night.mp3")));
p.prepare();
p.start();
And this is the code for playing from the local fs(does not work):
p = new MediaPlayer();
p.setAudioStreamType(AudioManager.STREAM_MUSIC);
p.setDataSource(getApplicationContext(), Uri.fromFile(new File("/storage/emulated/0/Music/heybrother.mp3")));
p.prepare();
p.start();
As you can see, it's pretty much the same, so I concluded that there is probably something wrong in the URI. But I can't see any typos, and I retyped it multiple times. Is the URI malformed for this purpose?
I got it working!
First, you need to get the path of the music folder like this:
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
.getPath() + "/";
Then you can append the file name in the music folder:
String file = path + "ThatOneSong.mp3";
And then play it as normal. (Or you could do this in one long line but that's kind of cluttered.)

screen recorder with kitkat

I'm developing a simple utility to record the screen of device using the new function of kitkat 4.4. So, I've create a button with various edit text for the parameters such as bit rate, seconds and file name. I've this code.
StringBuilder builder;
builder = new StringBuilder("system/bin/screenrecorder");
When i retrieve the parameters do something like
builder.append(" --bit-rate ").append(editText.getText());
Finally I've the complete command to record, like
"system/bin/screenrecorder --size 720x1184 --bit-rate 5 --time-limit 5 /storage/emulated/0/MyVideos/example.mp4"
My question now is this:
How can i execute this command? In other words, what should I do now to start recording using the variable builder?

how to start a new activity in one page on clicking a button in other page

I have created an remote controller app in android. In the main page,there are few keys which on pressing sends a signal from the mobile. First of all it asks for a configuration file and parse the file and save the control options in a spinbox. When a particular key is pressed he corresponding control from the spinbox is selected and the signal is sent.
In next screen i would like to have only the keys which on pressed should select the control in the main screen and it should send the signal. In short i should be able to access all the elements in my main_screen.java.
In this you can access your keys in second screen by sending keys from first screen by click on button through this code
Intent in=new Intent(this,yournextActivity.class)
e.g:- my current class is hello.java and next class is Applet.java then through intent
Intent in=new Intent(hello.this,Applet.class)
to pass data to next class use this...
in.putExtras(key,value);
e.g:- my value is String s="Welcome" then i can pass this s to next class like this
in.putExtras("Yours",s);
key should be any text.....
on second class receive this String through this code
Intent in=getIntent();
String m=in.getStringExtras("Yours");
where m is receiving string and "Yours" is the key that you pass from first class...
if you want to pass data from one activity to another, you can do that with the help of intent
to pass data use putExtras()
Intent intent = new Intent(this, yourSecondActivity.class);
intent.putExtra("key", "Value");
startActivity(intent);
and to receive it in second activity use:
getIntent().getExtras().getString("key");
Note i am explaining above as assuming that i am passing data of string type ! so while receiving data, it depends on type of data you are receiving get that accordingly like i used geString("key");
you could pass things by using intents, but have you considered using fragments instead of multiple activities ?
it could even be better if you actually wish the app to work on large screens.

Categories