gpt4 book ai didi

java - 如果墙纸是从另一个应用程序设置的,如何停止服务

转载 作者:行者123 更新时间:2023-11-30 02:49:06 25 4
gpt4 key购买 nike

我正在使用一项服务在一段时间后更改墙纸并且它工作正常,现在的问题是“如果用户从任何其他应用程序设置墙纸但由于我的应用程序服务在后台运行而无法显示”我怎么能如果用户设置任何其他应用壁纸,请停止它。

   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() {

}
}

最佳答案

如果您设置的壁纸是动态壁纸,并且看起来是这种情况,请使用:

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()));
}
}

关于java - 如果墙纸是从另一个应用程序设置的,如何停止服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24518927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com