WallpaperManager(Wallpaper Manager)
Introduction to this section:
This section brings you WallpaperManager (wallpaper manager), as its name suggests, it is related to mobile phone wallpapers An API. In this section we will describe the basic usage of WallpaperManager and call the system’s own Wallpaper selection function, set the background of the Activity as the wallpaper background, and write an example of changing the wallpaper regularly~ Okay, no BB, let’s start this section~
Official API Document:WallpaperManager
1.WallpaperManager Basic usage
Related methods
Related methods for setting wallpaper:
- setBitmap(Bitmap bitmap): Set the wallpaper to what the bitmap represents Bitmap
- setResource(int resid): Set the wallpaper to the picture represented by the resid resource
- setStream(InputStream data): Set The wallpaper is set to the picture represented by the 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 the wallpaper is not set
Get the WallpaperManager object
WallpaperManager wpManager =WallpaperManager.getInstance(this);
Permissions required to set the wallpaper
2. Call the wallpaper selection function that comes with the system
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, "选择壁纸")); } });
Run the rendering:
3. Set the background of Activity as wallpaper background
There are two methods, one is to set it with code in Activity, the other is to modify it in AndroidManifest.xml Activity theme~!
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 theme in AndroidManifest.xml:
<activity android:name=".MainActivity" android:theme="@android:style/Theme.Wallpaper.NoTitleBar"/>
4. Demo for changing wallpaper regularly
The AlarmManager (alarm clock service) learned earlier is used here. If you don’t know about it, you can go to: 10.5 AlarmManager (Alarm Clock Service) for study~ Let’s write a Demo~
Running renderings:
##Code implementation:
First we 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; //当前壁纸下标 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; //定义WallpaperManager服务 @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 we 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="开启自动换壁纸" /> <Button android:id="@+id/btn_off" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭自动换壁纸" /> <Button android:id="@+id/btn_clean" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除壁纸" /></LinearLayout>
Then comes our Activity, instantiate aManager here 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); //①获得AlarmManager对象: aManager = (AlarmManager) getSystemService(ALARM_SERVICE); //②指定要启动的Service,并指明动作是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, "自动更换壁纸设置成功", 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, "清除壁纸成功~", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } break; } } }
Finally don’t forget to set the wallpaper Permissions and registration for our Service: AndroidManifest.xml:
<uses-permission android:name="android.permission.SET_WALLPAPER" /> <service android:name=".WallPaperService"/>
Okay, very simple~
5. Download the sample code in this section
Summary of this section:
Okay, this section introduces you to some basics of WallpaperManager Usage ~ You still need more things by yourself Explore~Thank you~!