我在一段时间后使用服务更改壁纸并且工作正常,现在问题是"如果用户从任何其他应用程序设置壁纸,因为我的应用程序服务正在运行而无法显示背景"如果用户设置任何其他应用程序壁纸,我该如何阻止它。
public class Wallpaper extends Service {
SharedPreferences customSharedPreference;
int img[] = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h,
R.drawable.i, R.drawable.j, R.drawable.k, R.drawable.l,
R.drawable.m, R.drawable.n, R.drawable.o };
WallpaperManager wpm;
int delay = 0;
Timer timer;
int index = -1;
int int_delay = 500;
public Wallpaper() {
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
delay = 10;
customSharedPreference = getSharedPreferences(
"com.janbark.live.wall.paper", Context.MODE_PRIVATE);
int_delay = Integer.parseInt(customSharedPreference.getString(
"userChoice", "500").toString());
timer = new Timer();
}
@Override
public void onStart(Intent intent, int startId) {
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
index++;
if (int_delay != Integer.parseInt(customSharedPreference
.getString("userChoice", "500").toString())) {
timer.cancel();
}
Log.d("TAGDELAY", "" + int_delay);
wpm = WallpaperManager.getInstance(getApplicationContext());
if (index <= 14) {
if(!customSharedPreference.getBoolean("Live", true)){
timer.cancel();
stopService(new Intent(Wallpaper.this,Wallpaper.class));
}
try {
wpm.setResource(img[index]);
} catch (IOException e) {
e.printStackTrace();
}
if (index == 14)
index = -1;
}
}
}, delay, int_delay);
}
@Override
public void onDestroy() {
}
}
答案 0 :(得分:1)
如果你设置的壁纸是实时壁纸,看起来就是这种情况,请使用:
boolean wallpaperWasSetByAnotherApp() {
WallpaperManager wp_mngr = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
WallpaperInfo info = wp_mngr.getWallpaperInfo();
if (info == null) {
// wallpaper is a static image
return true;
} else {
// wallpaper is live, check implementing service
return !info.getComponent().equals(new ComponentName(this, getClass()));
}
}