Opening apps on buttons - java

Im trying to open. Google Search on my application. But the problem is. when I click the button. the COMPLETE ACTION USING windows is popping up instead of the google search.. search the net for more than an hour but it seems I cant find the solution.. here is my code..
#Override
public void onClick(View view) {
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(ComponentName.unflattenFromString("com.google.android.googlequicksearchbox"));
intent.addCategory("android.intent.category.LAUNCHER");
startActivity(intent);

This is due to Android's nature to allow users to make their own decisions. If, for instance, they have Bing installed and prefer it as a search engine over Google, they will have the option to select it. As far as I know, there is no way to open a specific app this way. If the user selects Google and makes it the default app for this action, it will auto-open in successive times, but they must make that conscious decision first.

have you probably tried to follow this guide..?
It describes well about what you might need..
hope it helps..

List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(intent, 0);
for(int i = 0; i < resolveInfoList.size(); i++) {
ResolveInfo info = resolveInfoList.get(i);
if (info.activityInfo.packageName.equals("com.google.android.googlequicksearchbox") {
// that's the one you want
}
}
I don't have Eclipse available right now to test it, but this should help you get there. Also check out the documentation for queryIntentActivities()
PS: I'm not sure about that packageName for google search

Try this. This code will search the meaning of value of query variable on google.
String url = "http://www.google.com/search?q=" + "Meaning of " +query;
Intent intentSearch = new Intent(Intent.ACTION_VIEW);
intentSearch.setData(Uri.parse(url));
startActivity(intentSearch);

Related

Android open external IMDB application from my app

I have a button in my application which opens the imdb application in the phone with a imdb id I received from https://developers.themoviedb.org/3/getting-started/introduction
But I couldnt find anyway(using intents) to make my app recognize the imdb app and open it and if imdb app do not exist then I want to open the web site. How can I accomplish this?
I think I may be able to point you in the right direction. Just to be sure, you seem to be using TMDB but wish to open in the IMDB app?
The code below is from the Android documentation.
It will start your intent if the package manager can find an app with the appropriate intent filter installed on your device. If multiple apps are able to open this intent then an app chooser should pop up, unless the user has previously set a default for this kind of URI.
Intent sendIntent = new Intent(Intent.ACTION_SEND);
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show the chooser dialog
Intent chooser = Intent.createChooser(sendIntent, title);
// Verify the original intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
If you add an else onto that then you can use a view intent like this :
Intent internetIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(movieUrl));
//Watch out here , There is a URI and Uri class!!!!
if (internetIntent.resolveActivity(getPackageManager()) != null){
startActivity(internetIntent);
}
I also found this (rather old) post about calling an explicit imdb uri
Imdb description
startActivity(android.intent.action.VIEW, imdb:///title/<titleID>);
// take note of the Uri scheme "imdb"
I hope this helps. If you post some more detail , code, links , I might be able to work through this with you.
If my answer is way off base then please be kind and set me right. We are all learning every day!
Good Luck.

How to add title and content after Facebook deprecation?

Here's some code that successfully shares a link to Facebook after a button click:
public void onClick(View view) {
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("A title")
.setContentDescription("Some description.")
.setContentUrl(Uri.parse("www.website.com"))
.build();
shareDialog.show(linkContent);
}
}
Using Android Studio, the ".setContentTitle" and ".setContentDescription" are deprecated, with a line through them. When I post the link, it is shared without the title and description. I assume this is because they are deprecated.
How could I add a title and description in? What were the deprecated terms replaced with? This isn't pre-filling a a post, and it wouldn't make sense for Facebook to get rid of these features completely. I have tried a few different links as the URL, none made a difference to this issue.
Many thanks in advance.
Edit: Please note that meta tags aren't an option, because if I were to link to an app in the Google Play Store, I can't control what tags the page has. I am looking to provide a title/description from the app, as was previously possible using the deprecated features mentioned.
I have found a suitable way around this, though not one that specifically replaces title and description. Another way to automatically add text to a post without pre-filling the user's text box is to use .setQuote().
For example with the code I provided above:
public void onClick(View view) {
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setQuote("This may be used to replace setTitle and setDescription.")
.setContentUrl(Uri.parse("www.website.com"))
.build();
shareDialog.show(linkContent);
}
}
If anyone knows a way to replace the deprecated functions properly, without such a different alternative like the one I just provided, please post it and I'll mark it as solved.
Many thanks.
You can use ShareOpenGraphContent that uses ShareOpenGraphAction and ShareOpenGraphObject.
Look at the code I answered in this question.
https://stackoverflow.com/a/46459350/4107421
That way you can add a title, description and even an image to your post.
It works for ShareDialog.show() but unfortunately on my experience it doesn't on MessageDialog.show()

Make a call via my app programatically [duplicate]

This question already has answers here:
How to make a phone call using intent in Android?
(21 answers)
Closed 7 years ago.
I'm building an app that lets you do various tasks and for this particular task. I want the user to be able to call someone. I've taken a look at many questions but all of them tell you to use intents and direct you to the phone app or instead they give you an answer where the users clicks a button and it calls the number assigned in the code.
I want the user to be able to type in a number in and EditText field and be able to hit the call button to call someone, how can I do something similar to this?
I'm not sure if this website is good for stuff like this as answers are hard to come by so I will keep looking around for answers, hopefully someone can answer my q and help me out a bit.
EDIT -
Super disappointed with all the notices and the downvote, really sucks however I appreciate the answers provided. But none helped lol.
So here is how I managed to do what I had asked. I create a onClick event and defined the two main objects with the objects and IDS above it (The EditText and the Button). In the onClick method I got the code to check and see if anything has been entered in the textfield (EditText), it detects with the following code -
(!TextUtils.isEmpty(txtPhone)) {
Uri uri = Uri.parse("tel:" + txtPhone);
If numbers have been typed in, it will run this intent and pull the string from the EditText object.
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
if nothing is in field it will show a toast.
Check below for my method. Hopefully someone in my position will benefit from this and wont have to get downvoted for no particular reason.
// Call button
ImageButton call = (ImageButton) findViewById(R.id.call_button);
final EditText txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
call.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String txtPhone = txtPhoneNo.getText().toString();
if (!TextUtils.isEmpty(txtPhone)) {
Uri uri = Uri.parse("tel:" + txtPhone);
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),
"Please enter recipients number", Toast.LENGTH_LONG)
.show();
}
}
});
Looks like I will be using YouTube for stuff like this now, StackOverFlow is supposed to be about helping new users. Not downvoting their questions and repeatedly labelling their posts as duplicates and already answered.
You should check this : how to make phone call using intent in android? and the accepted answer.
I don't know whether you have searched or not, I found this solution as first link in my search and its pretty much what you want to do (i.e. Call Directly from your app).
Try this
handle this on your call button click
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+ editText.getText.toString()));
startActivity(callIntent);
add <uses-permission android:name="android.permission.CALL_PHONE" /> in Manifest

Get Chosen App from Intent.createChooser

I am trying to capture the result of Intent.createChooser to know which app a user selected for sharing.
I know there have been a lot of posts related to this:
How to know which application the user chose when using an intent chooser?
https://stackoverflow.com/questions/6137592/how-to-know-the-action-choosed-in-a-intent-createchooser?rq=1
How to get the user selection from startActivityForResult(Intent.createChooser(fileIntent, "Open file using..."), APP_PICKED);?
Capturing and intercepting ACTION_SEND intents on Android
but these posts are somewhat old, and I am hoping that there might be some new developments.
I am trying to implement a share action without having it be present in the menu. The closest solution to what I want is provided by ClickClickClack who suggest implementing a custom app chooser, but that seems heavy handed. Plus, it seems like there might be some Android hooks to get the chosen app, like the ActivityChooserModel.OnChooseActivityListener.
I have the following code in my MainActivity, but the onShareTargetSelected method is never getting called.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, shareMessage());
sendIntent.setType("text/plain");
Intent intent = Intent.createChooser(sendIntent, getResources().getText(R.string.share_prompt));
ShareActionProvider sap = new ShareActionProvider(this);
sap.setShareIntent(sendIntent);
sap.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener() {
#Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
System.out.println("Success!!");
return false;
}
});
startActivityForResult(intent, 1);
As of API level 22 it is now actually possible. In Android 5.1 a method (createChooser (Intent target, CharSequence title, IntentSender sender)) was added that allows for receiving the results of the user's choice. When you provide an IntentSender to createChooser, the sender will be notified by the chooser dialog with the ComponentName chosen by the user. It will be supplied in the extra named EXTRA_CHOSEN_COMPONENT int the IntentSender that is notified.
I am trying to capture the result of Intent.createChooser to know which app a user selected for sharing.
That is not possible.
Other "choosing" solutions, like ShareActionProvider, may offer more. I have not examined the Intent handed to onShareTargetSelected() to see if it contains the ComponentName of the chosen target, though the docs suggest that it should.
And, if for some reason it does not, you are welcome to try to fork ShareActionProvider to add the hooks you want.
The reason why createChooser() cannot be handled this way is simply because the "choosing" is being done by a separate process from yours.
I have the following code in my MainActivity, but the onShareTargetSelected method is never getting called.
ShareActionProvider goes in the action bar. You cannot just create an instance, call a couple of setters, and expect something to happen.

Trying to pass an int between two classes on Android platform

I'm trying to pass an int between two classes using a bundle. I have looked online and it seems like it should be easy to do, its just not working for me :( any help is much appreciated! I am trying to pass the int life from one page to another in a simple game.
here is the code for the first page where I am passing it from
int life = 5;
....
if(input.equalsIgnoreCase(teststr))
{
Intent i = new Intent("com.name.PAGETWO");
i.putExtra("com.name.PAGETWO.life", life);
startActivity(i);
mpButtonClick.stop();
}
And on the second page, trying to receive the int
Intent i = new Intent("com.name.PAGETWO");
Bundle extras = i.getExtras();
int temp2 = extras.getInt("life");
int life = temp2;
when i try to run it, it tries to go on to the second page and then just says the application has unexpectedly stopped, but when i comment out the code it continues running as normal, so i know i am definitely putting in the code here wrong somewhere. thanks in advance!
Why would a brand new intent object contain the int you've passed? In the receiver activity, use getIntent() to retrieve the intent that the activity was started with. That intent would contain your extras.
You're creating a brand new Intent on the second page, which of course doesn't have the bundle. Instead, you need to retrieve the calling Intent using getIntent().
Here's how your second page's code could look:
int life = getIntent().getExtras().getInt("life");
EDIT: Looking at your code again, make sure the key name of the extra is consistent on both ends. To be sure, I usually use a final variable for this. So in your second activity (let's say it's ActivityTwo), declare this at the top (outside of onCreate()):
final static public String EXTRA_LIFE = "life";
Then in the calling activity:
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra(ActivityTwo.EXTRA_LIFE, life);
startActivity(i);
(You may have also been constructing your Intent incorrectly; compare my example).
And in the second activity, inside of onCreate():
int life = getIntent().getIntExtra(EXTRA_LIFE, 0);
(You can replace 0 with what you want the default value to be if the extra doesn't exist.)
That way you don't have to worry about typos and can rely on Eclipse's suggestions to make sure you're consistent.

Categories