您好,登錄后才能下訂單哦!
本文實例為大家分享了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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。