Note that there are some explanatory texts on larger screens.

plurals
  1. POonHandleIntent() - Wallpaper change not working correctly
    primarykey
    data
    text
    <p>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?</p> <p>Code inside Activity class - works correctly:</p> <pre><code>public void ChangeWallpaper(String imagepath) { DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int height = displayMetrics.heightPixels; int width = displayMetrics.widthPixels &lt;&lt; 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) { } } </code></pre> <p>Code inside onHandleIntent() - DOES NOT work correctly:</p> <pre><code> @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 &lt;&lt; 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) { } } </code></pre> <p>any ideas?</p> <p><strong>EDIT 2 - THE PROBLEM (Unsolved)</strong></p> <p>It turns out something is wrong with the onHandleIntent() itself. </p> <p>To test it, I did</p> <ol> <li><p>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:</p> <pre><code>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); } </code></pre></li> </ol> <p>I also added a dummie write to SharedPrefs in the onIntentHandle like this:</p> <pre><code> @Override protected void onHandleIntent(Intent intent) { SharedPreferences settings = getSharedPreferences("IS", 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("IS","IS Started"); editor.commit(); } </code></pre> <ol> <li>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.</li> </ol> <p>RESULT: Toast displayed "IS STarted" meaning onHandleIntent() is being called. </p> <p>BUT Then to identify the wallpaper issue I put a Toast directly inside onHandleIntent() to see if it works like this:</p> <pre><code> @Override protected void onHandleIntent(Intent intent) { Toast.makeText(this, "onHandleWorks", Toast.LENGTH_SHORT).show(); } </code></pre> <p>RESULT: It didn't show!</p> <p>So Why is onHandleIntent() being called and only recognising SOME commands?</p> <p><strong>EDIT 3</strong></p> <p>Code inside onHandleIntent() - Currently as it is:</p> <pre><code> 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 &lt;&lt; 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) { } } } </code></pre> <p>How do I get the display metrics outside activity when activity is not extended to IntentService?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload