Sie sind auf Seite 1von 11

Assignment No B-01

Aim
Write a program to build smart mobile app for context management.

Pre-requisite
1. Data Structure.
2. Programming language basics.

Objective
1. To build a smart app form smart phone which can sense some parameters of the user.
2. To convert this parameters into some contextual information in order to do the context
management.

Problem Statement
Implement a smart app form smart phone which can sense some parameters of the user
and convert it into some contextual information in order to do the context management.

Hardware / Software Used


1. Android Studio

Mathematical Model
M = { s, e, X, Y, DD, NDD, fme , M emshared , Fsuccess , Ff ailure , CP UCoreCount }

1. s = Initial state - Initialise time.


2. e = End state - Final count.
3. X = Input- Start and stop button pressed by user.
4. Y = Output- Displays count of steps and time taken by user.
5. DD = Deterministic data- Start time and stop time.
6. F me = intializeSensors() and stepDetector() are used for detect the steps.
7. Mem shared = No shared Memory is used.
8. Success = Steps counted successfully.
9. Failure = Program interrupted or abnormal termination.
10. CPU CoreCount = 1.

Theory
Context Management is an application that operates in dynamic conditions. Which means
it requires a reactive platform to make informed decisions on how to respond to device changes
in user preferences, capabilities, enterprise policies and other factors. Context Manager helps
to collect and combine and process context information. Context Manager is gaining peak due
to the amount of views of mobiles which can capture information about the users context is
increasing due to the enhanced software and hardware functionalities in mobile computing. On
the other hand, the variation in the way that user context can be used by different services is
growing rapidly.
A pedometer is a device, which is usually portable and electronic or electromechanical
which counts each step of a person by detecting the motion of the persons hips or hands. Because of which distance of each persons step varies, an informal detection of the correct range,
performed by the user, is required if presentation of the distance covered by the user is in a
unit of length (such as in kilometres or miles) is desired. Distance travelled (by walking or any
other means) can be measured by a GPS tracker directly.
Depending on your role within the CMS(Context Management System) workflow, there
are several uses might give:
A CMS is used to store information to present on a web site and/or other devices CMS
vendor.
A CMS is used to store and present content to enhance your sales and online conversions
experience CMS vendor.
A CMS is used to make sure your content follows standardized patterns so it is more
easily reused CMS integrator/content modeller.
A CMS makes content presentation more consistent (and unfortunately get in the way of
my artistic efforts) Web designer.
If it is linked up to our sales database, a CMS allows us to cross- and up-sell better to
our customer base; if its not, it just gets in the way Marketing department.
It provides me with a pool of information that I can reuse, depending on the situation
User assistance person.
A CMS provides an audit trail of content relationships and versions, to satisfy regulatory
requirements Compliance officer.
The CMS forces me to jump through hoops to maintain content that should be really
easy Content manager.

Procedure
1.Build and Run the project in android studio

Conclusion
Hence we have studied the Context Management System and developed an application called
Pedometer using Android.
3

Program
=================================================
GROUP
Assignment No : B-1
Title : Write a program to build smart mobile app for context management.
Roll No :
Batch : B
Class : BE ( Computer )
=================================================

------------------------------------------------DatabaseHelper.java
------------------------------------------------package com.example.pedometercm;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = DatabaseHelper.class.getName();
public static final String DATABASE_NAME = "pedometer";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_QUERY = "CREATE TABLE PEDODATA( ID INTEGER PRIMAR
private static DatabaseHelper sDbInstance = null;
private Context mContext = null;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_QUERY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public static DatabaseHelper getInstance(Context context) {
if (sDbInstance == null) {
sDbInstance = new DatabaseHelper(context);
}
return sDbInstance;
}
public synchronized Cursor ExecuteRawSql(String s) {// Select Query
try {
SQLiteDatabase sqLiteDb = getReadableDatabase();
Cursor cursor = sqLiteDb.rawQuery(s, null);
return cursor;

} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public synchronized boolean ExecuteSql(String s) {// Update Query
try {
Log.d(TAG, "Actual Query--->>" + s);
SQLiteDatabase sqLiteDb = getWritableDatabase();
sqLiteDb.execSQL(s);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean saveData(String startTime, String stopTime, int stepCount) {
String qry = "INSERT INTO PEDODATA (STARTTIME,STOPTIME,STEPS) VALUES("
+ startTime + "," + stopTime + "," + stepCount + ");";
return ExecuteSql(qry);
}
public ArrayList<String> getData() {
ArrayList<String> list = new ArrayList<String>();
String qry = "SELECT STARTTIME,STOPTIME,STEPS FROM PEDODATA ORDER BY ID DESC";
Cursor cursor = ExecuteRawSql(qry);
if (cursor != null && cursor.moveToFirst()) {
do {
String startTime = cursor.getString(0);
String stopTime = cursor.getString(1);
int stepCount = cursor.getInt(2);
String time = "From : " + startTime + " \nTo : " + stopTime;
String steps = " \nSteps : " + stepCount;
list.add(time + steps);
} while (cursor.moveToNext());
}
return list;
}
}
-----------------------------------------------DetailsListActivity.java
-----------------------------------------------package com.example.pedometercm;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
5

public class DetailsListActivity extends Activity {


private ListView list;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_list);
list=(ListView)findViewById(R.id.list);
DatabaseHelper database=new DatabaseHelper(getApplicationContext());
ArrayList<String> arrayList=database.getData();
adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.list_layout, arr
list.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.details_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
---------------------------------------------------MainActivity.java
---------------------------------------------------package com.example.pedometercm;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.FloatMath;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
6

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener,OnClickLi
private static int STEP_COUNT = 0;
private static String START_TIME;
private long START_MILI;
private static String STOP_TIME;
private float lastX, lastY, lastZ, deltaX, deltaY, deltaZ;
private TextView numberOfSteps,tvTimeCount;
private Button buttonStart, buttonStop;
private SensorManager senSensorManager;
private Sensor acceleroMeter;
private int seconds;
private int minutes;
private int hours;
private Handler myHandler;
private long lastStepTakenTime;
private Runnable updateTimerMethod = new Runnable() {
public void run() {
long timeInMillies = SystemClock.uptimeMillis() - START_MILI;
int totalseconds = (int) (timeInMillies / 1000);
minutes = totalseconds / 60;
hours = minutes / 60;
seconds = totalseconds % 60;
tvTimeCount.setText(String.format("%02d", hours) + ":"
+ String.format("%02d", minutes) + ":"
+ String.format("%02d", seconds));
myHandler.postDelayed(this, 0);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberOfSteps = (TextView)findViewById(R.id.numberOfSteps);
tvTimeCount = (TextView) findViewById(R.id.timeCount);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStop.setEnabled(false);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
intializeSensors();
myHandler=new Handler();
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
7

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_history) {
Intent intent=new Intent(this,DetailsListActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
final float[] values = sensorEvent.values;
final Sensor sensor = sensorEvent.sensor;
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
stepDetector(values, sensorEvent.timestamp / (500 * 10 ^ 6l));
}
}
private void intializeSensors() {
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
acceleroMeter = senSensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
private void stepDetector(float[] values, long timeStap) {
float x = values[0];
float y = values[1];
float z = values[2];
deltaX = lastX - x;
deltaY = lastY - y;
deltaZ = lastZ - z;
lastX = x;
lastY = y;
lastZ = z;
@SuppressWarnings("deprecation")
float totalAccel = FloatMath.sqrt((x - deltaX) * (x - deltaX)+ (y - deltaY) * (y Log.v("FFSPA", "totalAcceleration : " + totalAccel);
long timeBetweenTwoSteps=SystemClock.uptimeMillis()-lastStepTakenTime;
if (totalAccel > 8 && timeBetweenTwoSteps>300) {
STEP_COUNT++;
lastStepTakenTime=SystemClock.uptimeMillis();
publishData(STEP_COUNT);
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private void resetPedo() {
STEP_COUNT = 0;
publishData(000);
8

}
private void publishData(int steps) {
numberOfSteps.setText(String.format("%03d", steps));
tvTimeCount.setText("00.00.00");
}
public void onClick(View clickView) {
if (clickView.getId() == R.id.buttonStart) {
resetPedo();
startPedo();
buttonStart.setEnabled(false);
buttonStop.setEnabled(true);
}
if (clickView.getId() == R.id.buttonStop) {
stopPedo();
buttonStop.setEnabled(false);
buttonStart.setEnabled(true);
}
}
private void startPedo() {
senSensorManager.registerListener(this, acceleroMeter,
SensorManager.SENSOR_DELAY_NORMAL);
START_TIME=getDateTime();
START_MILI = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
}
private void stopPedo() {
senSensorManager.unregisterListener(this, acceleroMeter);
STOP_TIME=getDateTime();
saveData(START_TIME,STOP_TIME,STEP_COUNT);
myHandler.removeCallbacks(updateTimerMethod);
}
private void saveData(String startTime, String stopTime, int stepCount) {
DatabaseHelper database=new DatabaseHelper(getApplicationContext());
database.saveData(startTime,stopTime,stepCount);
}
public String getDateTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm:ss", Locale.US);
String formattedDate = formatter.format(c.getTime());
String s = formattedDate;
return s;
}
}

Output

10

Plagiarism Score

11

Das könnte Ihnen auch gefallen