1

I am trying to open a URL in a browser using an intent.

My code is

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.onair99.com"));
startActivity(i);

This will result in an error on any browser I have tried since the browser tries to open something like this:

http://http//onair99.com//

As far as I have tried, I only have encountered the problem with this specific URL.

Does anybody know why?

Thanks.

  • try removing 'http://' from the origin url? – suitianshi Aug 22 '14 at 05:14
  • Then I get "ActivityNotFoundException" (AndroidRuntime(1427): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.somewebsite.com }) – user3514699 Aug 22 '14 at 05:20
  • I copy pasted your code and its working absolutely fine in my Samsung Galaxy Young. Should you change any browser settings itself ? – Vny Kumar Aug 22 '14 at 05:20
  • @suitianshi what in case if origin url has `https://` ?? – jaimin Aug 22 '14 at 05:21
  • @VnyKumar I have tried on different devices and the emulator as well. I always get the same error... – user3514699 Aug 22 '14 at 05:27
  • What is the exact url you are trying to reach. Does it start with `https`? because your code should work normally. – Sadegh Aug 22 '14 at 05:30
  • No it doesn't start with https. And it's not related to the url since I get the same error if I try to open `http://www.google.com`. – user3514699 Aug 22 '14 at 06:09
  • I updated my question. I have actually found out that this only happens when the specific url I am trying to open which is `www.onair99.com`. Anybody can try? – user3514699 Aug 22 '14 at 17:51
  • It is a website issue. I will have to hear the site administrator. Thanks you everybody. – user3514699 Aug 23 '14 at 13:54

2 Answers2

2

do as mentioned in this post

String url = "http://www.somewebsite.com";

In production level code, you may like to check if the url begins with http or https... Would be better to check

if (!url.startsWith("http://") && !url.startsWith("https://"))  
  url = "http://" + url;

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

Here's the documentation of Intent.ACTION_VIEW.

Community
  • 1
  • 1
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
  • Thanks, but this doesn't solve my problem. The address I'm feeding to the intent is a constant and I know it starts with http. – user3514699 Aug 22 '14 at 06:21
  • if you are sure then it wont go inside if block and instead will directly go to intent part. what issue are you getting when you do startactivity using this intent code? – Rajen Raiyarela Aug 22 '14 at 06:23
  • I updated my question. I have actually found out that this only happens when the specific url I am trying to open which is `www.onair99.com`. Anybody can try? – user3514699 Aug 22 '14 at 17:51
  • then it is possible issue of .htaccess file on server. in that it is all defined how a request is to be handled. It is no issue with your android code. i would suggest to try with http:// onair99.com. Kept space as it is converting it into href. – Rajen Raiyarela Aug 23 '14 at 05:23
  • I still get error if I use the space as you said. Now the browser tries to open `%20onair99.com` – user3514699 Aug 23 '14 at 12:39
  • It is a website issue. I will have to hear the site administrator. Thanks you everybody. – user3514699 Aug 23 '14 at 13:54
  • space was kept for writing purpose only ... in code you have to use without space. – Rajen Raiyarela Aug 25 '14 at 05:09
0

Try This Code

main.xml

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Go to link" />

    </LinearLayout>

MyAndroidAppActivity.java

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MyAndroidAppActivity extends Activity {

    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        addListenerOnButton();

    }

    public void addListenerOnButton() {

        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.onair99.com"));
                startActivity(browserIntent);

            }

        });

    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MyAndroidAppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Rami
  • 7,879
  • 12
  • 36
  • 66
ishu
  • 1,322
  • 1
  • 11
  • 15
  • Thanks, but this doesn't solve my problem. The address I'm feeding to the intent is a constant and I know it starts with http. – user3514699 Aug 22 '14 at 06:22
  • Hey!You got the solution or not? – ishu Aug 22 '14 at 07:17
  • No...I'm starting to think I'm screwing up somewherelse in my code. I have read everywhere on the internet that this is the correct way to start the intent. Changing address/browser/device doesn't help at all...so it must be something in my code... – user3514699 Aug 22 '14 at 07:20
  • then try to create new progect see i send one link download the example and see its working or not – ishu Aug 22 '14 at 07:26
  • I updated my question. I have actually found out that this only happens when the specific url I am trying to open which is `www.onair99.com`. Anybody can try? – user3514699 Aug 22 '14 at 17:52
  • hii i update my answer and i also confirm with my device and emulator after apply this code you still didn't get solution than you please check emulator/dvice internet connection available or not. – ishu Aug 23 '14 at 05:43
  • do you get the same problem opening that url? I can't try your code now. I'll do it as soon as I get home. Thanks – user3514699 Aug 23 '14 at 06:45
  • It is a website issue. I will have to hear the site administrator. Thanks you everybody. – user3514699 Aug 23 '14 at 13:52