Easy Tutorial
❮ Android Tutorial Adapter Scrapy Detail ❯

10.9 WallpaperManager(Wallpaper Manager)

Category Android Basic Tutorial

Introduction:

This section introduces the WallpaperManager, an API related to mobile wallpaper management. In this section, we will describe the basic usage of WallpaperManager, how to invoke the system's built-in wallpaper selection feature, set the background of an Activity as the wallpaper background, and create an example of a wallpaper changer with a timer. Let's get started without further ado.

Official API Documentation: WallpaperManager


1. Basic Usage of WallpaperManager

Related Methods

Methods for setting wallpaper:

Other methods:

Obtaining the WallpaperManager Object

WallpaperManager wpManager = WallpaperManager.getInstance(this);

Permissions Required for Setting Wallpaper

<uses-permission android:name="android.permission.SET_WALLPAPER"/>

2. Invoking the System's Built-in Wallpaper Selection Feature

Button btn_set = (Button) findViewById(R.id.btn_set);
btn_set.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent chooseIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
        startActivity(Intent.createChooser(chooseIntent, "Select Wallpaper"));
    }
});

Running Effect:


3. Setting the Activity Background as Wallpaper Background

There are two methods: one is to set it in the Activity using code, and the other is to modify the theme of the Activity in the AndroidManifest.xml.

Method One: Setting in Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(android.R.style.Theme_Wallpaper_NoTitleBar_Fullscreen);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

Method Two: Modifying the Theme in AndroidManifest.xml:

<activity android:name=".MainActivity"
android:theme="@android:style/Theme.Wallpaper.NoTitleBar"/>

4. Demo of Timed Wallpaper Change

This demo uses the AlarmManager (alarm service). If you are unfamiliar with it, you can learn about it at: 10.5 AlarmManager (Alarm Service). Below is a demo.

Running Effect:

Code Implementation:

First, let's write a Service for timed wallpaper change: WallPaperService.java

public class WallPaperService extends Service {

    private int current = 0;  // Current wallpaper index
    private int[] papers = new int[]{R.mipmap.gui_1, R.mipmap.gui_2, R.mipmap.gui_3, R.mipmap.gui_4};
    private WallpaperManager wManager = null;   // Define WallpaperManager service

    @Override
    public void onCreate() {
        super.onCreate();
        wManager = WallpaperManager.getInstance(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (current >= 4) current = 0;
        try {
            wManager.setResource(papers[current++]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Next, create a simple layout with three Buttons: activity_main.xml:

<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/btn_on"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Automatic Wallpaper Change" />

    <Button
        android:id="@+id/btn_off"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop Automatic Wallpaper Change" />

    <Button
        android:id="@+id/btn_clean"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear Wallpaper" />

</LinearLayout>

Finally, in our Activity, instantiate the AlarmManager and set the timed event: MainActivity.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_on;
    private Button btn_off;
    private Button btn_clean;
    private AlarmManager aManager;
    private PendingIntent pi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get AlarmManager object:
        aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        // Specify the Service to be started and the action as Service:
        Intent intent = new Intent(MainActivity.this, WallPaperService.class);
        pi = PendingIntent.getService(MainActivity.this, 0, intent, 0);
        bindViews();
    }

    private void bindViews() {
        btn_on = (Button) findViewById(R.id.btn_on);
        btn_off = (Button) findViewById(R.id.btn_off);
        btn_clean = (Button) findViewById(R.id.btn_clean);
        btn_on.setOnClickListener(this);
        btn_off.setOnClickListener(this);
        btn_clean.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_on:
                aManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 3000, pi);
                btn_on.setEnabled(false);
                btn_off.setEnabled(true);
                Toast.makeText(MainActivity.this, "Automatic wallpaper change set successfully", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_off:
                aManager.cancel(pi);
                btn_off.setEnabled(false);
                btn_on.setEnabled(true);
                Toast.makeText(MainActivity.this, "Automatic wallpaper change stopped", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_clean:
                try {
                    WallpaperManager.getInstance(getApplicationContext()).clear();
                    Toast.makeText(MainActivity.this, "Wallpaper cleared", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
case R.id.btn_off:
    btn_on.setEnabled(true);
    btn_off.setEnabled(false);
    aManager.cancel(pi);
    break;
case R.id.btn_clean:
    try {
        WallpaperManager.getInstance(getApplicationContext()).clear();
        Toast.makeText(MainActivity.this, "Wallpaper cleared successfully~", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
    break;
}

Finally, don't forget to add the permission to set the wallpaper and register our Service in AndroidManifest.xml:

&lt;uses-permission android:name="android.permission.SET_WALLPAPER" />
<service android:name=".WallPaperService"/>

That's very simple~


5. Example Code Download

WallpaperManagerDemo.zip


Summary:

>

Alright, this section introduced the basic usage of WallpaperManager. More advanced features require further exploration on your part.

-1.0 Android Basic Beginner Tutorial

-1.0.1 2015 Latest Android Basic Beginner Tutorial Table of Contents

-1.1 Background and System Architecture Analysis

-1.2 Development Environment Setup

-1.2.1 Developing Android APP with Eclipse + ADT + SDK

-1.2.2 Developing Android APP with Android Studio

-1.3 Solving SDK Update Issues

-1.4 Genymotion Emulator Installation

-1.5.1 Git Tutorial: Basic Operations on Local Repository

-1.5.2 Git: Setting Up Remote Repository on GitHub

-1.6 How to Use 9-Patch Images

-1.7 Interface Prototype Design

-1.8 Project Source Analysis (Various Files, Resource Access)

-1.9 Signing and Packaging Android Applications

-1.11 Decompiling APK to Retrieve Code & Resources

-2.1 Concepts of View and ViewGroup

-2.2.1 LinearLayout (Linear Layout)

-2.2.2 RelativeLayout (Relative Layout)

-2.2.3 TableLayout (Table Layout)

-2.2.4 FrameLayout (Frame Layout)

-2.2.5 GridLayout (Grid Layout)

-2.2.6 AbsoluteLayout (Absolute Layout)

-2.3.1 Detailed Explanation of TextView (Text Box)

-2.3.2 Detailed Explanation of EditText (Input Box)

-2.3.3 Button and ImageButton

-2.3.4 ImageView (Image View)

-2.3.5.RadioButton (Radio Button) & Checkbox (Checkbox)

-2.3.6 ToggleButton and Switch

-2.3.7 ProgressBar (Progress Bar)

-2.3.8 SeekBar (Drag Bar)

-2.3.9 RatingBar (Star Rating Bar)

-2.4.1 ScrollView (Scroll Bar)

-2.4.2 Date & Time Components (Part 1)

-2.4.3 Date & Time Components (Part 2)

-2.4.4 Basic Explanation of Adapter

-2.4.5 Simple Usage of ListView

-2.4.6 Optimization of BaseAdapter

-2.4.7 Focus Issues with ListView

-2.4.8 Solving Checkbox Misalignment in ListView

-2.4.9 Data Update Issues with ListView

-2.5.0 Building a Reusable Custom BaseAdapter

-2.5.1 Implementing Multi-Layout ListView Items

-2.5.2 Basic Usage of GridView (Grid View)

-2.5.3 Basic Usage of Spinner (List Option Box)

-2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)

-2.5.5 Basic Usage of ExpandableListView (Collapsible List)

-2.5.6 Basic Usage of ViewFlipper (Flip View)

-2.5.7 Basic Usage of Toast

-2.5.8 Detailed Explanation of Notification (Status Bar Notification)

-2.5.9 Detailed Explanation of AlertDialog (Dialog Box)

-2.6.0 Basic Usage of Other Common Dialogs

-2.6.1 Basic Usage of PopupWindow (Floating Box)

-2.6.2 Menu

-2.6.3 Simple Usage of ViewPager

-2.6.4 Simple Usage of DrawerLayout (Official Side Menu)

-3.1.1 Event Handling Mechanism Based on Listeners

-3.2 Event Handling Mechanism Based on Callbacks

-3.3 Analysis of Handler Message Passing Mechanism

-3.4 TouchListener vs OnTouchEvent + Multi-Touch

-3.5 Listening for Content Changes in EditText

-3.6 Responding to System Settings Changes (Configuration Class)

-3.7 AsyncTask (Asynchronous Task)

-3.8 Gestures

-4.1.1 Introduction to Activity ```

Follow on WeChat

❮ Android Tutorial Adapter Scrapy Detail ❯