#Android Up And Running Script
This is the script used to create the 'Android Up And Running' demo video.
Note these instructions were written using Android Studio 1.0.2
- Open Android Studio (for the video open this ahead of time so you can close the last project you had open and start with a clean screen)
- You'll need the Android 19 SDK installed, so if you haven't done that already you should do it now. Tools > Android > SDK Manager. *Note this will take some time to download and install so pause the video if you need to do this.
- File > New Project
- Name application :
EngageSetup - Set Company Domain:
silverpop.com - Choose project location
- Click Next
- Make sure the box for 'Phone and Tablet' is checked
- Set Minimum SDK to 19 or lower > Next
- Select 'Blank Activity' (you can choose something different, but I'm picking this one as a very simple demo) > Next
- Name activity to your liking - I’m keeping the defaults
- Finish
- Turn on autoscroll to/from source (This step is optional, but it makes the demo nicer because you can see where the open files live in the project tree as we access them)
- Uncheck 'Compact Empty Middle Packages' (Also optional, but it makes it easier to see the directory structure)
- Switch the Project tool window from 'Android' to 'Project'
- The EngageSDK isn't in Maven yet so for now we'll manually add the dependency
- In the Project tool window, right click the
appfolder > New Directory > nameaars> Ok - Copy/paste the
engage-1.1.0.aarinto newaarsdirectory (you can either build this yourself or have Silverpop provide it for you) - Open
build.gradefrom Gradle tool window and add the following
repositories {
flatDir {
dirs 'aars'
}
}
dependencies {
// use this once maven support is added
//compile(group: 'com.silverpop', name: 'engage', version: '1.1.0', ext: 'aar')
compile 'com.silverpop:engage:1.1.0@aar'
}- Sync Gradle to pull in your new changes
- Right click the
engagedemopackage > New > Java Class - Name the new class
Application> Ok - Modify the
Applicationclass to extendEngageApplication
package com.silverpop.engagedemo;
import com.silverpop.engage.EngageApplication;
public class Application extends EngageApplication {
}- Open
AndroidManifest.xml - Add an attribute to the
applicationxml with the full class name of your newApplication.javaclass
android:name="com.silverpop.engagedemo.Application"- Oragnization Settings > Application Account Access > Add Application
- Name Application > Ok
- You'll see your new Client Id and Client Secret
- Add Account Access to setup your user. Once setup you'll get an email with your Refresh Token.
- Open
AndroidManifest.xml - In the
applicationxml block add your credentials
<meta-data android:name="ENGAGE_CLIENT_ID" android:value="02eb567b-3674-4c48-8418-dbf17e0194fc" />
<meta-data android:name="ENGAGE_CLIENT_SECRET_META" android:value="9c650c5b-bcb8-4eb3-bf0a-cc8ad9f41580" />
<meta-data android:name="ENGAGE_REFRESH_TOKEN" android:value="676476e8-2d1f-45f9-9460-a2489640f41a" />
<meta-data android:name="ENGAGE_HOST" android:value="https://apipilot.silverpop.com/" />- Note that you should never save your credentials in your application code and we are only doing this for demo purposes to get you up and running. Ideally you'd hide your credentials behind a webservice interface so app users can't decompile the code to reveal your tokens.
- Open
activity_main.xml - Click on the 'Hello World' text in the preview window
- Make sure you are on the 'Design' tab at the bottom
- In the Properties window on the right set the
idproperty toconnectionView - Switch to the Text view at the bottom
- Click through to the
strings.xmland replace the hello_world line with the following:
<string name="connection_status">Connection Status</string>- Go back to
activity_main.xml - Change the TextView to use the
connection_statusstring:
android:text="@string/connection_status"- Now we're going to add code that will poll for the connection status and update the connection status text view with the current status
- Open
MainActivity.java - Add a status handler field to the class
//android.os.Handler
private Handler statusHandler;- In the
onCreatemethod initialize the handler and start monitoring the status
if (statusHandler == null) {
statusHandler = new Handler();
}
startMonitoringStatus();- Add methods to control the
Handler
Runnable statusChecker = new Runnable() {
@Override
public void run() {
TextView connectionView = (TextView) findViewById(R.id.connectionView);
// check if authenticated
if (EngageConnectionManager.get().isAuthenticated()) {
connectionView.setText("Connection Successful!");
} else {
connectionView.setText("Not Connected");
}
statusHandler.postDelayed(statusChecker, 2000); // every two seconds
}
};
void startMonitoringStatus() {
statusChecker.run();
}
void stopMonitoringStatus() {
statusHandler.removeCallbacks(statusChecker);
}- Override the
onStart()andonStop()methods.
@Override
protected void onStart() {
super.onStart();
startMonitoringStatus();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopMonitoringStatus();
}- For reference the full class looks like this:
package com.silverpop.engagedemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.silverpop.engage.network.EngageConnectionManager;
public class MainActivity extends Activity {
private Handler statusHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (statusHandler == null) {
statusHandler = new Handler();
}
startMonitoringStatus();
}
@Override
protected void onStart() {
super.onStart();
startMonitoringStatus();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopMonitoringStatus();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Runnable statusChecker = new Runnable() {
@Override
public void run() {
TextView connectionView = (TextView) findViewById(R.id.connectionView);
// check if authenticated
if (EngageConnectionManager.get().isAuthenticated()) {
connectionView.setText("Connection Successful!");
} else {
connectionView.setText("Not Connected");
}
statusHandler.postDelayed(statusChecker, 2000); // every two seconds
}
};
void startMonitoringStatus() {
statusChecker.run();
}
void stopMonitoringStatus() {
statusHandler.removeCallbacks(statusChecker);
}
}- In the Run Configurations click the run button for
app - Pick the emulator or Android device of your choice. If you haven't set one up yet you can click the '...' button next the 'Android virtual device' field in the 'Choose Device' dialog that opens up. Or you can set these up under Tools > Android > AVD Manager before running the app. Additional instructions can be found here.
- The app will start up and the text will change to "Connection Successful!" once the application has successfully authenticated with the Silverpop APIs (NOTE: The Android emulator takes several minutes to start up so we may want to consider editing some of that wait time out of the video)