Introduction
As mentioned in the Getting Started with the Mobile SDK for Android Guide, the Salesforce Mobile SDK is targeted at developers looking to create mobile applications that interact with Salesforce.com, Force.com, or Database.com. While the Getting Started article focused on the various aspects of building a native Android app, this article covers the use of the Mobile SDK to create a hybrid Android application using HTML5.
To do a quick recap, hybrid applications, use standard web technologies?typically HTML5, JavaScript and CSS. This write-once-run-anywhere approach to mobile development creates cross-platform mobile applications that work on multiple devices and platforms. While developers can create sophisticated apps with HTML5 and JavaScript alone, some vital limitations remain at the time of this writing, specifically session management, access to native device functionality like the camera, calendar and address book, etc. The hybrid version of the Mobile SDK (based on the industry leading PhoneGap implementation) makes it possible to embed HTML5 apps inside a thin native container, producing a hybrid application.
Prerequisites
The Mobile SDK only supports Android 2.2 and above. The Mobile SDK requires that you set up the target computer with the basic Android development environment. Specifically, the SDK requires:
This article also assumes that you have basic knowledge of developing HTML5, Mobile SDK and native Android applications. You can refer to the following links for further details.
Getting Started
This article assumes that you have already downloaded the Mobile SDK for Android from this GitHub repository.
The Mobile SDK for Android includes a hybrid app sample project which is present under \MobileSDK-Android\hybrid\SampleApp\VFConnector
Create a new project by importing the VFConnector directory using File ? Import ? General ? Existing project into your workspace. Double check the following before you move to the next step.
- Link your app references correctly to PhoneGap and Mobile SDK library.
- Clean the project using Project ? Clean, to ensure you start with a clean slate
Once you have completed the above steps, you should be able to run the VFConnector hybrid sample application. The Main Activity class for this project is SalesforceDroidGapActivity and when launched, it loads the following URL in a WebView "file:///android_asset/www/bootstrap.html";
Project Components
Let's briefly discuss the components of this hybrid app project.
VFConnectorApp.java
Every Hybrid Android project that uses the Mobile SDK must provide an Application class that extends the abstract com.salesforce.androidsdk.app.ForceApp class. For the VFConnector project, this is the VFConnectorApp.java class. The most important method in this class is the getMainActivityClass() method which returns the SalesforceDroidGapActivity class.
SalesforceRImpl.java
Each Android Hybrid project that uses the Mobile SDK must provide an implementation of the SalesforceR.java interface (which is what SalesforceRImpl.java does). This is a way for your Hybrid application to give the Mobile SDK references to some of the resources that the SDK requires at run-time (e.g., the layout to use for displaying the main Login Activity). In most cases, you don't have to modify the default SalesforceRImpl.java class that the template project includes. You should only ever have to modify the SalesforceRImpl.java class if you need to override some default behavior of the SDK (e.g., to customize the error message that displays when a Salesforce authentication fails).
SalesforceDroidGapActivity
This is your Application's Main activity class. For a Hybrid application built using PhoneGap, this class must extend the DroidGap class.
public class SalesforceDroidGapActivity extends DroidGap
This class contains the following logic:
- Two whitelist entries for Salesforce domain for authentication
- WebView for render the application as a mobile web app that would work on multiple android devices.
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //ensure we have a CookieSyncManager CookieSyncManager.createInstance(this); //ensure that we allow urls from all salesforce domains to be loaded this.addWhiteListEntry("force.com", true); this.addWhiteListEntry("salesforce.com", true); // Load bootstrap super.loadUrl("file:///android_asset/www/bootstrap.html"); } } The onCreate() method loads the with file bootstrap.html (present under the "assets/www" folder) into a Webview.
bootstrap.html
This is a HTML page that performs the initial Salesoforce authentication for your Hybrid application. This file contains the following logic:
- Salesforce authentication
- if authentication is successful, it redirects the user to the index page (or any other as configured in the bootconfig.js file).
- Error handling in case authentication fails.
The onDeviceReady() method illustrates how any Hybrid application would perform Salesforce authentication using the SalesforceOAuthPlugin JavaScript plugin.
function onDeviceReady() { logToConsole("onDeviceReady called: PhoneGap is ready."); // Authenticate via the Salesforce OAuth plugin. var oauthProperties = new OAuthProperties(remoteAccessConsumerKey, oauthRedirectURI, oauthScopes, autoRefreshOnForeground); SalesforceOAuthPlugin.authenticate(loginSuccess,loginFailure,oauthProperties); } bootconfig.js
This file contains the Salesforce oAuth 2.0 configuration details. Developer can modify this file and replace the values with the corresponding configuration values for their application. This file defines the following configuration constants:
- remoteAccessConsumerKey, The client ID (Consumer key) value specified for your remote access object that defines your application in Salesforce. You can get more detail here on how to configuring OAuth 2.0 Access for your Salesforce applications.
- oauthRedirectURI, represents Remote access callback url; optional to change.
- oauthScopes, set to api or version
- startPage, defines the startup page which the application will load after Salesforce authentication is successful
- autoRefreshOnForeground, determines whether the the container app should automatically refresh the oAuth session when the app comes into foreground
SalesforceOAuthPlugin.js
This plugin provides basic functions to perform Salesforce oAuth authentication using PhoneGap API. It provides a JavaScript implementation of the following:
- OAuthProperties object.
- Functions to authenticate, login and logout from Salesforce
forcetk.js
This library provides a convenient wrapper for the Force.com REST API, allowing JavaScript in Visualforce pages to use the API via the Ajax Proxy.
inline.js
This file contains the jQuery code to
- Fetch a device contact
- Fetch a SFDC contact
- Fetch a SFDC account
- Logout
- Reset
Summary
The Mobile SDK lets you develop native and hybrid iOS and Android applications that source data from Salesforce.com, Force.com, or Database.com. In this article, you learned how to use of the SDK to develop hybrid Android applications. You can also check the Mobile SDK Workbook for additional articles and tutorials on using the Mobile SDK.
Remember also that the Mobile SDK is an open source project and you can help enhance and improve the SDK by contributing to the project. You can also use GitHub's issue tracking feature to file bug/enhancement requests. Better yet, you can fix the issue and submit a Pull Request!
About the Authors
This article has been contributed by Gaurav Kheterpal and Manish Kumar from Metacube Software Private Limited. Gaurav Kheterpal is a Mobile and Web Evangelist at Metacube, he can be contacted via [1]. Manish Kumar is a seasoned force.com developer and he's reachable at [2]
the incredibles jon bon jovi dead new jersey plane crash ohio state kobe bryant wife bonjovi dead sam shepard
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.