Java code example
This code example shows how to use the Splunk MINT SDK for Android API with Java.
package com.splunk.sdkexample; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.splunk.mint.Mint; import com.splunk.mint.MintLog; import com.splunk.mint.MintLogLevel; import java.util.HashMap; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //To preserve resources, the MINT SDK can be configured to send data only over a WIFI connection Mint.setFlushOnlyOverWiFi(true); //To protect privacy, the MINT SDK can be disabled so that no mobile telemetry is reported Mint.setUserOptOut(true); //The MINT SDK will start a new session if the application has not been in the foreground for //the duration of the session interval //Default session interval is 60 seconds Mint.setSessionInterval(60); //In order to send logcat logs in crash reports, enableLogging must be set to true Mint.enableLogging(true); //Set the number of logcat messages to send in the crash report Mint.setLogging(10); //Set the filter of logcat messages to send in the crash report Mint.setLogging("ActivityManager:I MyApp:D *:S"); //Set the number and the filter of logcat messages to send in the crash report Mint.setLogging(10, "ActivityManager:I MyApp:D *:S"); //Set some form of userIdentifier for this session Mint.setUserIdentifier("userid"); //Set the applicationEnvironment, the available options are /* appEnvironmentRelease appEnvironmentStaging appEnvironmentUserAcceptanceTesting appEnvironmentTesting appEnvironmentDevelopment */ Mint.setApplicationEnvironment(Mint.appEnvironmentRelease); //Disable network monitoring Mint.disableNetworkMonitoring(); //Selectively disable network monitoring on blacklisted url's Mint.addURLToBlackList("http://api.facebook.com/"); //Breadcrumbs are only sent out in crash reports //Leave breadcrumbs throughout the source code to better debug the execution path Mint.leaveBreadcrumb("loginScreen"); //Initialize the SDK to use the MINT Backend to transport data //Will start a new session if one is not active Mint.initAndStartSession(this.getApplication(), "API_KEY"); //Initialize the SDK to directly send data to a Splunk instance using //HTTP Event Collector //Will start a new session if one is not active Mint.initAndStartSessionHEC(this.getApplication(), "HEC URL", "HEC TOKEN"); //Will start a new session if one is not active Mint.startSession(this.getApplication()); //Will close the active session Mint.closeSession(this.getApplication()); //Adding local MintLimitedExtraData to global extra Data //Global value's will be overwritten if there is a key clash //Global extra data is sent out with each data point //Add key and value to global extra data Mint.addExtraData("level", "5"); //Add multiple values to global extra data HashMap<String, Object> extraData = new HashMap<String, Object>(2); extraData.put("level", "5"); extraData.put("difficulty", "easy"); Mint.addExtraDataMap(extraData); //Remove a specific key and value from global extra data Mint.removeExtraData("level"); //Return hashmap of global extra data key and values HashMap<String, Object> currentData = Mint.getExtraData(); //Clears global extra data Mint.clearExtraData(); //Will return the remote settings Mint.getDevSettings(); //Will return the UUID of this MINT installation Mint.getMintUUID(); //Will return the sessionID of the current session Mint.getSessionId(); //Will log a custom view Mint.logView("loginScreen"); //Will log a custom view with extra data HashMap<String, Object> viewData = new HashMap<String, Object>(2); viewData.put("level", "5"); viewData.put("difficulty", "easy"); Mint.logView("loginScreen", viewData); /* Transactions are manually started through API calls. Transactions have three separate END states: SUCCESS: The transaction was stopped normally through a transactionStop API Call CANCEL: The transactionw was cancelled, the API requires a reason for canceling a transaction FAIL: Active transactions are automatically failed when an exception crashes the application. For each API there are 3 variations based on the extra data being passed in. */ //Start a transaction String txID1 = Mint.transactionStart("transaction1"); //Start a transaction with extra data key value String txID2 = Mint.transactionStart("transaction2", "level", "5"); //Start a transaction with extra data as map HashMap<String, Object> transactionData = new HashMap<String, Object>(2); transactionData.put("level", "5"); transactionData.put("difficulty", "easy"); String txID3 = Mint.transactionStart("transaction3", transactionData); //Cancel a transaction Mint.transactionCancel(txID1, "user canceled"); //Cancel a transaction with extra data key value Mint.transactionCancel(txID2, "user canceled", "level", "5"); //Cancel a transaction with extra data as map Mint.transactionCancel(txID3, "user canceled", transactionData); //Stop a transaction Mint.transactionStop(txID1); //Stop a transaction with extra data key value Mint.transactionStop(txID2, "level", "5"); //Stop a transaction with extra data as map Mint.transactionStop(txID3, transactionData); //The logEvent API calls can be used to log notable events. Mint.logEvent("eventName"); /* There are 4 variations based on the extra data being passed in and the log level to display. The following are allowed log levels: Verbose, Debug, Info, Warning, Error */ Mint.logEvent("eventName", MintLogLevel.Debug); //Log an event with extra data key value Mint.logEvent("eventName", MintLogLevel.Debug, "key", "value"); //Log an event with extra data as map HashMap<String, Object> eventData = new HashMap<String, Object>(2); eventData.put("level", "5"); eventData.put("difficulty", "easy"); Mint.logEvent("eventName", MintLogLevel.Debug, eventData); //MintLog is a wrapper arround Log class that sends the data to MINT //There are 5 different logging levels d: debug, i: information, v: verbose, w: warning, e: error MintLog.i("MyApp", "This is an info message"); //The logException API calls can be used to log handled exceptions. //For each API there are 3 variations based on the extra data being passed in. try { String a = null; a.toString(); } catch (Exception e) { //Just log the exception Mint.logException(e); //Log the exception with key and value as extra data Mint.logExceptionMessage("key", "value", e); //Log the exception with a hashmap as extra data HashMap<String, Object> crashData = new HashMap<String, Object>(2); crashData.put("level", "5"); crashData.put("difficulty", "easy"); Mint.logExceptionMap(crashData, e); } //Flush all the collected data Mint.flush(); //Get the ID of the latest crash Mint.getLastCrashID(); //Get the number of the crashes int crashes = Mint.getTotalCrashesNum(); //Clears the number of the crashes Mint.clearTotalCrashesNum(); } }
Generate network events with an OkHTTP Interceptor | API Reference |
This documentation applies to the following versions of Splunk MINT™ SDK for Android (Legacy): 5.2.x
Feedback submitted, thanks!