0

I am creating an analogClock widget with the second hand. I use the alarmManager for updating my widget every second.

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    if (updateIntent == null) {
        Intent update = new Intent(context, UpdateService.class);
        updateIntent = PendingIntent.getService(context, 0, update, PendingIntent.FLAG_CANCEL_CURRENT);
    }

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 1000, updateIntent);
}

My UpdateService updates the current positions of the clocks. In onCreate method I load bitmaps

hourBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.hour);
minuteBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.minute);
secondBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.second);

in onStartCommand I updatePositions

Bitmap hourClockBitmap = getRotatedClock(hourBitmap, getHourAngle());
remoteViews.setImageViewBitmap(R.id.hour, hourClockBitmap);

Bitmap minuteClockBitmap = getRotatedClock(minuteBitmap, getMinuteAngle());
remoteViews.setImageViewBitmap(R.id.minute, minuteClockBitmap);

Bitmap secondClockBitmap = getRotatedClock(secondBitmap, getSecondAngle());
remoteViews.setImageViewBitmap(R.id.second, secondClockBitmap);

getRotatedClock method:

private Bitmap getRotatedClock(Bitmap bitmap, int angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

When I run my watch, it works well about 20 seconds, after this I have TransactionTooLargeException in the service in onStartCommand method on AppWidgetManager's updateAppWidget method invocation. I experimented with the code and when I replaced getRotatedClock method implementation with this

return bitmap

I had no problems. Therefore, I think the rotation of bitmaps is the reason of the exception. Is it correct? What should I do?

0 Answers0