Android WallpaperManager (Wallpaper Manager)

Introduction to this section:

What this section brings to you is WallpaperManager. As its name suggests, it is an API related to mobile phone wallpapers. In this section we will describe the basic usage of WallpaperManager, call the system's own wallpaper selection function, and add Activity Set the background as wallpaper background, and write an example of changing wallpaper regularly~ Start this section~

Official API documentation : WallpaperManager


1.Basic usage of WallpaperManager

Related methods

Related methods of setting wallpaper:

  • setBitmap (Bitmap bitmap): Set the wallpaper to the bitmap represented by bitmap
  • setResource (int resid): Set the wallpaper to the picture represented by the resid resource
  • setStream (InputStream data): Set the wallpaper to the picture represented by data data

Other methods:

  • clear (): clear the wallpaper and set it back to the system default wallpaper
  • getDesiredMinimumHeight (): Minimum wallpaper height
  • getDesiredMinimumWidth (): Minimum wallpaper width
  • getDrawable (): Get the current system wallpaper. If no wallpaper is set, return the system default wallpaper.
  • getWallpaperInfo (): Add that the current wallpaper is a dynamic wallpaper and return the dynamic wallpaper information
  • peekDrawable (): Get the current system wallpaper, return null if no wallpaper is set

Get WallpaperManager object

WallpaperManager wpManager =WallpaperManager.getInstance(this);

Permissions required to set wallpaper

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

2. Call the system’s own wallpaper selection function

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, "Choose Wallpaper"));
        }
    });

Operation rendering :


3. Set the background of the Activity as the wallpaper background

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

Method 1: Set 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 2: Modify the theme in AndroidManifest.xml :

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

4. Demo of regularly changing wallpapers

The AlarmManager (Alarm Clock Service) we learned earlier is used here. If you don’t know about it, you can go to:  10.5 AlarmManager (Alarm Clock Service) to learn~ Let’s write a Demo~

Operation rendering :

Code implementation :

First, let's write a Service that changes wallpaper regularly: WallPaperService.java

/**
 * Created by Jay on 2015/11/13 0013.
 */
public class WallPaperService extends Service {

    private int current = 0; //Current wallpaper subscript
    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;
    }
}

Then make 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="Turn on automatic wallpaper change" />

    <Button
        android:id="@+id/btn_off"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Turn off automatic wallpaper change" />

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

</LinearLayout>

Next is our Activity, where we instantiate aManager and set up scheduled events~: 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 the AlarmManager object:
        aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        //②Specify the Service to be started and indicate that the action is Servce:
        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, "Automatically change wallpaper setting successfully", Toast.LENGTH_SHORT).show();
                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, "Clear wallpaper 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: AndroidManifest.xml :

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

Okay, very simple~

Guess you like

Origin blog.csdn.net/leyang0910/article/details/132438349