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 中,该 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);
    }
    

我还在 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. 然后我两次调用该服务:第一次因为虚拟 SharedPref 将是空的,因为它在 onHandleIntent() 之前被调用,并且 onHandleIntent() 有机会写入虚拟字符串第二次(现在 onHandleIntent() 已经写了一些东西)用于 onStartCommand( ) 读取虚拟字符串的内容并显示它们。

结果:Toast 显示“已开始”,意思是 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 时,如何获取活动之外的显示指标?

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

您正在跳过代码中的一行,即

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

这是您的样本量出错的主要原因。现在您可以使用您的方法来计算样本量,而不是使用 sampleSize = 4;

希望你的问题现在得到解决:)

于 2013-04-04T15:07:36.957 回答