Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách đặt hình nền (wallpaper) theo chương trình trong Android

Giới thiệu

Hướng dẫn này trình bày cách đặt hình ảnh làm wallpaper (hình nền) thiết bị theo chương trình trong Android sử dụng WallpaperManager.

Các bước thực hiện

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: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="Đặt Wallpaper"
        android:layout_gravity="center_vertical"
        android:layout_centerInParent="true"
        android:layout_marginLeft="135dp"/>
</LinearLayout>

Bước 3: Viết logic xử lý (src/MainActivity.java)

import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button button = findViewById(R.id.button);
        button.setOnClickListener(v -> setWallpaper());
    }
    
    private void setWallpaper() {
        // Đọc bitmap từ resource drawable
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper);
        
        // Lấy WallpaperManager instance
        WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
        
        try {
            // Đặt bitmap làm wallpaper
            manager.setBitmap(bitmap);
            Toast.makeText(this, "Đã đặt wallpaper!", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(this, "Lỗi: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
}

Bước 4: Cấu hình Manifest (androidManifest.xml) - Cần quyền

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <!-- Quyền bắt buộc để đặt wallpaper -->
    <uses-permission android:name="android.permission.SET_WALLPAPER"/>
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Lưu ý quan trọng

  • Bắt buộc khai báo quyền android.permission.SET_WALLPAPER trong Manifest
  • File ảnh (wallpaper.jpg/png) cần đặt trong res/drawable/
  • Từ Android 8.1 (API 27), có thể dùng WallpaperManager.setBitmap() trực tiếp
  • Một số thiết bị có thể yêu cầu xác nhận từ người dùng trước khi đổi wallpaper

Chạy ứng dụng

Kết nối thiết bị Android, nhấn Run trong Android Studio. Nhấn nút "Đặt Wallpaper" để thay đổi hình nền thiết bị.

Cách đặt hình nền (wallpaper) theo chương trình trong Android

Cách đặt hình nền (wallpaper) theo chương trình trong Android

Cách đặt hình nền (wallpaper) theo chương trình trong Android