Android-Service实现手机壁纸自动更换
作者:水中鱼之1999 时间:2022-05-21 23:15:35
本文实例为大家分享了Android Service实现自动更换手机壁纸的具体代码,供大家参考,具体内容如下
先看下效果:
使用界面:
划重点,使用service前别忘了给相应的service添加服务
<service android:name=".ChangeService"></service>
具体实现:
首先定义ChangeService类,继承Service访问系统实现壁纸切换
public class ChangeService extends Service {
int[] wallpapers = new int[]{
R.drawable.a2, R.drawable.a3, R.drawable.a4
};
WallpaperManager wallpaperManager;
int current = 0;
@Override
public void onCreate() {
super.onCreate();
wallpaperManager = WallpaperManager.getInstance(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (current >= 3) {
current = 0;
} try {
wallpaperManager.setResource(wallpapers[current++]);
} catch (IOException e) {
e.printStackTrace();
} return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
这里我们用闹钟访问pendingIntent来实现壁纸更换:
public class MainActivity extends Activity {
private Button mStart;
private Button mStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStart = (Button) findViewById(R.id.start);
mStop = (Button) findViewById(R.id.stop);
final Intent intent = new Intent(MainActivity.this, ChangeService.class);
final PendingIntent pi = PendingIntent.getService(MainActivity.this, 0, intent, 0);
mStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, 2000, pi);
mStart.setEnabled(false); mStop.setEnabled(true);
Toast.makeText(MainActivity.this, "壁纸定时更换启动成功啦", Toast.LENGTH_SHORT).show();
}
});
mStop.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
mStart.setEnabled(true);
mStop.setEnabled(false);
AlarmManager alarmManager = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
alarmManager.cancel(pi);
}
});
}
}
布局设计:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Start"
android:textSize="25dp"
android:textColor="#ffffffff"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Stop"
android:textSize="25dp"
android:textColor="#ffffffff"/>
</LinearLayout>
</RelativeLayout>
来源:https://blog.csdn.net/qq_43377749/article/details/84347766
标签:Android,Service,手机壁纸
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 退出程序的若干方法总结
2022-04-01 08:29:47
java之CSV大批量数据入库的实现
2021-06-30 15:53:49
![](https://img.aspxhome.com/file/2023/0/66170_0s.png)
Spring Batch入门教程篇
2022-02-15 01:53:01
![](https://img.aspxhome.com/file/2023/4/83014_0s.png)
Java 8新特性方法引用详细介绍
2023-06-22 08:31:50
详解feign调用session丢失解决方案
2021-08-29 20:38:20
![](https://img.aspxhome.com/file/2023/1/82001_0s.png)
Spring Boot集成Spring Cache过程详解
2023-08-02 19:20:16
![](https://img.aspxhome.com/file/2023/3/84583_0s.png)
详解Java内存泄露的示例代码
2023-06-08 03:34:51
Springboot使用POI实现导出Excel文件示例
2021-09-22 08:18:31
![](https://img.aspxhome.com/file/2023/8/61398_0s.jpg)
详解Java利用深度优先遍历解决迷宫问题
2022-08-20 02:46:54
Java实现简单的日历界面
2021-10-08 03:13:01
![](https://img.aspxhome.com/file/2023/7/88147_0s.jpg)
C#实现让ListBox适应最大Item宽度的方法
2022-06-30 16:41:26
SpringBoot使用Spark过程详解
2021-07-30 06:19:21
![](https://img.aspxhome.com/file/2023/1/66191_0s.png)
WinForm导出文件为Word、Excel、文本文件的方法
2022-07-08 20:07:16
java 创建线程的几种方式
2023-10-29 19:40:16
![](https://img.aspxhome.com/file/2023/5/58695_0s.png)
c# 钩子学习笔记
2023-03-31 20:54:22
使用genymotion访问本地上Tomcat上数据的方法
2022-11-23 05:51:43
![](https://img.aspxhome.com/file/2023/3/91003_0s.jpg)
java 中锁的性能提高办法
2021-07-28 10:50:28
C#使用Word中的内置对话框实例
2023-08-11 08:04:20
深入Android Handler与线程间通信ITC的详解
2021-12-09 16:02:17
![](https://img.aspxhome.com/file/2023/3/97843_0s.png)
java 抽象类的实例详解
2023-06-08 05:52:40