1. Introduction
In today’s mobile environment, the Android platform is one of the most widely used operating systems. In particular, using the Java language to develop apps is a familiar approach for many developers. In this course, we will start from the basics of app development through integration with basic Android apps and take it a step further to implement various features. Through this course, you will understand the interaction between Android apps and basic apps (e.g., contacts, camera, etc.) and develop the ability to implement them yourself.
2. Setting Up the Android App Development Environment
To develop Android apps, you first need to set up the development environment. Using Android Studio provides many convenient tools and features. Below are the installation steps for Android Studio.
- Download Android Studio: Download from the official Google site.
- Installation: Follow the installation wizard.
- SDK Installation: The necessary SDKs and tools will be installed automatically.
- Creating a Project: Click on ‘New Project’ to create a new Android project.
3. Understanding Android Basic Apps
Android has several types of basic apps. These apps provide functionalities that developers can easily use without the need for external libraries. For example, there are contacts apps, camera apps, maps apps, etc. In this section, we will use an example connected to the contacts app to explore the functionalities of basic apps.
4. Integrating with the Contacts App
We will implement a feature that retrieves contact information by integrating with the contacts app. The following are the steps to implement this feature.
4.1. Requesting Permissions
To access the contacts, you need to request permission from the user. Add the following permission to the AndroidManifest.xml file.
<uses-permission android:name="android.permission.READ_CONTACTS" />
4.2. Retrieving Contact Information
Now, open the MainActivity.java file and write the code to retrieve contact information.
import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_CONTACT = 1;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkPermission();
}
});
}
private void checkPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE_CONTACT);
} else {
getContacts();
}
}
private void getContacts() {
ContentResolver resolver = getContentResolver();
Uri uri = ContactsContract.Contacts.CONTENT_URI;
Cursor cursor = resolver.query(uri, null, null, null, null);
StringBuilder contactsList = new StringBuilder();
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactsList.append(name).append("\n");
}
cursor.close();
textView.setText(contactsList.toString());
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_CONTACT && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getContacts();
}
}
}
4.3. Building the UI
Open the activity_main.xml file and build the UI. You will add a Button and a TextView by default.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Contacts" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
4.4. Running the App
Now, run the project. When you click the ‘Get Contacts’ button, the list of contacts will be displayed in the TextView. If a permission request dialog appears, you must click allow to retrieve contact information.
5. Integrating with the Camera App
Next, we will implement a feature to take a photo by integrating with the camera app. Integrating with the camera is also a great example of integration between basic apps.
5.1. Requesting Permissions
The method for requesting permissions to access the camera is similar to that of the contacts app. Add the following to the AndroidManifest.xml.
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
5.2. Taking a Photo
Add the function for taking a photo in the MainActivity.java file.
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_IMAGE_CAPTURE = 1;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
imageView = findViewById(R.id.imageView);
button.setOnClickListener(view -> {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
}
5.3. Building the UI
Add an ImageView in the activity_main.xml file to display the captured photo.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Photo" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
5.4. Running the App
Now run the program and click the ‘Take Photo’ button. If it works correctly, the camera will launch, and after taking the photo, the result will be displayed in the image view.
6. Conclusion
In this course, we learned how to integrate with basic Android apps using Java. Through the integration of contacts and camera apps, we explored how to utilize the functionalities of basic apps in app development. Based on this example, you can expand your own app functionalities and try various forms of integration. Continue to learn and practice Android app development to further your skills!