0

バックグラウンドで壁紙を変更するために 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() 自体に問題があることがわかりました。

それをテストするために、私はやった

  1. onHandleIntent() に SharedPreferences を追加してダミー文字列 "IS Started" を書き込み、onStartCommand() を IntentService に含めました。これはダミーの SharedPreferences を読み取り、保存された文字列をトーストに表示します。

    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 にダミーの書き込みを追加しました。

    @Override
    protected void onHandleIntent(Intent intent) {
        SharedPreferences settings = getSharedPreferences("IS", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("IS","IS Started");
        editor.commit();
    }
  1. 次に、サービスを 2 回呼び出しました。1 回目は onHandleIntent() の前に呼び出されるとダミーの SharedPref が空になり、onHandleIntent() がダミー文字列を書き込む可能性があるためです。 ) ダミー文字列の内容を読み取って表示します。

結果: トーストには、onHandleIntent() が呼び出されていることを意味する "IS STarted" が表示されます。

しかし、次に、壁紙の問題を特定するために、トーストを 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 に拡張されていない場合、アクティビティ以外のディスプレイ メトリックを取得するにはどうすればよいですか?

4

2 に答える 2

1

IntentService が壁紙をランダムな単色に変更している場合、問題はビットマップにある可能性があります。コードが正しいビットマップを生成しているかどうかをデバッグして確認します。ビットマップのパス、高さ、幅も確認してください。

編集:

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

}

于 2013-04-02T05:34:42.737 に答える
1

コード内の 1 行をスキップしています。

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

これが、サンプル サイズが間違っていた主な理由です。sampleSize = 4 を使用する代わりに、独自のメソッドを使用してサンプル サイズを計算できるようになりました。

あなたの問題が今解決されることを願っています:)

于 2013-04-04T15:07:36.957 に答える