문제

I've never developed for Android before, so please consider me 100% dumb when you answer :)

I would like to create an application launcher that will open the default web browser to a given url. In other words, I want to make an icon with my website logo, and when you click on it, it opens the site in your default web browser.

Could someone direct me towards a tutorial/documentation page to achieve this? Or if it's really simple, maybe show me some code here?

Thanks for your time!

P

도움이 되었습니까?

해결책

If I understand what you need correctly, you could just create a simple app with just 1 activity and stick this in the onCreate:

Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.yourwebsite.com"));  
startActivity(viewIntent);

And here are some resources on creating a simple app:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/HelloWorld.html

And here is some info on how you can set your app icon:

http://www.connorgarvey.com/blog/?p=97

다른 팁

I have written a tutorial for just this :=D

http://www.anddev.org/code-snippets-for-android-f33/button-to-open-web-browser-t48534.html

Modified version:

package com.blundell.twitterlink;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sendToTwitter();         // Open the browser
        finish();                // Close this launcher app
    }

    protected void sendToTwitter() {
        String url = "http://twitter.com/blundell_apps"; // You could have this at the top of the class as a constant, or pass it in as a method variable, if you wish to send to multiple websites
        Intent i = new Intent(Intent.ACTION_VIEW); // Create a new intent - stating you want to 'view something'
        i.setData(Uri.parse(url));  // Add the url data (allowing android to realise you want to open the browser)
        startActivity(i); // Go go go!
    }
}

One line answer

startActivity(new Intent("android.intent.action.VIEW", Uri.parse(url)));

Why do you want to create an application to do this? You can just create a shortcut directly on your home screen.

Here's what to do:
1. Go to the website in your browser
2. Add a bookmark for the site (menu, add bookmark)
3. Go to the home screen where you want the logo
4. Press and hold the screen, when the menu pops up select 'add shortcut'
5. Select 'bookmarks'
6. Find the bookmark you just created and click on it

You are done!!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top