[Q] Updating search queries sent to other applications via Intents - Java for Android App Development

I am currently developing an android app that must launch other applications (YouTube, etc) to perform search queries. I have succeeded in sending search queries to other applications via the following code:
Code:
searchIntent = new Intent(Intent.ACTION_SEARCH);
searchIntent.putExtra("query", searchPhrase);
searchIntent.setPackage(packageName); //Youtube package name, etc
startActivity(searchIntent);
Unfortunately, when the queried app is left running and another query is performed, it simply relaunches with the original query, failing to perform another search.
For example: I launch a search intent for YouTube with the name "puppies." I then background that instance of YouTube and launch another search intent with the name "kittens." Upon launching that second intent, the same instance of YouTube launches and continues to show "puppies" in the search bar.
I have tried using various flags (FLAG_ACTIVITY_NEW TASK, FLAG_ACTIVITY_CLEAR_TOP, and others) to no avail to solve this problem.
Does anyone know how I may go about resetting the search query in the launched activity so that a second search can be performed without having to kill the activity instance (back button or otherwise)?
Thanks in advance for your assistance!

Bump. Hopefully someone can provide some guidance!

Even at this point, a solution would still be appreciated!

Related

keeping a service and an activity in-sync

I'm working on a music player and I'm running my MediaPlayer object within a service so that I can run other stuff while the music is playing. For the most part this works. If I leave the activity for any reason (like hitting the "home" key), then go back, I BindService in OnCreate and this usually links me to the already running service and I can pick up where I left off. However, sometimes BindService launches a new service and the original becomes orphaned. Then I've got two songs playing on top of each other and I have to kill the whole process with a task killer to stop the orphaned service. I can't seem to figure out what set of conditions causes this so it's difficult to debug.
Any ideas?
After tons of searching, I found some informative posts over at stackoverflow.com. Seems that if you bind to a service using the default ContextWrapper, you can get a different context with a different instance of an activity. If you use the application context (from GetApplicationContext) for binding, it should always bind to the same service.
Thank you for posting the solution.

[Q] Android app auto starts again when device rotated

Hello all,
I've written a simple audio player app and testing it in Samsung Galaxy S.
It is a simple app with can create playlist and some buttons to play the mp3 file.
However, there's a funny bug in it.
After starting the app, whenever I rotate the phone, the running app will launch another instance of the app. If I rotate again, another instance (3rd) would be started. So u can hear 3 copies of the same audio being played simultaneously.
What could be wrong with it?
Is there some code which can prevent program re-entry?
Thanks.
It's not a bug, that's the way andorid OS works. You need to account for this in OnCreate for your activity. I assume you are spawning off the actual playing of the MP3 in a thread or service. You need to check if the service is already running in OnCreate and attach, else spawn a new one.
Hello Gene,
How do I check if an activity is already running?
I could not find the answer on the android developer page (the topic on application fundamentals).
And no, I'm not converting it to a service yet as this is my first app.
Haven't explore service yet.
Thanks.
I read on another article, a simple way to prevent this is to add overwrite the onConfigurationChanged function
@Override
public void onConfigurationChanged(Configuration newConfig) {
//ignore orientation change
super.onConfigurationChanged(newConfig);
}
and modify the androidmanifest.xml
<activity android:name="selectCategories" android:configChanges="orientation|keyboardHidden"></activity>
But it still launch multiple instance of the app.
Thanks.
This is a good question and should be in an FAQ somewhere.
As already mentioned, changing the display orientation basically restarts your app. Read the Android dev page on Activity lifecycle for more info.
A short answer for your question is: the Bundle parameter for onCreate() will be null when your app is first run. When your app is paused and restarted, that Bundle will be non-null. You can store data in that Bundle by overriding onSaveInstanceState(), then check for that data in onCreate(). It's a good idea to learn how to do this (save/read app data on pause/restart). Once you start testing apps by rotating the display at various times, you'll find a lot of them FC at unexpected places.
This is indeed a topic that keeps surprising people who are new to android (ahem, like me 4-5 months ago ).
The solutions above are perfect, however in certain situations there's another trick that might make your life much easier. I suspect it won't help you in this case, but it might help others who tackle the problem and see this thread.
Certain applications, mainly games, should be fixed in a single orientation. I.E. you won't be playing angry birds on portrait - that game is locked to landscape, as it should be. In order to lock your activity in a certain orientation you add this attribute to the manifest under the Activity tag. So a standard activity might look something like that, combined with the ignore tag from the previous posts:
Code:
<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden"
android:name="whatever">
The great thing about this combination is that your activity will not restart at all when the phone is rotated. Of course, for a standard activity you'd usually want to support both landscape and portrait, but if you need your app to be in a fixed orientation this is the way to go - no weird FCs or annoying bugs
Hi,
How do I check the bundle?
And also, what to do if the bundle is not null? Just return?
Thanks.
regards
r_p_ang said:
This is a good question and should be in an FAQ somewhere.
As already mentioned, changing the display orientation basically restarts your app. Read the Android dev page on Activity lifecycle for more info.
A short answer for your question is: the Bundle parameter for onCreate() will be null when your app is first run. When your app is paused and restarted, that Bundle will be non-null. You can store data in that Bundle by overriding onSaveInstanceState(), then check for that data in onCreate(). It's a good idea to learn how to do this (save/read app data on pause/restart). Once you start testing apps by rotating the display at various times, you'll find a lot of them FC at unexpected places.
Click to expand...
Click to collapse

Communication between Service & Activity

Hello
I want to create an Android App. In the last days i read a lot of Android API Documentations, Tutorials and "how do do's". Now I'm really confused, because on one hand, it's nice to have so many possibility, but on the other it confuses me, whats the best way to do it. So I played a little with Activity life-cycle.
Now I'm going to start to build my first 'real' App.
This App should download initial data from a Webservice, process these data with a database, download more data related to results from a database, save these data again to database, and make it accessible in the Acitvity.
So please correct me if I'm wrong from this point on:
I assumend that a Android Service is the best way, to process these Data in a background-worker-thread.
So far so good. So I read about the Android Handler which seems to be a pipeline, so Threads can work on the Handler message queue. I can Access the Service from within my MainActivity over the ServiceConnection.onServiceConnetcted where I recieve a simple Binder which gives me access to my Service. From there i can put Messages in the Message queue from the HandlerThread. But how I can tell my Acitivity from within the Service, that it has finished Processing and send the Data to it?
I read that I don't have to use AIDL since the Service is running in the same Process. But How can I do it then? I tried to call onBind() in the Service, hoping OnServiceConnected will be triggered in the Acitvity which initially Binds these Services, but it doesn't seem to work. I also tried to "hack" the Funktionality, by spending my Service a singleton member "MyMainActivity" with corresponing static setMainActivity(MyMainActivity activity), calling it in the ServiceConnector onServiceConnected, with the same result: Runtime Error
I even don't think I understand the functionality behind these HandlerThread.
will it loop infinitly waiting for HandlerMessages while draining the Battery, or is it on a wait() status since it recieves a message?
I read the Android API about Services but for me, it seems that they only describe to Access the Service from the Activity, and not the other way round.
If I call a method of my Service within my Activity over the ServiceBinder, which has a return value, but was started in another Thread in my Service, how will my Activity know that it processes finish? 'busy waiting' on a boolean member of my Service doesn't seem to be the way to go. If I do that, like I saw in a tutorial, I don't see a reason not to do the work in the Activity Thread itself.
I read also about an AsyncTask, but this doesn't seem to be practicable for me, because I have to do different work related to JSON Objects I get.
Please you expirienced Android guys: show me the way.
You probably don't need a full-fledged service if you just need a background thread. Services can run independent of an activity and reconnect, etc. If you don't intend for the thread to be stand-alone, then just use a class that implements Runnable. Since you seem to have a handle (no pun intended) on handlers, this won't be too hard. Just post a message to your handler from your thread to let the activity know what's going on.

Playing with Otter Intents and Book Shortcuts

Unlike most, I've found myself actually liking the content-oriented otter launcher on stock. But, being a geek, I'm never satisfied so I was playing around with GoLauncher and ADW to see if I could get the best of both worlds. This is what I've uncovered so far.
To launch a specific part of Otter, such as the book library, you need to send the following type of intent:
Code:
Action: com.amazon.kindle.otter.action.SHOW_BOOKS
Category: android.intent.category.HOME
The valid actions are:
Code:
com.amazon.kindle.otter.action.SHOW_BOOKS
com.amazon.kindle.otter.action.SHOW_NEWS
com.amazon.kindle.otter.action.SHOW_APPS
com.amazon.kindle.otter.action.SHOW_DOCS
com.amazon.kindle.otter.action.SHOW_MUSIC
com.amazon.kindle.otter.action.SHOW_VIDEO
com.amazon.kindle.otter.action.SHOW_WEB
If you want to open a specific book, then you send an intent like this:
Code:
Action: android.intent.action.VIEW
Data: kindle://book/?action=open&book_id=AMZNID0%2FB002WB0XW0%2F0%2F
All that you want to change in that data string is the escaped book_id field. '%2F' just means '/', so you can see it's really just 'AMZNID0/(Book ID)/0/'. You can extract the book's ID from any Amazon Kindle product page (go to Manage your Kindle on Amazon.com). So, the book ID in this example is: B002WB0XW0 and the URL of the product page is http://www.amazon.com/dp/B002WB0XW0 (plus some useless SEO keywords and tracking cruft I omitted).
Launching documents is very similar, but I assume each document's id is user specific. To see what it is, fire-up logcat ("adb logcat ActivityManager *:S") and look for a message like this:
Code:
D/ActivityManager( 1452): Starting: Intent { act=android.intent.action.VIEW dat=
kindle://book/?action=open&book_id=AMZNID0%2FAEMEN728GNT2MXUF3VMENDMEY4SDV5AZ%2F
4%2F flg=0x10000000 cmp=com.amazon.kindle/.UpgradePage } from pid 3749
As for how to launch intents, several launchers let you do this. Sadly, Amazon gutted Settings.apk, so we're left with no provider for android.intent.action.pick_activity (ditto for shortcuts and folders). Therefore, we need an app like QCustomShortcut that will allow us to construct a custom intent shortcut, then directly add the icon to the launcher without going through the activities or shortcut menus. Sadly, that's not listed as compatible with the Fire, so you have to install it on another Android device and sideload the APK (which is kept under /mnt/asec, not /data/app, BTW).
In the end, I decided to return to Otter, but figured I could save someone a bit of trouble or hopefully even spark some ideas by posting this. It's all pretty basic stuff, but finding/launching intents and what-not took me a bit to sort through this evening so I suppose there may be others on this neophytic ledge between being a newb and a guru that I keep finding myself on.
Hi izomiac,
I'm interested in what you said. I'm creating an android that can open any kind of documents, including ebooks.
Unfortunately I could not see the kind of information you show in my logcat.
You said :
Launching documents is very similar, but I assume each document's id is user specific.
Click to expand...
Click to collapse
Could you give me an example to how open an ebook not downloaded by the amazon kindle app?
Really thank you for your post, it helps me a lot.
I don't think the Kindle app can directly open Documents/Books that haven't been downloaded. When I just tried that the Kindle App (for ICS) stayed on the splash screen and never loaded anything. I suspect Amazon uses some sort of push service rather than an intent to start an automatic download.
For logcat, what I did was run the logcat command using ADB, then launch the document I wanted to see the ID for on the Kindle using the stock launcher.
izomiac said:
To launch a specific part of Otter, such as the book library, you need to send the following type of intent:
Code:
Action: com.amazon.kindle.otter.action.SHOW_BOOKS
Category: android.intent.category.HOME
Click to expand...
Click to collapse
Hello,
I know this is an old post, but I am trying to get this to work on my HDX. Looking at both the Otter manifest, and the logcat, it seems not a lot has changed (except that now you can install qCustomSHortcut).
However, when I try to create a shortcut to my books library, I get a permission denied error. When I test the shortcut, the output says that this
Code:
com.amazon.SHOW_CONTENT_LIBRARY
action is required. Any thoughts?
The direct book link works well. Thank you.
~Leko

[Q] Make SearchProvider public for Google Search

Hi,
I would like to have my SearchContentProvider to be used from Google Search as well, so my app users can also use Google Search and retrieve search results from my app.
I followed the instructions in this tutorial: developer.android.com/guide/topics/search/adding-custom-suggestions.html
but without success, also the example mentions there seems to be gone, i just found the old code on github, but its also not working.
i searched already and haven't found yet a working solutions.
did anybody have an idea or example what todo that you App Search Provider appears in Google Search -Phone Search - Settings, and will be used to deliver search results when a user uses google search ?
(I don't mean, app indexing, where you can index the pages a user already saw, i want to display search results which the user haven't seen before)
thanks for your help

Categories

Resources