Blog
Android Application Development MSc IT 4th Final Past Paper Fall 2014 – UOS Main Campus
University of Sargodha main campus MSc IT 4th semester Android Application Development final exam, Fall 2014 (held 16 February 2015). Retyped from the original paper, with solved answers and code below.
Solved answers
(a) Database helper class
A helper class extending SQLiteOpenHelper hides creation, upgrade and CRUD operations behind simple methods:
public class DBHelper extends SQLiteOpenHelper {
private static final String DB = "school.db"; private static final int VER = 1;
public DBHelper(Context c) { super(c, DB, null, VER); }
@Override public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE student(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");
}
@Override public void onUpgrade(SQLiteDatabase db, int o, int n) {
db.execSQL("DROP TABLE IF EXISTS student"); onCreate(db);
}
public long insert(String name) {
ContentValues v = new ContentValues(); v.put("name", name);
return getWritableDatabase().insert("student", null, v);
}
public Cursor getAll() {
return getReadableDatabase().rawQuery("SELECT * FROM student", null);
}
public int delete(int id) {
return getWritableDatabase().delete("student", "id=?", new String[]{String.valueOf(id)});
}
}(b) Socket programming and client
Sockets give a direct two-way connection (IP address + port) between the app and a server for real-time communication such as chat, games and live updates, without the overhead of HTTP requests. Client that sends and listens:
new Thread(() -> {
try (Socket s = new Socket("192.168.1.10", 5000);
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()))) {
out.println("Hello server"); // send
String line;
while ((line = in.readLine()) != null) { // listen
final String msg = line;
runOnUiThread(() -> txtChat.append(msg + "\n"));
}
} catch (IOException e) { e.printStackTrace(); }
}).start();
// Manifest: <uses-permission android:name="android.permission.INTERNET"/>(c) Send and receive SMS
// send
SmsManager.getDefault().sendTextMessage(phone, null, message, null, null);
// receive
public class SmsReceiver extends BroadcastReceiver {
@Override public void onReceive(Context ctx, Intent intent) {
Bundle b = intent.getExtras();
Object[] pdus = (Object[]) b.get("pdus");
for (Object p : pdus) {
SmsMessage m = SmsMessage.createFromPdu((byte[]) p, b.getString("format"));
Toast.makeText(ctx, m.getOriginatingAddress() + ": " + m.getMessageBody(), Toast.LENGTH_LONG).show();
}
}
}
// Manifest: SEND_SMS, RECEIVE_SMS permissions and
// <receiver android:name=".SmsReceiver"><intent-filter>
// <action android:name="android.provider.Telephony.SMS_RECEIVED"/></intent-filter></receiver>(d) Download and display a picture from a URL
new Thread(() -> {
try {
URL url = new URL(imageUrl);
InputStream is = url.openConnection().getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
runOnUiThread(() -> imageView.setImageBitmap(bmp));
} catch (Exception e) { e.printStackTrace(); }
}).start();
// needs INTERNET permission; network work must not run on the UI thread(e)
(i) GPS: Android obtains location through LocationManager (GPS_PROVIDER, NETWORK_PROVIDER) or the Fused Location Provider of Google Play services. Steps: add ACCESS_FINE_LOCATION permission (and request it at runtime), get the location manager, call requestLocationUpdates() with a LocationListener, and read location.getLatitude() / getLongitude() in onLocationChanged(). Uses: maps and navigation, ride-hailing, finding nearby places, geotagging, fitness tracking.
(ii) Linking activities using Intents: an Intent is a message object used to start another activity, service or broadcast. Explicit intents name the target class (used within your app); implicit intents declare an action and let the system choose an app; data is passed with putExtra() and results returned with startActivityForResult()/Activity Result API.
// explicit intent with data
Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtra("name", "Ali");
startActivity(i);
// in SecondActivity
String name = getIntent().getStringExtra("name");
// implicit intent
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://shiningstudy.com")));
