Android-Service实现手机壁纸自动更换

 更新时间:2018年11月28日 15:25:50   作者:水中鱼之1999  
这篇文章主要为大家详细介绍了Android-Service实现手机壁纸自动更换,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

最新评论