看了很多大神的博客,从来没想过自己写,一直都觉得自己特别懒,哎,终于忍不住了,亲自动手我们能学到更多,闲话不多说,直接上思路:
目前总结了几种设置壁纸的方法:
(1)通过在onCreate()方法中设置activity的主题;
setTheme(android.R.style.Theme_Wallpaper_NoTitleBar_Fullscreen);
( 2 )通过在Android Mainifest.xml 文件中设置 ; android:theme=”@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen”
( 3 )调用系统自带的壁纸选择功能,当然这里需要一个intent,表明自己选择壁纸的意图;
Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivity(Intent.createChooser(intent, “选择壁纸”));
(4)利用wallpaperManager,记得添加权限;

WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
或者 @SuppressLint(“ServiceCast”)
WallpaperManager manager =(WallpaperManager)getSystemService(WALLPAPER_SERVICE);
这里如果不添加这个@SuppressLint(“ServiceCast”)会有问题,暂不清楚为什么,可能是API版本问题吧,后续跟进

(5)动态加载壁纸
这个我参考别人的博客,也实现了,但是切换壁纸的速度太慢了,达不到自己的预期。后续改进
AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
Intent serviceIntent = new Intent(MainActivity.this,SetWallpaperService.class);
PendingIntent pi =PendingIntent.getService(this, 0, serviceIntent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 1000, pi);

1. MainActivity.java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;

public class MainActivity extends Activity implements View.OnClickListener{

private Button button1,button2,button3,button4;private ImageView imageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    //设置壁纸为该activity主题,不需要设置权限,或者可以在androidManifest中设置theme    //setTheme(android.R.style.Theme_Wallpaper_NoTitleBar_Fullscreen);    //setTheme(android.R.style.Theme_Wallpaper_NoTitleBar);    setContentView(R.layout.activity_main);    button1 = (Button) findViewById(R.id.button1);    button2 = (Button) findViewById(R.id.button2);    button3 = (Button) findViewById(R.id.button3);    button4 = (Button) findViewById(R.id.button4);    button1.setOnClickListener(this);    button2.setOnClickListener(this);    button3.setOnClickListener(this);    button4.setOnClickListener(this);}@Overridepublic void onClick(View view) {    switch (view.getId()){        case R.id.button1:            //调用系统自带的壁纸选择功能,不需要添加权限            Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);            startActivity(Intent.createChooser(intent, "选择壁纸"));            break;        case R.id.button2:            //利用WallpaparManager,添加权限set_wallpaper            WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);            try {                wallpaperManager.setBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.fffg));            } catch (IOException e) {                e.printStackTrace();            }            //getSystemService(WALLPAPER_SERVICE)获取wallpaperManager会出问题          /*  @SuppressLint("ServiceCast")            WallpaperManager manager =(WallpaperManager)getSystemService(WALLPAPER_SERVICE);            try {                manager.setBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.pic1));            } catch (IOException e) {                e.printStackTrace();            }*/            break;        case R.id.button3:            //动态加载壁纸,定时加载图片,需要AlarmManager            AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);            Intent serviceIntent = new Intent(MainActivity.this,SetWallpaperService.class);            PendingIntent pi =PendingIntent.getService(this, 0, serviceIntent, 0);            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 1000, pi);            /*//取消操作            alarmManager.cancel(pi);            //清除操作            WallpaperManager manager =WallpaperManager.getInstance(this);            try {                manager.clear();            } catch (IOException e) {                e.printStackTrace();            }*/            break;        default:            break;    }}

}

2 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><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:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="调用系统选择手机壁纸"        android:id="@+id/button1" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="利用WallpaparManager"        android:id="@+id/button2" />    <Button        android:layout_width="207dp"        android:layout_height="wrap_content"        android:text="动态加载壁纸"        android:id="@+id/button3" />LinearLayout>

4 SetWallpaperService.class

package tingting.com.mytest;import android.app.Service;import android.app.WallpaperManager;import android.content.Intent;import android.os.IBinder;import java.io.IOException;/** * Created on 2017/6/28. */public class SetWallpaperService extends Service{    private int current = 0;  //当前壁纸下标    private int[] papers = new int[]{R.drawable.ffrhh, R.drawable.pic1,                                        R.drawable.fffg, R.drawable.jjde};    private WallpaperManager wManager = null;    @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 {           /* Bitmap bitmap =BitmapFactory.decodeResource(getResources(),papers[current]);            wManager.setBitmap(bitmap);*/            //切换第二张            wManager.setResource(papers[current++]);        } catch (IOException e) {            e.printStackTrace();        }        return START_STICKY;    }    @Override    public IBinder onBind(Intent intent) {        return null;    }}

更多相关文章

  1. Android客制化------在设置中加入RAM flash计算
  2. Android(安卓)几种弹框样式 自定义Dialog PopupWindow的使用
  3. Android(安卓)XML设置圆角边框
  4. 直播平台软件开发Android(安卓)Activity横竖屏切换生命周期详解
  5. Android(安卓)Toolbar 使用总结
  6. Android中Activity常用功能设置小结(包括全屏、横竖屏等)
  7. Android(安卓)ListView中动态显示和隐藏Header&Footer的方法(转)
  8. 解读Android之HttpURLConnection
  9. Android(安卓)Google Map设置自动zoom scale

随机推荐

  1. Android(安卓)源码结构
  2. Android可缩放矢量图形(SVG)
  3. Android中的Selector的用法
  4. Android窗口机制(二)Window,PhoneWindow,Deco
  5. Android的IPC机制
  6. [Android]android源码下载&Eclipse关联an
  7. 【android】EditText属性大全
  8. 随笔:Android不爱C++
  9. 【Android常用控件】EditText常用属性【
  10. Android短信的发送和广播接收者实现短信