Android Basics Getting Started tutorial--10.9 wallpapermanager (wallpaper manager)

Source: Internet
Author: User

Android Basics Getting Started tutorial--10.9 wallpapermanager (wallpaper manager)

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section:

This section brings you a Wallpapermanager (wallpaper manager), such as its name, is the phone wallpaper related
An API, in this section we will describe the basic usage of the next Wallpapermanager, calling the system's own
Wallpaper selection feature, set activity background as wallpaper background, and write an example of a timed change wallpaper ~
OK, no BB, start this section ~
Official API Document : Wallpapermanager

Basic usage related methods of 1.WallpaperManager

Ways to set up your 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

Other methods:
- Clear (): Clear wallpaper, set back to system default wallpaper
- getdesiredminimumheight(): Min Wallpaper Height
- getdesiredminimumwidth(): Min. wallpaper width
- getdrawable(): Get the current system wallpaper and return the system default wallpaper if no wallpaper is set
- Getwallpaperinfo(): Join current wallpaper is live wallpaper, return live wallpaper Info
- peekdrawable(): Get the current system wallpaper and return NULL if no wallpaper is set
...

Get Wallpapermanager Object
WallpaperManager wpManager =WallpaperManager.getInstance(this);
Set the permissions required for wallpaper
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
2. Call the system's own wallpaper selection feature
        Button btn_set = (Button) findViewById(R.id.btn_set);        btn_set.setOnClickListener(new View.OnClickListener() {            @Override            publicvoidonClick(View v) {                new Intent(Intent.ACTION_SET_WALLPAPER);                "选择壁纸"));            }        });

Run :

3. Set the background of the activity as a wallpaper background

There are two ways to do this, one is to set the code in the activity, and the other is to modify it in the Androidmanifest.xml
The subject of activity!

method One: Set in activity :

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

Method Two: Androidmanifest.xml Modify theme:

  <activity android:name=".MainActivity"  android:theme="@android:style/Theme.Wallpaper.NoTitleBar"/>
4. The demo of changing the wallpaper regularly

Here to learn the Alarmmanager (Alarm clock service), if you do not understand it can be to:
Android Basics Getting Started tutorial--10.5 alarmmanager (Alarm clock service) to learn ~
Now, let's write a demo~.

Run :

Code Implementation :

First of all, let's write a service to change the wallpaper regularly:Wallpaperservice.java

/** * Created by Jay on 2015/11/13 0013. * * Public  class wallpaperservice extends Service {    Private intCurrent =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};PrivateWallpapermanager Wmanager =NULL;//define Wallpapermanager services    @Override     Public void onCreate() {Super. OnCreate (); Wmanager = Wallpapermanager.getinstance ( This); }@Override     Public int Onstartcommand(Intent Intent,intFlagsintStartid) {if(Current >=4) Current =0;Try{Wmanager.setresource (papers[current++]); }Catch(Exception e) {E.printstacktrace ();}returnStart_sticky; }@Override     PublicIBinderOnbind(Intent Intent) {return NULL; }}

Then a simple layout, 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 auto-change wallpaper"/> <button android:ID="@+id/btn_off"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:text="Turn off auto-change wallpaper"/> <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 timed events ~:Mainactivity.java:

 Public  class mainactivity extends appcompatactivity implements View . Onclicklistener {    PrivateButton btn_on;PrivateButton Btn_off;PrivateButton Btn_clean;PrivateAlarmmanager Amanager;PrivatePendingintent Pi;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//① Get Alarmmanager object:Amanager = (Alarmmanager) getsystemservice (Alarm_service);//② Specifies the service to start and indicates that the action is Servce:Intent Intent =NewIntent (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 ()) { CaseR.id.btn_on:amanager.setrepeating (Alarmmanager.rtc_wakeup,0, the, pi); Btn_on.setenabled (false); Btn_off.setenabled (true); Toast.maketext (mainactivity. This,"Auto-change wallpaper setup succeeded", Toast.length_short). Show (); Break; CaseR.id.btn_off:btn_on.setenabled (true); Btn_off.setenabled (false); Amanager.cancel (PI); Break; CaseR.id.btn_clean:Try{wallpapermanager.getinstance (Getapplicationcontext ()). Clear (); Toast.maketext (mainactivity. This,"Clear Wallpaper Success ~", Toast.length_short). Show (); }Catch(IOException e)                {E.printstacktrace (); } Break; }    }}

Finally, don't forget to add permission to set up your wallpaper and register for our service:androidmanifest.xml:

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

OK, very simple ~

5. Sample code download for this section

Wallpapermanagerdemo.zip

This section summarizes:

OK, this section introduces some basic usage of the next Wallpapermanager ~ more things you need yourselves
To explore ~ Thank You ~!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started tutorial--10.9 wallpapermanager (wallpaper manager)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.