Pallas
Pallas

Reputation: 1579

Opening a link in the browser?

I have been trying to use an intent to open a link to a kml file using the browser. That way when it goes to the link it will download and open the file in Google Maps (or Google Earth). However, when I click on it in the emulator nothing seems to happen. Any ideas?

package shc_BalloonSat.namespace;

import android.content.Intent;
import android.net.Uri;

public class dl_viewKML
{
    void downloadFile()
    {
        String encodedURL = "http://" + "www.wktechnologies.com/shc_android_app/data.kml";

        Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(encodedURL));
        startActivity(webIntent);

    }

    private void startActivity(Intent webIntent)
    {
        // TODO Auto-generated method stub

    }
}

Eclipse doesn't show up any problems and it doesn't show up anything in LogCat.

Upvotes: 2

Views: 331

Answers (1)

user
user

Reputation: 87064

For the method startActivity() to start your Intent you have to either call it from the class or subclasses(like Activity, FragmentActivity) of Context or get a reference to the context and call it.

Because your class dl_viewKML isn't a subclass of Context you have to get a reference to the context. You can do this by adding a constructor with a Context parameter like in this:

package shc_BalloonSat.namespace;

import android.content.Intent;
import android.net.Uri;

public class dl_viewKML {
private Context ctx

public dl_viewKML(Context ctx) {
this.ctx = ctx;
}
    void downloadFile()
    {
        String encodedURL = "http://" + "www.wktechnologies.com/shc_android_app/data.kml";

        Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(encodedURL));
        ctx.startActivity(webIntent);

    }

}

In your Activity where you instantiate the dl_viewKML class you will do something like this:

dl_viewKML obj = new dl_viewKML(this);

or

dl_viewKML obj = new dl_viewKML(getApplicationContext());

Upvotes: 2

Related Questions