It is actually very straightforward to create and call an Intent to request the users weight from WiiScale. The following code will do a few things. It will first call a method to check if the required intent for WiiScale is available on the users phone. If the intent is available it calls the intent, if it is not, it directs the user to the Android market to download the application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Uri uri = Uri.parse("weigh://broschb.wiiscale?unit=lbs"); | |
if (isIntentAvailable(this, "com.broschb.action.WEIGH",uri)) | |
startActivityForResult(new Intent("com.broschb.action.WEIGH",uri),WEIGHT_REQUEST); | |
else { | |
// direct to market | |
String APP_MARKET_URL = "market://details?id=com.broschb.wiiscale"; | |
Intent intent = new Intent( Intent.ACTION_VIEW, | |
Uri.parse(APP_MARKET_URL)); | |
startActivity(intent); | |
} |
The uri is specific uri defined by the WiiScale application for receiving intents. The unit parameter can have two values. The valid values are 'lbs' or 'kg' depending on whether your application would like the weight in pounds or kilograms. The WEIGHT_REQUEST variable is just a static Integer datatype to uniquely identify the intent when we get the result back. Note the call to 'startActivityForResult', this will allow your application to get a result back in the onActivityResult method. This method looks like the following.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
switch (requestCode) { | |
case WEIGHT_REQUEST: | |
if (resultCode == RESULT_OK) { | |
String unit = data.getStringExtra("unit"); | |
double weight = data.getDoubleExtra("weight", 0); | |
} | |
} |
This simply checks to make sure the activity result if for our WEIGHT_REQUEST from WiiScale intent call, and that it returned successfully. WiiScale returns two values from the intent. The unit of measure, either 'kg' or 'lbs' and the weight in the given unit measurement. Your application can then use this data as you please. It couldn't be simpler to integrate WiiScale into your own application. Please see the source on github for for details as well as how to check if the intent is available on your phone.