【问题标题】:onHandleIntent() - Wallpaper change not working correctlyonHandleIntent() - 壁纸更改无法正常工作
【发布时间】:2013-04-05 12:38:04
【问题描述】:

我正在使用 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() 正在被调用。

但是为了识别墙纸问题,我直接在 onHandleIntent() 中放了一个 Toast,看看它是否像这样工作:

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

activity 未扩展到 IntentService 时,如何获取 Activity 外的显示指标?

【问题讨论】:

    标签: wallpaper intentservice


    【解决方案1】:

    您正在跳过代码中的一行

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

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

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

    【讨论】:

      【解决方案2】:

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

      }

      【讨论】:

      • 我试过你的代码,它在意图服务和活动中运行良好。
      • 您不需要 onStartCommand() 方法来实现这一点。我在虚拟项目中复制并粘贴了您的代码,并从 SD 卡中选择了一个图像,它工作得很好。请检查我唯一拥有的编辑代码更改是 options.inSampleBitmap = 4 可能是在计算样本时出错了。
      • 非常感谢 :) 你是对的,将 inSampleBitmap 更改为 4 修复了它。它的计算方法有问题。再次感谢你:) 我已经坚持了 2 天!
      • 没问题,我很乐意帮助你。:)
      • 我有一个新问题。如果我想将 calculateInSample 方法添加到主 onHandle 方法 - 在“options.inSampleSize = 4;”之前并在那里计算,我将如何更改 inCalculateMethod?我试图做到这一点,但我无法让它发挥作用。见编辑 3 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      相关资源
      最近更新 更多