onHandleIntent() - 壁纸更改无法正常工作

时间:2013-04-02 04:42:11

标签: wallpaper intentservice

我正在使用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,用于编写一个dummie字符串“IS Started”,并将onStartCommand()包含在IntentService中,读取dummie 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);
    }
    
  2. 我还在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. 然后我调用了两次服务:第一次因为假的SharedPref将为空,因为它在onHandleIntent()之前被调用,并且onHandleIntent()有机会写入dummie字符串 第二次(现在onHandleIntent()写了一些内容)onStartCommand()读取dummie字符串的内容并显示它们。
    2. 结果: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,如何在活动外获取显示指标?

2 个答案:

答案 0 :(得分: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) {
    }
}

}

答案 1 :(得分:1)

您正在代码中跳过一行

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

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

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