Building Arduino Projects for the Internet of Things

(Steven Felgate) #1

CHAPTER 5 ■ IOT PATTERNS: REALTIME CLIENTS


return super.onOptionsItemSelected(item);


}


//Custom function that renders view
public void updateView(String sensorMessage) {
try {
SharedPreferences sharedPref = getSharedPreferences(
"com.codifythings.motionsensorapp.PREFERENCE_FILE_KEY",
Context.MODE_PRIVATE);


if (sensorMessage == null || sensorMessage == "") {
sensorMessage = sharedPref.getString("lastSensorMessage",
"No Activity Detected");
}


final String tempSensorMessage = sensorMessage;


runOnUiThread(new Runnable() {
@Override
public void run() {


TextView updatedField = (TextView)
findViewById(R.id.updated_field);
updatedField.setText(tempSensorMessage);
}
});


SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("lastSensorMessage", sensorMessage);
editor.commit();
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}


public void createNotification(String notificationTitle,
String notificationMessage) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_template_icon_bg)
.setContentTitle(notificationTitle)
.setContentText(notificationMessage);


// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(getApplicationContext(),
MainActivity.class);

Free download pdf