I have a question about URL got from JSON details.
This is my code:
JSONObject currentEarthquake = couponCategoryArray.getJSONObject(i);
JSONObject properties = currentEarthquake.getJSONObject("campaign");
String name = properties.getString("name");
String promo_code = currentEarthquake.getString("promocode");
String goto_store = currentEarthquake.getString("goto_link");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(goto_store));
CouponCategory couponCategory = new CouponCategory(name, promo_code, goto_store);
couponcategory.add(couponCategory);
From JSON, I get 3 fields: name, promocode and goto_link.
goto_link is URL.
My intent is click the link on goto_link field to open the link in the browser.
I added an intent under goto_link String.
Some suggestion to code it correctly?
Add this.
Your URI starts with HTTP or HTTPS like this: http://www.google.com
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(goto_store));
startActivity(browserIntent);
public void openWebPage(String url) {
try {
Uri webpage = Uri.parse(url);
Intent myIntent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(myIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No application can handle this request. Please install a web browser or check your URL.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
How is you goto link formatted ?
anyways an answer from #MarkB solves it
string goto_link = "http://www.google.com";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(goto_link));
startActivity(browserIntent);
incase your link doesnt have the http:// part
string goto_link = "www.google.com";
if (!goto_link.startsWith("http://") && !goto_link.startsWith("https://"))
goto_link = "http://" + goto_link;
You can open a link in the browser like this:
yourButton.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(goto_store)
if (intent.resolveActivity(requireContext().packageManager) != null)
startActivity(intent)
}
Related
I am trying to open a web page from my Android app.
This is what I did:
successCallback = {
url-> runOnUiThread{
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("$url"))
startActivity(browserIntent)}
}
But it doesn't work. Any thoughts?
This the error message
Please check your URL is correct or not i.e URL contain proper link or value, then parse it.
successCallback = {
url-> runOnUiThread{
if (url!= null && !url.isEmpty()){
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("$url"))
startActivity(browserIntent)}}
successCallback = {
url-> runOnUiThread {
Intent browserIntent = new Intent (Intent.ACTION_VIEW,
Uri.parse ("YOUR URL") );
startActivity (browserIntent);
}
String url = yoururl;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Do you want to open the page within your app? You should use a Webview or chrome custom tabs https://developer.android.com/guide/webapps/webview
In my application I receive a URL inserted by the user. This URL can be - for example - xx.sd. Using any web browser, this URL is a valid URL, but when try to open it by intent, a crash happens: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=xx.sd }.
I check this URL is valid URL by using this
Patterns.WEB_URL.matcher(model.getTarget().getUrl()).matches()
and open intent by using this code
Intent i = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(model.getTarget().getUrl()));
itemView.getContext().startActivity(i);
I know i can solve this issue by append http or https before URL if not exist but if my URL start with another protocol like ftp or file and other protocols. Can any one help me to handle this issue.
As you said this issue is really related to not well-formatted URL.
You can check for the ACTION_VIEW intent for the URL. First, this resolveActivity function check is there any application exists which can load URL. This will resolve the crash issue.
public void openWebPage(String url) {
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}else{
//Page not found
}
}
OR, you can manage this by exception handling:
public void openWebPage(String url) {
try {
Uri webpage = Uri.parse(url);
Intent myIntent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(myIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No application can handle this request. Please install a web browser or check your URL.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
Add un try-catch and call-again, such as:
public boolean startOpenWebPage(String url) {
boolean result = false;
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
try {
startActivity(intent);
result = true;
}catch (Exception e){
if (url.startsWith("http://")){
startOpenWebPage(url.replace("http://","https://"));
}
}
return result;
}
I'm making a browser android application.
How do you determine if the url is a file? Like when you click a download button. Currently webView just loads and does nothing.
Do I need to check if the url ends with an extension and open it through intents? or is there another way?
You can do as following:
1) Check if the url is file:
if(URLUtil.isFileUrl(file)){
getExtention(url);
}
2) Get the extension:
public String getExtention(String url) {
String filenameArray[] = url.split("\\.");
String extension = filenameArray[filenameArray.length-1];
return extension;
}
Trigger intent according to the extension:
if(getExtention(url).equals("jpg")){
openGallery(url);
}
else if(getExtention(url).equals("pdf")){
openPDF(url);
}
3.a) OpenGallery() as ex:
public void openGallery(String url){
Uri uri = Uri.parse(url);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(photoUri, "image/*");
startActivity(intent);
}
3.b) Open Pdf
public void openPdf(String url){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
Hope this helps.
How can I open a url in the android web browser from my application on clicking a button.
I tried this :
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);
but I am getting exception:
but I got an Exception :
"No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com"
You are doing it fundamentally correct, you just need to include the full url.
String strURL="http://www.google.com";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);
Basically, the http is the protocol. It is the computer's way of trying to figure out how you want to open it. You might also be interested in https, if you want a secure connection over SSL.
Almost seems like this would be useful to read... from the page:
Uri uri = Uri.parse("http://androidbook.blogspot.com");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uri);
startActivity(launchBrowser);
There is a second way, involving the WebView, but that seems like it's not your question. Hope this helped.
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Try this:
String strURL = "http://www.google.com";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));
startActivity(myIntent);
As for the missing "http://" I'd just do something like this:
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
I was looking for some way to launch Twitter app and open a specified page from my application, without webview.
I found the solution for Facebook here:
Opening facebook app on specified profile page
I need something similar.
[EDIT]
I've just found a solution:
try {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("twitter://user?screen_name=[user_name]"));
startActivity(intent);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/#!/[user_name]")));
}
Based on fg.radigales answer, this is what I used to launch the app if possible, but fall back to the browser otherwise:
Intent intent = null;
try {
// get the Twitter app if possible
this.getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USERID"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch (Exception e) {
// no Twitter app, revert to browser
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/PROFILENAME"));
}
this.startActivity(intent);
UPDATE
Added intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); to fix an issue where twitter was opening inside my app instead of as a new activity.
This worked for me: twitter://user?user_id=id_num
Open page on Twitter app from other app using Android in 2 Steps:
1.Just paste the below code (on button click or anywhere you need)
Intent intent = null;
try{
// Get Twitter app
this.getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USER_ID"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch () {
// If no Twitter app found, open on browser
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/USERNAME"));
}
2.intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USER_ID"));
To get USER_ID just write username https://tweeterid.com/ and get Twitter User ID in there
Reference: https://solutionspirit.com/open-page-twitter-application-android/
My answer builds on top of the widely-accepted answers from fg.radigales and Harry.
If the user has Twitter installed but disabled (for example by using App Quarantine), this method will not work. The intent for the Twitter app will be selected but it will not be able to process it as it is disabled.
Instead of:
getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));
You can use the following to decide what to do:
PackageInfo info = getPackageManager().getPackageInfo("com.twitter.android", 0);
if(info.applicationInfo.enabled)
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));
else
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/wrkoutapp"));
Just try this code snippet. It will help you.
//Checking If the app is installed, according to the package name
Intent intent = new Intent();
intent.setType("text/plain");
intent.setAction(Intent.ACTION_SEND);
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : list)
{
String packageName = resolveInfo.activityInfo.packageName;
//In case that the app is installed, lunch it.
if (packageName != null && packageName.equals("com.twitter.android"))
{
try
{
String formattedTwitterAddress = "twitter://user/" ;
Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress));
long twitterId = <Here is the place for the twitter id>
browseTwitter.putExtra("user_id", twitterId);
startActivity(browseTwitter);
return;
}
catch (Exception e)
{
}
}
}
//If it gets here it means that the twitter app is not installed. Therefor, lunch the browser.
try
{
String twitterName = <Put the twitter name here>
String formattedTwitterAddress = "http://twitter.com/" + twitterName;
Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress));
startActivity(browseTwitter);
}
catch (Exception e)
{
}
For me this did the trick it opens Twitter app if you have it or goes to web browser:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/"+"USERID"));
startActivity(intent);
try {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("twitter://user?screen_name=[user_name]"));
startActivity(intent);
} catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/#!/[user_name]")));
}
This answer was posted as an edit to the question Open page in Twitter app from other app - Android by the OP jbc25 under CC BY-SA 3.0.