Android-Service实现手机壁纸自动更换
程序员文章站
2023-10-29 12:56:40
本文实例为大家分享了android service实现自动更换手机壁纸的具体代码,供大家参考,具体内容如下
先看下效果:
使用界面:
划重点,使用servic...
本文实例为大家分享了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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 广州美食,在广州吃什么好呢
下一篇: ajax提交form表单问题