Courses / Mobile Application Development (CDE-315) – BS CS 5th Semester
Week 6 Lab 6 – Open the Browser and the Dialer Using Buttons (plus a notification bonus)
Week 6 – Intents and Notifications · 120 min read · 9 MCQs to check yourself
Intents and intent filters – implicit intents explained
(Bonus task) Notifications and notification channels
Objective
Use implicit intents to let other apps do the work: open the browser with a website and the dialer with a phone number, safely, without crashing – plus share text, show the campus in Maps and (bonus) post a notification. CLOs: CLO-2 (develop apps that use built-in apps), CLO-1 (intent resolution between apps).
What you will build in CampusMate
A Contact & Links screen (ContactActivity), opened from the Home tab:
| Button | Intent | What opens |
|---|---|---|
| Open website | ACTION_VIEW + https://… |
Browser |
| Call office | ACTION_DIAL + tel:… |
Dialer (user presses call) |
| Share app | ACTION_SEND + text |
Share sheet (WhatsApp, Gmail…) |
| Campus on map | ACTION_VIEW + geo:… |
Maps app |
| Notify me (bonus) | Notification | Status bar notification |
Explicit vs implicit: in Lab 4 we named the class (SecondActivity.class) – explicit. Here we only say what we want (view a web page, dial a number) and Android finds a suitable app – implicit.
Workflow
| Step | What you do | File |
|---|---|---|
| 1 | Create ContactActivity | New → Activity |
| 2 | Design the screen (2 inputs, 4 buttons) | activity_contact.xml |
| 3 | Write a safe openIntent() helper |
ContactActivity.java |
| 4 | Browser button (ACTION_VIEW) | ContactActivity.java |
| 5 | Dialer button (ACTION_DIAL) | ContactActivity.java |
| 6 | Share and Map buttons | ContactActivity.java |
| 7 | Bonus: notification | ContactActivity.java + manifest |
| 8 | Run and test | Emulator |
Step 1 – Create the activity
New → Activity → Empty Views Activity → ContactActivity (Java). In HomeFragment (Lab 5) change the button so it opens it:
view.findViewById(R.id.btnHello).setOnClickListener(v ->
startActivity(new Intent(requireContext(), ContactActivity.class)));
Step 2 – Layout (activity_contact.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:padding="24dp">
<EditText
android:id="@+id/etUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Website, e.g. www.hec.gov.pk"
android:inputType="textUri"
android:text="www.hec.gov.pk" />
<Button
android:id="@+id/btnBrowser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open website" />
<EditText
android:id="@+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Office phone number"
android:inputType="phone"
android:text="0519999999" />
<Button
android:id="@+id/btnDial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Call office" />
<Button
android:id="@+id/btnShare"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Share CampusMate" />
<Button
android:id="@+id/btnMap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Campus on map" />
<Button
android:id="@+id/btnNotify"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Notify me (bonus)" />
</LinearLayout>
inputType="textUri"shows a keyboard with/and.com;inputType="phone"shows the number pad.
Step 3 – ContactActivity.java: the basic structure and a safe helper
package com.example.campusmate;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class ContactActivity extends AppCompatActivity {
private EditText etUrl, etPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
etUrl = findViewById(R.id.etUrl);
etPhone = findViewById(R.id.etPhone);
findViewById(R.id.btnBrowser).setOnClickListener(v -> openWebsite());
findViewById(R.id.btnDial).setOnClickListener(v -> openDialer());
findViewById(R.id.btnShare).setOnClickListener(v -> shareApp());
findViewById(R.id.btnMap).setOnClickListener(v -> showCampusOnMap());
findViewById(R.id.btnNotify).setOnClickListener(v -> notifyMe()); // Step 7
}
// One helper used by every button: start the intent without crashing
private void openIntent(Intent intent) {
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No app found to do this", Toast.LENGTH_SHORT).show();
}
}
// Steps 4–7 go below
}
Step 4 – Open the browser (ACTION_VIEW)
private void openWebsite() {
String url = etUrl.getText().toString().trim();
if (url.isEmpty()) {
etUrl.setError("Enter a website");
return;
}
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "https://" + url; // without a scheme Android may not treat it as a web link
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
openIntent(intent);
}
Step 5 – Open the dialer (ACTION_DIAL)
private void openDialer() {
String phone = etPhone.getText().toString().trim();
if (phone.isEmpty()) {
etPhone.setError("Enter a phone number");
return;
}
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
openIntent(intent); // NO permission needed: the user presses the call button
}
ACTION_CALLwould call immediately but needs the CALL_PHONE runtime permission – avoid it unless really required.
Step 6 – Share and map buttons
private void shareApp() {
Intent send = new Intent(Intent.ACTION_SEND);
send.setType("text/plain");
send.putExtra(Intent.EXTRA_TEXT, "I use CampusMate for timetable and notices!");
openIntent(Intent.createChooser(send, "Share via")); // always shows the chooser
}
private void showCampusOnMap() {
// Replace the coordinates with your own campus (copy them from Google Maps)
Uri location = Uri.parse("geo:33.6844,73.0479?q=33.6844,73.0479(Our Campus)");
openIntent(new Intent(Intent.ACTION_VIEW, location));
}
Step 7 (bonus) – A notification
- Manifest, above
<application>:<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> - Create an icon: New → Vector Asset → notifications → name
ic_notification. - Add to ContactActivity:
// Press Alt+Enter on red names to import them (Build, Manifest, NotificationChannel, ...)
private static final String CHANNEL_ID = "campus_updates";
private final ActivityResultLauncher<String> notifPermission =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), granted -> {
if (granted) showNotification();
else Toast.makeText(this, "Notifications are blocked", Toast.LENGTH_SHORT).show();
});
private void notifyMe() {
if (Build.VERSION.SDK_INT >= 33) {
notifPermission.launch(Manifest.permission.POST_NOTIFICATIONS); // Android 13+ asks the user
} else {
showNotification();
}
}
private void showNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // channels: Android 8+
NotificationChannel ch = new NotificationChannel(CHANNEL_ID, "Campus updates",
NotificationManager.IMPORTANCE_DEFAULT);
getSystemService(NotificationManager.class).createNotificationChannel(ch);
}
NotificationCompat.Builder b = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("MAD quiz tomorrow")
.setContentText("Revise intents and fragments")
.setAutoCancel(true);
try {
NotificationManagerCompat.from(this).notify(1, b.build());
} catch (SecurityException e) {
Toast.makeText(this, "Permission missing", Toast.LENGTH_SHORT).show();
}
}
Run and test
| Test | Expected result |
|---|---|
Open website with www.hec.gov.pk |
Browser opens https://www.hec.gov.pk |
| Clear the URL → Open | Red error "Enter a website" |
| Call office | Dialer opens with the number; nothing is called automatically |
| Share CampusMate | Chooser with installed apps |
| Campus on map | Maps app with a pin "Our Campus" |
| Notify me (Android 13+) | Permission dialog, then a notification |
Common errors and fixes
| Problem | Fix |
|---|---|
| App crashes "No Activity found to handle Intent" | Always start implicit intents through openIntent() (try/catch) |
| Browser shows a search instead of the site | Add https:// (Step 4) |
| No notification on Android 13+ | Permission not granted, or channel not created |
Short and long questions
1. (Viva) Write the code of the Open Browser button.
Model answer
Read the URL from the EditText, add "https://" if no scheme is present, then call startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) inside try/catch for ActivityNotFoundException.
2. (Viva) Why did the Open Dialer button not need any permission?
Model answer
ACTION_DIAL only opens the phone's dialer app with the number pre-filled; the user must press the call button, so no dangerous permission is involved. Calling directly with ACTION_CALL would need CALL_PHONE.
3. (Viva) What is the difference between the intents used for the browser, dialer, share and map buttons?
Model answer
Browser: ACTION_VIEW with an https URI; dialer: ACTION_DIAL with a tel: URI; share: ACTION_SEND with type text/plain and EXTRA_TEXT wrapped in createChooser; map: ACTION_VIEW with a geo: URI. All are implicit intents resolved by other apps.
This lesson ends with a quiz of 9 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: Week 6 Theory – Calling Built-in Applications Using Intents; Displaying Notifications · Next: Week 7 Theory – Components of a Screen; Adapting to Display Orientation; Managing Changes to Screen Orientation
All lessons in Mobile Application Development (CDE-315) – BS CS 5th Semester