Я использую IntentService для смены обоев в фоновом режиме. Используя приведенный ниже код внутри основного класса Activity, обои правильно меняются на изображение, но когда код добавляется в onHandleItent(), фон каждый раз меняется на другой сплошной цвет. Это проблема с памятью или что?
Код внутри класса Activity - работает правильно:
public void ChangeWallpaper(String imagepath) {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels << 2;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(imagepath, options);
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
decodedSampleBitmap = BitmapFactory.decodeFile(imagepath, options);
WallpaperManager wm = WallpaperManager.getInstance(this);
try {
wm.setBitmap(decodedSampleBitmap);
} catch (IOException e) {
}
}
Код внутри onHandleIntent() - НЕ работает корректно:
@Override
protected void onHandleIntent(Intent workIntent) {
String imagepath = workIntent.getStringExtra("String");
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager hi = ((WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE));
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels << 2;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(imagepath, options);
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
decodedSampleBitmap = BitmapFactory.decodeFile(imagepath, options);
WallpaperManager wm = WallpaperManager.getInstance(this);
try {
wm.setBitmap(decodedSampleBitmap);
} catch (IOException e) {
}
}
есть идеи?
РЕДАКТИРОВАНИЕ 2 — ПРОБЛЕМА (нерешенная)
Оказывается, что-то не так с самим onHandleIntent().
Чтобы проверить это, я сделал
Я добавил SharedPreferences в onHandleIntent(), чтобы написать фиктивную строку «IS Started», и включил onStartCommand() в IntentService, который считывает фиктивные SharedPreferences и отображает сохраненную строку в Toast:
public int onStartCommand(Intent intent, int flags, int startId) { SharedPreferences settings2 = getSharedPreferences("IS", 0); String IS = settings2.getString("IS",""); Toast.makeText(this, "" + IS, Toast.LENGTH_SHORT).show(); return super.onStartCommand(intent,flags,startId); }
Я также добавил фиктивную запись в SharedPrefs в onIntentHandle следующим образом:
@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences settings = getSharedPreferences("IS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("IS","IS Started");
editor.commit();
}
- Затем я дважды вызвал службу: в первый раз, потому что фиктивный SharedPref будет пустым, поскольку он вызывается перед onHandleIntent(), и у onHandleIntent() есть шанс записать фиктивную строку. Второй раз (теперь, когда onHandleIntent() что-то написал) для onStartCommand( ), чтобы прочитать содержимое фиктивной строки и отобразить его.
РЕЗУЛЬТАТ: Toast отображает «IS STARTed», что означает, что вызывается onHandleIntent().
НО Затем, чтобы определить проблему с обоями, я поместил Toast прямо в onHandleIntent(), чтобы посмотреть, работает ли он следующим образом:
@Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(this, "onHandleWorks", Toast.LENGTH_SHORT).show();
}
РЕЗУЛЬТАТ: Он не показал!
Итак, почему onHandleIntent() вызывается и распознает только НЕКОТОРЫЕ команды?
ИЗМЕНИТЬ 3
Код внутри onHandleIntent() - в настоящее время как есть:
public class intentClass extends IntentService {
public intentClass() {
super("intentClass");
}
@Override
protected void onHandleIntent(Intent workIntent) {
String imagepath = workIntent.getStringExtra("String");
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager hi = ((WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE));
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels << 2;
// the problem is here - the above command doesn't retrieve the Display size
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(imagepath, options);
options.inSampleSize = 4; // this is what needs to be calculated.
options.inJustDecodeBounds = false;
decodedSampleBitmap = BitmapFactory.decodeFile(imagepath, options);
WallpaperManager wm = WallpaperManager.getInstance(this);
try {
wm.setBitmap(decodedSampleBitmap);
} catch (IOException e) {
}
}
}
Как получить показатели отображения вне активности, когда активность не распространяется на IntentService?