- Developers
- Professional Navigation for Fleets
- Android
- Getting started
- Embedded integration using Android Studio
Embedded integration using Android Studio
Introduction
In the following we will guide you through creating a simple Android application with embedding Sygic navigation engine through window in window style.
In this example we will develop a simple application, where you can type in an address string and through a custom button you can start navigation to that address.
Prepare your working environment
For being able to compile your application you need to have Android Studio on your computer. If you don't have it please follow Download Android Studio.
Not necessarily but we recommend first to test the compilation flow using the IntegDemo3D project within the Sygic SDK package. To get the demo of Embedded SDK version please contact us.
Create new project
Let's start Android Studio and start the new project through Start a new Android Studio project, name the application e.g. MyApp, and name the package e.g. com.sygic.example.hello3dwiw. Let's define the minimum SDK version API 19, and let's select the template Empty Activity.
The project is automatically populated with several files, while these are relevant:
- MainActivity.java, which is the placeholder for application coding (java code)
- activity_main.xml, which will define graphical look of application (layout form)
The java code should be generated as follows:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The layout form should be generated as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" ></TextView>
</RelativeLayout>
Define graphical look
As an example of the window-in-window look we can prepare the layout in such a way, that it splits the screen into 2 segments,
one, which defines the UI interface for address input (Editext edit1) and the button, which triggers the navigation(Button button1)
second, which prepares placeholder for Sygic navigation pannel (FrameLayout sygicmap).
Thus we can modify the default generated layout to the following form
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.rcmar.mysygicwiw.MainActivity"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="0.35">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="20sp"
android:paddingBottom="30sp"
android:text="Hello World!" ></TextView>
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="E.g. deu,berlin,alexanderplatz,10" ></EditText>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Navigate to Address" ></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.65">
<FrameLayout
android:id="@+id/sygicmap"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
</LinearLayout>
Modify AndroidManifest.xml
The following 3 modifications are needed:
- add tools:replace="android:icon, label, theme" into application section
- add android:configChanges="orientation|keyboardHidden|screenSize" into activity section
- optionally add application permissions
- optionally add intent filter for com.sygic
The xml could look as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.sygic.example.wiwdemo3d">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" ></uses>
<uses-permission android:name="android.permission.READ_PHONE_STATE" ></uses>
<uses-permission android:name="android.permission.INTERNET" ></uses>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar"
tools:replace="android:icon, label, theme">
<activity android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" ></action>
<category android:name="android.intent.category.LAUNCHER" ></category>
</intent-filter>
<intent-filter>
<data android:scheme="com.sygic.aura"></data>
<action android:name="android.intent.action.VIEW" ></action>
<category android:name="android.intent.category.BROWSABLE" ></category>
<category android:name="android.intent.category.DEFAULT" ></category>
</intent-filter>
</activity>
</application>
</manifest>
Optional modification
If you have your own application class it is required to update the manifest tools:replace setting as follows:
tools:replace="android:icon, label, theme, android:name"
android:name="android.support.multidex.MultiDexApplication"
Adapt build.gradle
app build gradle
dependencies {
...
implementation 'com.sygic.fleet:embedded-with-res:20.3.0'
}
project build gradle
allprojects {
repositories {
...
maven { url "https://public.repo.sygic.com/repository/maven-sygic-releases/" }
}
}
Add Kotlin support
Through Android Studio follow Tools -> Kotlin -> Configure Kotlin Project
Code the application
The following four modifications steps are minimum to be applied:
- define Sygic navigation UI framework by extending MainActivity class
- add SygicNaviFragment class into applicatiom
- add SygicNaviCallback class into application
- add Sygic API call to allow for navigation operation
- optionally add application permissions (required for API version 23 and higher)
Please check Permissions management
1. Define navigation UI framework
The following code is the typical framework to pick when starting to write an application using Sygic Window in Window mode.
The function checkSygicResources unpacks and sets up the navigation resources provided they are contained within the package.
The function initUI() instantiates Sygic navigation fragment and passes it into Fragment manager.
The Sygic navigation then requires that the methods (onCreateDialog, onPrepareDialog, onActivityResult, onNewIntent, onKeyUp, onKeyDown) are overriden with the code bellow.
public class MainActivity extends AppCompatActivity
{
private SygicNaviFragment fgm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(PermissionsUtils.requestStartupPermissions(this) == PackageManager.PERMISSION_GRANTED)
{
checkSygicResources();
}
}
private void checkSygicResources() {
ResourceManager resourceManager = new ResourceManager(this, null);
if(resourceManager.shouldUpdateResources()) {
Toast.makeText(this, "Please wait while Sygic resources are being updated", Toast.LENGTH_LONG).show();
resourceManager.updateResources(new ResourceManager.OnResultListener() {
@Override
public void onError(int errorCode, @NotNull String message) {
Toast.makeText(MainActivity.this, "Failed to update resources: " + message, Toast.LENGTH_LONG).show();
finish();
}
@Override
public void onSuccess() {
initUI();
}
});
}
else {
initUI();
}
}
private void initUI() {
setContentView(R.layout.activity_main);
fgm = new SygicNaviFragment();
getSupportFragmentManager().beginTransaction().add(R.id.sygicmap, fgm, null).commit();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
for(int res : grantResults) {
if(res != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "You have to allow all permissions", Toast.LENGTH_LONG).show();
finish();
return;
}
}
// all permissions are granted
checkSygicResources();
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dlg = fgm.onCreateDialog(id);
if (dlg == null)
return super.onCreateDialog(id);
return dlg;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
fgm.onPrepareDialog(id, dialog);
}
@Override
protected void onNewIntent(Intent intent) {
fgm.onNewIntent(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
fgm.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return fgm.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
return fgm.onKeyUp(keyCode, event);
}
}
Please note
The above code using the function checkSygicResources works only when including the package version with navigation resources (see section Adapt build.gradle).
When including the package version without navigation resources the function checkSygicResources would end up with an error.
Alternatively you would need to take care of setting up resources yourself, e.g. by copying them manually (see section Navigation resources).
Please note
Sygic navigation requires the following 3 permissions as of the release version 13.7.4
- ACCESS_FINE_LOCATION
- WRITE_EXTERNAL_STORAGE
- READ_PHONE_STATE
As of Android 6.0 the application needs to ask those permissions explicitely. For convenience SygicLib provides the function PermissionsUtils.requestStartupPermissions as indicated above.
Please note
Consider this change of the code on the Sygic navigation fragment invocation using commitAllowingStateLoss() instead of commit() due to a bug in some Android versions
private void initUI() {
setContentView(R.layout.activity_main);
fgm = new SygicNaviFragment();
getSupportFragmentManager().beginTransaction().add(R.id.sygicmap, fgm, null).commitAllowingStateLoss();;
}
2. Add Sygic Navigation Fragment class
The bare minimum required implementation of the SygicNaviFragment class should be as follows, i.e. defining start of navigation and setting callback reference within the onResume() method.
import com.sygic.aura.embedded.SygicFragmentSupportV4;
public class SygicNaviFragment extends SygicFragmentSupportV4 {
@Override
public void onResume() {
startNavi();
setCallback(new SygicNaviCallback(getActivity()));
super.onResume();
}
}
3. Add Sygic navigation callback class
The implementation of the callback class is neccessary if we want to react on some navigation events.
The callback class must be inherited from the Sygic IAPiCallback class. The basic implementation defines the constructor, through which the main activity reference is passed.
And interestingly we need to fill the mandatory onEvent method, through which various events are coming from navigation and can be processed as desired.
The example here shows the navigation exit event is captured, on which we close the whole application.
import com.sygic.aura.embedded.IApiCallback;
import com.sygic.sdk.api.events.ApiEvents;
public class SygicNaviCallback implements IApiCallback {
private Activity mActivity;
public SygicNaviCallback(Activity activity) {
mActivity = activity;
}
@Override
public void onEvent(final int event, final String data) {
if(event == ApiEvents.EVENT_APP_EXIT) {
mActivity.finish();
}
if(event == ApiEvents.EVENT_APP_STARTED) {
// make sure API is only used after this event occurs
}
}
@Override
public void onServiceConnected() {
}
@Override
public void onServiceDisconnected() {
}
}
4. Add navigation operation through API call
We will call the navigation function from the button click on our designed button through its OnClick method.
Inside OnClick we read the user typed address and use it in the API call navigateToAddress. Here we use the API call using the thread mechanics.
Thus the whole functionality can be defined by extending the initUI() function as follows.
private void initUI() {
setContentView(R.layout.activity_main);
fgm = new SygicNaviFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.sygicmap, fgm).commit();
final EditText address = (EditText)findViewById(R.id.edit1);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread() {
public void run() {
try {
ApiNavigation.navigateToAddress(address.getText().toString(), false, 0, 0);
} catch (GeneralException e) {
e.printStackTrace();
}
}
}.start();
}
});
}
Attention
Sygic navigation with its API inside the fragment requires few milliseconds initialization at the application start. It means that an unexpected crash can occur in case an API call is used immediately at the start, e.g. in early stages of OnCreate() function. It is recommended to make sure an API call is not used before the event ApiEvents.EVENT_APP_STARTED occurs (see SygicNaviCallback).
Navigation resources
Finally, the Sygic Navigation resources must be present in the device file system. The resources are part of the package you obtain and are contained inside the Navigation/Sygic3D/SygicLib folder.
There are 2 options to place them in the file system: automatic load using a helper class, or manual copying.
Automatic load
When using the helper class ResourceManager through the function checkSygicResources as exemplified in section Code the application the resources setting up is automatically taken care of. For an alternative manual handling see the following.
Manual copying
The SygicLib folder needs to be copied to an appropriate location, see the following.
-
for Android 10 and higher
The placement of the resources must be the application specific folder:/Android/data/com.sygic.fleet/files/ SygicLib\ Android\ Res\ Maps\
-
For Android 9 and below
The placement of the resource folder can be at the root of the device, i.e. sdcardsdcard\ SygicLib\ Android\ Res\ Maps\
Sygic navigation will search for the SygicLib folder to read resources.
Since the Sygic version 20.0.0 the SygicLib folder will be moved into the application specific folder with the first application start after installation.
Attention
it is important that the resources are aligned with the library version. Therefore it is strongly recommended that for an application development the resources and library are taken from the same release package.
Build and deploy application
Building the application is simple as clicking on Run button in Android Studio.
The result of the build process is generation of MyApp.apk, which is normally automatically deployed on your Android device and run.
Running the application should yield the following outcome.

Full sample reference
See on github
- Previous article: Standalone integration for Sygic2D
- Next article: Embedded integration using Xamarin