Consider adding a `<query>` declaration to your manifest when calling this \method; - java

I am getting this warning on resolveActivity line while using implicit intents and I am unable to open any website because of that.
btnWeb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String v = et.getText().toString();
Uri uri = Uri.parse(v);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (intent.resolveActivity(getPackageManager())!=null){
startActivity(intent);
}
}
});

Get rid of resolveActivity(). Just call startActivity(), but do so in a try/catch, so you can catch the ActivityNotFoundException if there is no Web browser available.

Related

What is the prob with using Bundle extra to string?

Here is my code.
In the MainActivity.java, I called this method.
public void doViewRecord(View v){
startActivity(new Intent(this, ActivityView.class));
}
And in the SecondActivity (ActivityView.java).
[This is where I get my error]
tv_id = findViewById(R.id.tvId);
tv_name = findViewById(R.id.tvName);
tv_course = findViewById(R.id.tvCourse);
try{
extra = getIntent().getExtras();
id = extra.getString("id");
tv_id.setText(id);
}catch (Exception e){
e.printStackTrace();
displayError(e.getMessage());
}
The error I get is this.
I already tried the intent then bundle, but still it gives me an error.
You have to put the string early on the intent
public void doViewRecord(View v){
Intent intent = new Intent(this, ActivityView.class);
intent.putExtra("id", "your-id");
startActivity(intent);
}

Open a URL when click on ImageView on Android

I get the following error below
startActivity (android.content.intent) in activity cannot be applied to (intent)
For this code - I have tried many different things - is there anything I need to add to the manifest? Thank you so much for your help!
img = (ImageView) findViewById(R.id.Fb);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent browserintent = new intent("android.intent.action.VIEW", Uri.parse("www.yahoo.com"));
startActivity(browserintent);
}
}
Change your code to this
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com"));
startActivity(browserIntent);
You are missing a scheme http or https in your Uri:
Intent browserintent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com"));
startActivity(browserintent);
Try this code
Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.yahoo.com"));
startActivity(intent);

Launching a web-link from onClickListener

I'm trying my hand at developing an app for android, and the java is a bit confusing. The code I have so far is....
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButtonClick = (Button) findViewById(R.id.ButtonClick);
final String link = "htttp://www.stackoverflow.com";
ButtonClick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
URI uri;
try {
uri = new URI("http://www.stackoverflow.com");
} catch (URISyntaxException e) {
e.printStackTrace();
}
browserIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(browserIntent);
}
});}}
The issue with the way the code is currently written is when I try to assign the new intent (browserIntent), uri in what I understand to be the "data" part of that object. Android Studio says that uri cannot be converted to a URI, but it's declared as a URI only a couple of lines above!
I also tried to insert a string of text, initialized above as well, which looks like...
browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link);
This compiles fine, but when I actually click on the button it says that there is no activity to handle the intent.
So my questions in order are:
1) If I declare the URI uri variable earlier in the code, and the second property of the intent is looking for a URI, why is there an error when I simply feed it the variable uri?
2) In the second case, when I am trying to parse the string, where there is no activity to handle the intent, is this issue with ACTION_VIEW? It seems that its able to find the URI fine, however it can't actually go through with opening a browser. Perhaps
3) What is the simplest java api or java tutorial that would cover this issue that you have found or know of?
AS must be saying something like "uri is not initialized", because due to try-catch block that isn't guaranteed. So, we change the code as following:
EDIT:
Silly me, we need to use the Uri class instead of URI class as there is no constructor new Intent(String action, URI uri) but there is one for new Intent(String action, Uri uri). For differences between URI and Uri, visit here.
ButtonClick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri uri;
try {
uri = Uri.parse("http://www.stackoverflow.com");
browserIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(browserIntent);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
});}}

Different methods to open a website

I want to implement a button in my app which leads to a website. I found these two easy ways to solve it and I wonder if there is actually a functional difference between these two methods?
1st way:
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("http://google.com/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
2nd way:
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.yourURL.com"));
startActivity(intent);
}
});
According to the Intent documentation (http://developer.android.com/reference/android/content/Intent.html), the first and second parameters to the constructor merely set the Action and the Data URL (respectively).
So:
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
Would be functionally identical to:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
The only difference I see would be the inclusion of the category in the second block, which is documented here: http://developer.android.com/reference/android/content/Intent.html#addCategory%28java.lang.String%29

A link not to the mobile broswer, but to the applications

I have a link to some page (Instagram), when I click on the link, it takes me to the link and the username to that like in the mobile browser, what should I type in order to work it in Instagram application in the mobile? If it doesn't exist, it will go to play.google/instagram in the play store
This is the link but it's not what I want
blah.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.instagram.com/blahhhh"));
startActivity(browserIntent);
}
});
try {
Intent LaunchIntent =
getPackageManager().getLaunchIntentForPackage("com.instagram.android");
startActivity(LaunchIntent);
//Here you can add more information, such as the specific profile that you want to launch, in Intent format
}
catch (Exception myIntent) {
//TODO Auto-generated catch block
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.instagram.android"));
startActivity(browserIntent);
}

Categories