onHandleIntent()-墙纸更改无法正常工作 [英] onHandleIntent() - Wallpaper change not working correctly

查看:122
本文介绍了onHandleIntent()-墙纸更改无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IntentService在后台更改墙纸.使用下面的代码在主Activity类中将墙纸正确更改为图像,但是将代码添加到onHandleItent()后,背景每次均更改为不同的纯色.这是内存问题还是什么?

I am using the IntentService to change wallpaper in the background. Using the code below inside the main Activity class the wallpaper changes to an image correctly, but when the code is added to the onHandleItent() the background changes to a different solid color each time. Is this a memory issue or what?

Activity类中的代码-正常工作:

Code inside Activity class - works correctly:

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()中的代码-无法正常工作:

Code inside onHandleIntent() - DOES NOT work correctly:

    @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()本身存在问题.

It turns out something is wrong with the onHandleIntent() itself.

为了测试它,我做了

  1. 我向onHandleIntent()添加了一个SharedPreferences,以编写一个哑巴字符串"IS Started",并将onStartCommand()包括到IntentService中,该IntentService读取了哑巴SharedPreferences,并在Toast中显示了保存的字符串:

  1. I added a SharedPreferences to the onHandleIntent() to write a dummie string "IS Started" and included the onStartCommand() to the IntentService which reads the dummie SharedPreferences and displays the saved string in a 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添加了一个哑巴写操作:

I also added a dummie write to SharedPrefs in the onIntentHandle like this:

    @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()来读取假人字符串的内容并显示它们.
  1. I then called the service twice: first time becuase dummy SharedPref will be empty as it gets called before onHandleIntent() and a chance for onHandleIntent() to write the dummie string Second time (now that onHandleIntent() has written something) for onStartCommand() to read the contents of dummie string and display them.

结果:Toast显示为"IS STarted",表示正在调用onHandleIntent().

RESULT: Toast displayed "IS STarted" meaning onHandleIntent() is being called.

但是然后要确定墙纸问题,我将Toast直接放在onHandleIntent()内以查看其是否像这样工作:

BUT Then to identify the wallpaper issue I put a Toast directly inside onHandleIntent() to see if it works like this:

    @Override
    protected void onHandleIntent(Intent intent) {
        Toast.makeText(this, "onHandleWorks", Toast.LENGTH_SHORT).show();
    }

结果:没有显示!

那么为什么要调用onHandleIntent()并且仅识别某些命令?

So Why is onHandleIntent() being called and only recognising SOME commands?

编辑3

onHandleIntent()中的代码-当前为:

Code inside onHandleIntent() - Currently as it is:

    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时如何获得活动之外的显示指标?

How do I get the display metrics outside activity when activity is not extended to IntentService?

推荐答案

您正在跳过代码中的一行

Your are skipping one line in your code which is

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

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

this is the main reason that your sample size was getting wrong. now you can use your method to calculate the sample size instead of using sampleSize = 4;

希望现在就解决您的问题:)

Hope so your problem is solved now :)

这篇关于onHandleIntent()-墙纸更改无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆