Tag: invoke

Invoke the browser with raw HTML

On the BrowserField2 (BlackBerry OS 5 and OS 6) the BrowserField has method to load the content from the String HTML raw.

this is the code for the previous version of BlackBerry OS, how to invoke the browser with the raw HTML.

The mechanism for using the BlackBerry Browser to invoke the browser with raw HTML content is shown in the sample code below. The key aspect of the code is the section where you pass in the following string to the browser at the start of the data sequence:

data:text/html;base64,

/*
* RawHTMLBrowserSample.java
*
* © Research In Motion Limited, 2007
* Confidential and proprietary.
*/

import java.io.*;
import javax.microedition.io.*;
import net.rim.blackberry.api.browser.*;
import net.rim.device.api.io.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.util.*;

/**
* A simple example of how to invoke the BlackBerry Browser using
* raw HTML data.
*/
public class RawHTMLBrowserSample extends UiApplication
{
    public static void main( String[] args )
    {
        RawHTMLBrowserSample sample = new RawHTMLBrowserSample();
        sample.enterEventDispatcher();
    }

    /**
    * Create the splash screen and start off the networking
    * thread that will eventually invoke the browser.
    */
    public RawHTMLBrowserSample()
    {
        MainScreen splashScreen = new MainScreen();
        splashScreen.add( new RichTextField( "Please Wait..." ));
        pushScreen( splashScreen );
        MyThread thread = new MyThread();
        thread.start();
    }
    private static class MyThread extends Thread
    {
        public void run()
        {
            try {
            // Open the connection and the input stream. Select your own URL.
            HttpConnection connection = (HttpConnection)Connector.open( "http://www.blackberry.com:80" );
            InputStream input = connection.openInputStream();
            DataBuffer buffer = new DataBuffer();
            byte[] temp = new byte[ 1024 ];
            // Read in the web page to the DataBuffer.
            for( ;; ) {
                int bytesRead = input.read( temp );
                if( bytesRead == -1 ) {
                    break;
                }
                buffer.write( temp, 0, bytesRead );
            }
            input.close();
            connection.close();
            // Create an output stream that will be used by the Base64 code.
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            Base64OutputStream boutput = new Base64OutputStream( output );
            // Write out the special sequence which indicates to the browser
            // that it should treat this as HTML data in base64 format.
            output.write( "data:text/html;base64,".getBytes() );
            boutput.write( buffer.getArray() );
            boutput.flush();
            boutput.close();
            output.flush();
            output.close(); // Invoke the browser with the encoded HTML content.
            BrowserSession bSession = Browser.getDefaultSession();
            bSession.displayPage( output.toString() );
        } catch( IOException e ) {
            System.out.println( "IOException: " + e );
        }
    }
}
}

The browser uses the sequence to base64 decode the string following the comma and displays it in the browser. When displaying the page, images and other items do not appear because the browser cannot fetch them.

This article applies to BlackBerry® Device Software 4.2 and later.

[1] http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/796557/800332/800440/How_To_-_Invoke_the_browser_with_raw_HTML.html?nodeid=1313878&vernum=0?CPID=EMC-DM2007M-37


Google Maps on BlackBerry with Landmark

this article from the popular link [1].

basically, google maps on BlackBerry can be start with URL like this:

http://gmm/x?action=LOCN&a=@latlon:35.0000,-105,0000&title=something&description=something

this is the code snapshot:

int mh = CodeModuleManager.getModuleHandle("GoogleMaps");
if (mh == 0) {
     throw new ApplicationManagerException("GoogleMaps isn't installed");
}
URLEncodedPostData uepd = new URLEncodedPostData(null, false);
uepd.append("action","LOCN");
uepd.append("a", "@latlon:"+l.getLatitude()+","+l.getLongitude());
uepd.append("title", l.getName());
uepd.append("description", l.getDescription());
String[] args = { "http://gmm/x?"+uepd.toString() };
ApplicationDescriptor ad = CodeModuleManager.getApplicationDescriptors(mh)[0];
ApplicationDescriptor ad2 = new ApplicationDescriptor(ad, args);
ApplicationManager.getApplicationManager().runApplication(ad2, true);

here is more complete code from [2]:

import net.rim.blackberry.api.browser.URLEncodedPostData;
import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.ApplicationManagerException;
import net.rim.device.api.system.CodeModuleManager;
import net.rim.device.api.ui.component.Dialog;
public class Application extends net.rim.device.api.ui.UiApplication {
        public static void main(String[] args) {
                int mh = CodeModuleManager.getModuleHandle("GoogleMaps");
                if (mh == 0) {
                        Dialog.alert("GoogleMaps isn't installed");
            System.exit( 1 );
                }
//              URLEncodedPostData uepd = new URLEncodedPostData(null, false);
//              uepd.append("action","LOCN");
//              uepd.append("a", "@latlon:"+"35.0000"+","+"105,0000");
//              uepd.append("title", "something");
//              uepd.append("description", "something");
                String[] args1 = { "http://gmm/x?action=LOCN&a=@latlon:
40.03731412913736,116.34973964097298&title=something&description=something"        };
                ApplicationDescriptor ad =
CodeModuleManager.getApplicationDescriptors(mh)[0];
                ApplicationDescriptor ad2 = new ApplicationDescriptor(ad, args1);
                try {
                        ApplicationManager.getApplicationManager().runApplication(ad2,
true);
                } catch (ApplicationManagerException e) {
                        // TODO Auto-generated catch block
                        Dialog.alert("Debug ..."+e.getMessage());
            System.exit( 1 );
                        //e.printStackTrace();
                }
        }
        Application() {
        }
}

some one report using address instead of coordinates [3]:

I've been able to avoid the error in 3.0.2 by removing the "@latlon:" tag, as in:


uepd.append("a", l.getLatitude()+","+l.getLongitude());

The other option is to put in the actual address, as in:

uepd.append("a", "1600 Pennsylvania Avenue, Washington DC");

[1] http://www.blackberryforums.com/developer-forum/143263-heres-how-start-google-maps-landmark.html
[2] http://groups.google.com/group/google-maps-api-china/browse_thread/thread/49f72c8082f3caa0
[3] http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=20392


  • Copyright © 1996-2010 vaks.in. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress