Android 壁纸设置 总结
来源:互联网 发布:淘宝排除王 编辑:程序博客网 时间:2024/05/16 14:02
看了很多大神的博客,从来没想过自己写,一直都觉得自己特别懒,哎,终于忍不住了,亲自动手我们能学到更多,闲话不多说,直接上思路:
目前总结了几种设置壁纸的方法:
(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; }}
- Android 壁纸设置 总结
- android 设置壁纸wallpaper 流程总结
- android-设置屏幕壁纸
- Android壁纸设置
- android 设置壁纸
- Android setWallpaper()设置壁纸
- Android设置壁纸
- Android Launcher 设置壁纸
- Android设置壁纸
- android 设置壁纸
- Android设置壁纸
- 设置Android默认壁纸为动态壁纸
- 设置Android默认壁纸为动态壁纸
- 设置Android默认壁纸为动态壁纸
- 设置Android默认壁纸为动态壁纸
- 设置Android默认壁纸为动态壁纸
- android 设置为壁纸代码
- Android 壁纸设置代码 详解
- IOMMU From Wikipedia, the free encyclopedia
- 原生js获取select标签选中值
- GLSL语言基础——OpenGL编程笔记1
- 多个查询结果合并问题
- python(一)列表与元组——列表
- Android 壁纸设置 总结
- 售前项目方案模板
- LeetCode 9. Palindrome Number
- 数据仓库Hive表
- 策略模式
- 第十八周:[Leetcode]111. Minimum Depth of Binary Tree
- 暑期C++之 copy构造函数调用时机
- 漫谈操作系统3 -- 基础知识 (用户态和内核态分离)
- ORA-12514