后台服务,可自动从少数所选图像更改设备的壁纸。它在lolipop和post lolipop设备上完美运行。但是当我在kitkat设备上尝试时会出现问题。该服务保持活跃但不起作用,因为我看不到壁纸的变化。
在kitkat设备上,当我转到“设置” - >“应用” - >“运行”时。它显示了为我的应用程序运行的1个服务,因为它是我在整个应用程序中实现的唯一服务,我确信该服务是活的。此外, greenify 表示该服务仍在运行。
用户可以下载存储在特定文件夹中的图像。当用户设置壁纸应该更改的间隔并单击按钮以启动它时,将启动后台服务,该服务从先前下载的图像中获取位图,并在指定的时间间隔后将其设置为墙纸。
<service android:name=".AutomaticWallpaperChanger" android:enabled="true" android:exported="false"/>
public class AutomaticWallpaperChanger extends Service {
// constant
public static long NOTIFY_INTERVAL;
Boolean shuffle;
int wallpaper;
SharedPreferences mySharedPreferences;
SharedPreferences.Editor editor;
// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
private Timer mTimer = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("onstart gallery dialog","called");
editor.putBoolean("service not running",false).commit();
return START_STICKY;
}
@Override
public void onCreate() {
// cancel if already existed
if (mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
mySharedPreferences = getApplicationContext().getSharedPreferences("preferences", Activity.MODE_PRIVATE);
editor= mySharedPreferences.edit();
AutomaticWallpaperChanger.NOTIFY_INTERVAL=mySharedPreferences.getInt("interval",3600);//3600*1000)*1000;
AutomaticWallpaperChanger.NOTIFY_INTERVAL*=1000;
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}
class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {
Log.d("timer","Running");
// run on another thread
mHandler.post(new Runnable() {
@Override
public void run() {
//Toast.makeText(getApplicationContext(),
//"running",
// Toast.LENGTH_SHORT).show();
// display toast
AsyncTask asyncTask= new AsyncTask() {
@Override
protected Object doInBackground(Object[] objects) {
File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "WallR"+ File.separator + "Gallery");
if(folder.exists()) {
File[] listOfFiles = folder.listFiles();
shuffle = mySharedPreferences.getBoolean("checked", false);
Log.d("shuffle", String.valueOf(shuffle));
if (shuffle != true) {
if (listOfFiles.length != 0) {
wallpaper = mySharedPreferences.getInt("wallpaper", 0);
wallpaper++;
editor.putInt("wallpaper", wallpaper);
editor.commit();
if (wallpaper >= listOfFiles.length) {
wallpaper = 0;
editor.putInt("wallpaper", wallpaper);
editor.commit();
}
Log.d("wallpaperr number", String.valueOf(wallpaper));
Bitmap bitmap = BitmapFactory.decodeFile(listOfFiles[wallpaper].getPath());
WallpaperManager mWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
mWallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
bitmap.recycle();
}
}
else {
if (listOfFiles.length != 0) {
wallpaper = mySharedPreferences.getInt("wallpaper", 0);
wallpaper++;
editor.putInt("wallpaper", wallpaper);
editor.commit();
if (wallpaper >= listOfFiles.length) {
wallpaper = 0;
editor.putInt("wallpaper", wallpaper);
editor.commit();
}
shuffleArray(listOfFiles);
Log.d("wallpaperr number", String.valueOf(wallpaper));
Bitmap bitmap = BitmapFactory.decodeFile(listOfFiles[wallpaper].getPath());
WallpaperManager mWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
mWallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
bitmap.recycle();
}
}
}
return null;
}
};
asyncTask.execute();
}
});
}
void shuffleArray(File[] ar)
{
// If running on Java 6 or older, use `new Random()` on RHS here
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
File a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
}
@Override
public void onDestroy() {
editor.putBoolean("service not running", true).commit();
mTimer.cancel();
mTimer.purge();
}
}
对于棒棒糖,棉花糖和牛轧糖设备,该服务运行良好。但对于kitkat设备,只要应用程序处于打开状态,它就会运行。在我从最近的应用程序中刷出应用程序后,该服务仍保持活动状态,但不再更改壁纸。