Java 如果我在设备中更改墙纸,如何跟踪事件
我正在创建一个应用程序,其中我正在设置一个具有墙纸洗牌效果的图像库。若应用程序在后台,则洗牌效果有效。 但问题是,当我将我的壁纸从我的设备更改为设置在主屏幕上时(不是从应用程序,而是选择设备的形式壁纸),该壁纸将设置在我的主屏幕上,并且洗牌效果仍将继续(因为我认为服务正在运行)。有人能告诉我是什么问题以及如何解决这个问题吗。这是我的服务课:Java 如果我在设备中更改墙纸,如何跟踪事件,java,android,service,Java,Android,Service,我正在创建一个应用程序,其中我正在设置一个具有墙纸洗牌效果的图像库。若应用程序在后台,则洗牌效果有效。 但问题是,当我将我的壁纸从我的设备更改为设置在主屏幕上时(不是从应用程序,而是选择设备的形式壁纸),该壁纸将设置在我的主屏幕上,并且洗牌效果仍将继续(因为我认为服务正在运行)。有人能告诉我是什么问题以及如何解决这个问题吗。这是我的服务课: public class WallpaperService extends Service { ArrayList<String> arrayL
public class WallpaperService extends Service {
ArrayList<String> arrayList = new ArrayList<>();
int counter = 0;
Bitmap bmImg = null;
int seconds;
public WallpaperService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flag, int startId) {
super.onStartCommand(intent, flag, startId);
arrayList = intent.getStringArrayListExtra("image_url");
seconds = intent.getIntExtra("seconds", 5000 * 60);
Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
SaveWallpaperAsync async = new SaveWallpaperAsync();
async.execute();
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onTaskRemoved(Intent rootIntent){
Intent restartServiceTask = new Intent(getApplicationContext(),this.getClass());
restartServiceTask.setPackage(getPackageName());
PendingIntent restartPendingIntent =PendingIntent.getService(getApplicationContext(), 1,restartServiceTask, PendingIntent.FLAG_ONE_SHOT);
AlarmManager myAlarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
myAlarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartPendingIntent);
}
public class SaveWallpaperAsync extends AsyncTask<String, Integer, Void> {
URL ImageUrl;
Bitmap bmImg = null;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
Toast.makeText(WallpaperService.this, "downloading", Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(String... args) {
// TODO Auto-generated method stub
InputStream is = null;
for (int i = 0; i < arrayList.size(); i++) {
try {
ImageUrl = new URL(arrayList.get(i));
HttpURLConnection conn = (HttpURLConnection) ImageUrl.openConnection();
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
bmImg = BitmapFactory.decodeStream(is, null, options);
saveImageToInternalStorage(bmImg, i);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
Toast.makeText(WallpaperService.this, "Downloaded", Toast.LENGTH_SHORT).show();
createNotificationIcon();
}
}
protected String saveImageToInternalStorage(Bitmap bitmap, int index) {
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath()
+ "/Dark Wallpapers/");
if (!dir.exists()){
dir.mkdirs();
}
File file = new File(dir, "UniqueFileName" + index + ".jpg");
try {
OutputStream stream = null;
stream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
Uri savedImageURI = Uri.parse(file.getAbsolutePath());
return savedImageURI.toString();
}
private Bitmap loadImageFromStorage(int counter) {
Bitmap b = null;
try {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Dark Wallpapers/", "UniqueFileName" + counter + ".jpg");
b = BitmapFactory.decodeStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return b;
}
public void createNotificationIcon() {
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
counter += 1;
if (counter < arrayList.size()) {
try {
wallpaperManager.setBitmap(loadImageFromStorage(counter));
wallpaperManager.suggestDesiredDimensions(1080, 1920);
} catch (IOException e) {
e.printStackTrace();
}
} else if (counter == arrayList.size()) {
counter = 0;
try {
wallpaperManager.setBitmap(loadImageFromStorage(counter));
wallpaperManager.suggestDesiredDimensions(1080, 1920);
} catch (IOException e) {
e.printStackTrace();
}
}
}
},
0,
seconds);
}}
如果我理解的很好,你想得到一个“事件”(或只是想知道…)当家庭壁纸改变。是这样吗? 如果检测到用户希望从那时起设置固定墙纸,您可能希望停止服务 没有任何事件可以这样做,但有一个解决方案:记住你从应用程序中自动更改的壁纸(可能记住/xxxxx/files文件夹中文件的路径+文件名),然后在尝试设置新壁纸之前检查当前壁纸,使用以下方法:
wallpermanager=wallpermanager.getInstance(this);
Drawable wallparDrawable=wallparkmanager.getDrawable()代码>
通过这种方式,如果当前壁纸与您上次记忆的文件名相同,则99%的可能性是用户没有自行设置固定的壁纸。(剩下的1%可能是用户手动设置与上次自动设置的墙纸相同的墙纸,但这种情况非常罕见)
Intent intent = new Intent(CategoryActivity.this, WallpaperService.class);
intent.putExtra("image_url", img_urls);
intent.putExtra("seconds", 60);
intent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
startService(intent);