0% found this document useful (0 votes)
77 views

Assignment # 02: Air University

The document is an assignment submission for a mobile computing course. It includes: 1) Code to declare variables and instantiate classes for accessing location updates from the network sensor. 2) The full Java code for an Android application that uses a BroadcastReceiver to count minutes on the device screen. 3) The XML layout file for the main activity's text view to display the counter.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Assignment # 02: Air University

The document is an assignment submission for a mobile computing course. It includes: 1) Code to declare variables and instantiate classes for accessing location updates from the network sensor. 2) The full Java code for an Android application that uses a BroadcastReceiver to count minutes on the device screen. 3) The XML layout file for the main activity's text view to display the counter.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

AIR UNIVERSITY

Faculty of Basic & Applied Sciences


Department of Computer Science & Engineering Spring 2019

Course: CS303 – Mobile Computing | Batch: BSCS-VIII-A-B |

Assignment # 02
Submitted On: Sunday, 19th May, 2019 | Total Marks: 40
Submitted By Syed Muhammad Taqi Mehdi 150835(Section A)
Contribution:

Assignment Tasks:

1. To use a network sensor, you need to implement four steps. Write the code required for each of
the following steps:
a. Declare a location manager variable and a network listener variable.
b. Access/reference to the location Manager.
c. Instantiate the LocationListener and assign it to the network listener. Write the four
methods that need to be implemented. No implementation is required.
d. Send a message to the LocationManager to begin listening for location changes of the
Network sensor.

a.LocationManager locationManager = (LocationManager)


getSystemService(LOCATION_SERVICE);

b. public class LocationManager extends Object

c. LocationManager#requestLocationUpdates(String, long, float, LocationListener)

Public methods

onLocationChanged(Location location)

onProviderDisabled(String provider)

onProviderEnabled(String provider)

onStatusChanged(String provider, int status, Bundle extras)

d.

public abstract void onLocationChanged (Location location)


CODE:
MainActivity.JAVA
package com.example.assignment3;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


private TextView counterText;
private BroadcastReceiver minuteUpdateReceiver;
private int counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

counterText=(TextView) findViewById(R.id.counter_text);
}
public void startMinuteUpdate(){
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_TICK);
minuteUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
counter++;
counterText.setText("" + counter);
}
};
registerReceiver(minuteUpdateReceiver, intentFilter);

}
@Override
protected void onResume() {
super.onResume();
startMinuteUpdate();
}
@Override
protected void onPause()
{
super.onPause();
unregisterReceiver(minuteUpdateReceiver);
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/counter_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
SCREENSHOT

You might also like