2013-04-02 1 views
0

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를 추가

  1. 을했고, 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(); 
    } 
  1. 나는 다음 두 번 서비스를 호출 : 그것은 이제합니다 (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로 확장되지 않은 외부 활동을 어떻게해야합니까?

답변

1

. 이제 sampleSize = 4를 사용하는 대신 샘플 크기를 계산하기 위해 메서드를 사용할 수 있습니다.

희망이 그렇게 문제가 이제 해결 :

+0

예 감사합니다 :) – KickAss

1

intentService가 배경 무늬를 임의의 단색으로 변경하면 비트 맵에 문제가있을 수 있습니다.코드가 올바른 비트 맵을 생성하는지 여부를 디버그하고 확인하십시오. 비트 맵 경로, 높이 및 너비도 확인하십시오.

편집 : 귀하는이 샘플 크기가 잘못 점점 된 주된 이유

DisplayMetrics displayMetrics = new DisplayMetrics();  
WindowManager hi = ((WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE)); 
hi.getDefaultDisplay().getMetrics(displayMetrics); 

입니다 코드에서 한 줄을 건너 뛰는

public class intentService extends IntentService { 

Context context; 
private String res; 

public intentService() { 
    super("intentService"); 

} 
@Override 
protected void onHandleIntent(Intent workIntent) { 
    String imagepath = workIntent.getExtras().getString("img"); 

    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 = 4; 
    options.inJustDecodeBounds = false; 
    decodedSampleBitmap = BitmapFactory.decodeFile(imagepath, options); 
    WallpaperManager wm = WallpaperManager.getInstance(this); 
    try { 
     wm.setBitmap(decodedSampleBitmap); 
    } catch (IOException e) { 
    } 
} 

}

+0

참조 편집 2 : – KickAss

+0

위의 편집 2 : – KickAss

+1

제발 봐 난 당신의 코드를 시도하고 의도 서비스 및 활동에서 잘 작동했다. –

관련 문제