IntentService를 사용하여 백그라운드에서 배경 화면을 변경하고 있습니다. 아래의 코드를 주 활동 클래스 내에서 사용하면 벽지가 이미지로 올바르게 변경되지만 코드가 onHandleItent()에 추가되면 매번 배경이 다른 단색으로 변경됩니다. 이것은 메모리 문제인가요? 활동 클래스 내부onHandleIntent() - 바탕 화면 변경이 올바르게 작동하지 않습니다.
코드 - 올바르게 작동 : onHandleIntent() 내부
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) {
}
}
코드 - 제대로 작동하지 않습니다 :
@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() 자체에 문제가 있습니다.
그것을 테스트하기 위해, 나는 내가 dummie 문자열 "시작"을 작성하기 위해 onHandleIntent()로 된 SharedPreferences를 추가
- 을했고, dummie의 된 SharedPreferences를 읽고 IntentService에 onStartCommand()를 포함
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); }
나는이 같은 onIntentHandle에 SharedPrefs에 dummie 쓰기를 추가 : 토스트에 저장된 문자열을 표시
@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences settings = getSharedPreferences("IS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("IS","IS Started");
editor.commit();
}
- 나는 다음 두 번 서비스를 호출 : 그것은 이제합니다 (dummie 문자열 그런 onHandleIntent을 두 번째 시간을 쓸 onHandleIntent()와 onHandleIntent 수있는 기회() 전에 호출되는대로 더미 SharedPref becuase 처음 비어 있습니다()는 dummie 문자열의 내용을 읽고 표시하기 위해 onStartCommand()에 대해 무언가를 작성했습니다.
결과 : onHandleIntent()가 호출됨을 의미하는 "IS가 시작되었습니다"라는 토스트가 표시되었습니다. 그것은 다음과 같이 작동하는 경우
그러나 나는 onHandleIntent 내부에 직접 토스트를 넣어 벽지 문제를 식별하기 위해()를 참조하십시오 :
@Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(this, "onHandleWorks", Toast.LENGTH_SHORT).show();
}
결과를 : 그것은 보이지 않았다!
왜 onHandleIntent()가 호출되고 일부 명령 만 인식합니까? onHandleIntent() 내부
EDIT 3
코드 - 현재는 그대로 :
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로 확장되지 않은 외부 활동을 어떻게해야합니까?
예 감사합니다 :) – KickAss