Giới thiệu
Ví dụ này minh họa cách kiểm tra thiết bị Android có là thiết bị RAM thấp (low RAM) hay không.
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.
Bước 2: Cấu hình layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
TextView hiển thị kết quả true/false cho thiết bị low RAM.
Bước 3: Viết code logic (src/MainActivity.java)
package com.example.myapplication;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.view.WindowManager;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
ActivityManager result = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
boolean device = result.isLowRamDevice();
textView.setText("" + device);
}
}
Sử dụng ActivityManager.isLowRamDevice() để kiểm tra. Trả về true nếu thiết bị được cấu hình là low RAM.
Chạy ứng dụng
Kết nối thiết bị, nhấn Run. Màn hình hiển thị "true" (RAM thấp) hoặc "false" (RAM cao).
