Share
Explore

w24 Android March 15

image.png
Resources:
Lecture Notes summary from the first few days:
Note that the need to do this procedure stems from our coding practice of using ViewBindings → Replacing the old deprecated approch of findViewByID {you might still see this in older references, but DO NOT do this in any assignments or hand in work }
image.png

image.png

Lab Worksheets:

Google Maps Lab:

Implicit Intents ·What is an implicit intent? Typical usage cases?
megaphone

Learning outcomes: Implementing implicit intents using Kotlin, emphasizing the adherence to the latest Android development standards.

Implicit Intents in Kotlin for Android
1. Definition and Importance**
- Implicit intents do not directly specify the Android component which should be called.
Instead, they declare an action to perform, allowing the system to find an appropriate component for the intent. // or else, if not available, notifier the user in a graceful way.
Implicit Intents are essential for performing tasks that integrate with other apps, like opening a webpage in a browser or accessing the phone dialer.

2. Example Use Cases
a. Opening the Phone Dialer**
To open the phone dialer, you would use the `ACTION_DIAL` action.
Ensure your application includes necessary permissions and handles user data responsibly according to [Google's policies on user safety and data](https://developers.google.com/maps/documentation/urls/android-intents#kotlin)
```kotlin ​val dialIntent = Intent(Intent.ACTION_DIAL).apply { data = Uri.parse("tel:5551234") } startActivity(dialIntent) ```
b. Sending an SMS
For sending an SMS, use the `ACTION_SENDTO` action.
As with the dialer, ensure the SDKs your app uses are compliant with Google Play policies regarding user data and applicable local privacy legislation in your juridiction.
```kotlin val smsIntent = Intent(Intent.ACTION_SENDTO).apply { data = Uri.parse("sms:4165551234") putExtra("sms_body", "Hello, World!") } startActivity(smsIntent) ```
c. Opening a Location in Google Maps
Displaying a location in Google Maps is a common task that involves `Intent.ACTION_VIEW` and a `geo:` URI.
ake sure that any location data used complies with Google's user data policies and SDK guidelines.
```kotlin val locationIntent = Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California")).apply { setPackage("com.google.android.apps.maps") } startActivity(locationIntent) ```

3. Best Practices for Kotlin Android Development
Use Android Annotations:
Annotations help reduce boilerplate and improve code reliability. Use annotations like `@NonNull` to indicate that parameters should not be null, thus eliminating unnecessary null checks.
Leverage Build Tools:
Declare required components in the manifest of your SDK and use build variables for specific configurations, ensuring streamlined integration for app developers.
- Design Consistent APIs:
Your API design should be intuitive for developers, sticking to naming conventions and behaviors familiar within the Android ecosystem.
- **Exception Handling:**
Robust exception handling is crucial. Implement lint checks and static code analysis in your CI processes to ensure reliability.
- **Explicit API Mode in Kotlin:**
For SDK/library authors, enabling explicit API mode helps make the API clearer and ensures consistency. This Kotlin feature enforces visibility and type explicitness, leading to safer, more predictable code.
- **Optimize Resource Usage:**
Be mindful of system resource utilization. Strategies such as batching network requests and optimizing battery use can significantly enhance the user experience and app performance.
By adhering to these practices and leveraging the power of implicit intents in Kotlin, developers can create seamless, integrated Android applications that safely and efficiently interact with other apps and services.

What is the purpose of the Intent.resolveActivity() function?
error

Understanding the Purpose of `Intent.resolveActivity()`

The `Intent.resolveActivity()` function in Android development is used to determine if there's an activity available that can handle the intent that has been created.
This verification step is integral to ensure that when an app attempts to perform an action, there is an application component on the device capable of handling that action.
If no component can handle the intent,
`resolveActivity()` returns null, helping developers avoid an `ActivityNotFoundException` and thus making the application more robust and user-friendly.

Use Case: Opening a Web Page with Implicit Intent
A common use case for `Intent.resolveActivity()` is when an application wants to open a web page.
This task typically involves creating an implicit intent to view a URI.
Before attempting to start this activity, it's best practice to check if any installed browser can handle this request.
Here's how to do it:
Kotlin Code Example
```kotlin import android.content.Intent import android.net.Uri import androidx.appcompat.app.AppCompatActivity import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)
// Define the URL you want to open val webpage: Uri = Uri.parse("https://www.example.com") // Create an Implicit Intent with ACTION_VIEW action val webIntent = Intent(Intent.ACTION_VIEW, webpage)
// Check if there is an activity that can handle this intent if (webIntent.resolveActivity(packageManager) != null) { // There is an application capable of handling the action, go ahead and start it startActivity(webIntent) } else { // Handle the scenario where no application can handle the intent // For example, notify the user with a Toast Toast.makeText(this, "No application found to perform this action", Toast.LENGTH_LONG).show() } } } ```
→Explanation:
1. **Creating the Intent:**
First, we create an intent with the action `Intent.ACTION_VIEW` and the data `Uri` of the webpage we want to display.
2. **Using `resolveActivity()`:**
Before calling `startActivity()`, we use `webIntent.resolveActivity(packageManager)` to check if any installed app can handle this intent.
- If a suitable app is found (i.e., `resolveActivity()` does not return null), `startActivity(webIntent)` is called, and the webpage opens in the user's preferred browser. - If no suitable app is found,
a metaphorical pathway here would be to inform the user, perhaps through a Toast notification, that no application can perform the desired action, enhancing the user experience by gracefully handling the failure scenario.
By utilizing `Intent.resolveActivity()`, developers can ensure that their applications behave predictably and provide feedback to users when an action cannot be performed, adhering to the best practices of robust Android app development.

ok

Lab Exercises showing the use of implicit intents to:

Open the phone dialer app using ACTION_DIAL
Send an SMS Message
Open the Google Maps app with INTENTS and display a location
Lab Exercises showing the use of implicit intents to:
Open the phone dialer app using ACTION_DIAL (https://www.tutorialspoint.com/how-to-make-a-phone-call-using-intent-in-android-using-kotlin)

Send an SMS Message


Open the Google Maps app with INTENTS and display a location

Documentation References



Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.