Categories
discuss

Lock screen doesn’t cause onStop() to get called

My app uses GPS, so I remove GPS updates in onStop() so it doesn’t use up battery when it shouldn’t. However, I noticed that the battery on my test phone was running down if my app was open when the lock screen comes on.

So to test it, I added a Log.i(…) to the onStop() method of my activity. Sure enough, onStop() does not get called when I press the off button on my HTC Incredible (which turns off the screen).

Is there any way to listen for the lock screen, or the screen turning off, since it doesn’t call onStop() like it should?

Thanks.

Answer

onStop is not guaranteed to be called, onPause is. Use onPause to remove GPS updates from eating battery.

Categories
discuss

jQuery – equivalent to each(), but for a single element

What’s the jQuery equivalent for each():

$(".element").each(function(){  
  // do stuff
});

when attaching a function to a single element, like #element ?

Answer

You can always reference the jQuery object in a variable:

var $el = $('#element');

…then manipulate it.

$el.doSomething();          // call some jQuery methods from the cached object
$el.doSomethingElse();

If the reason you wanted .each() was to reference the DOM element as this, you don’t really need the this keyword to do it, you can simply grab the DOM element out of the jQuery object.

var element = $('#element')[0];       // both of these give you the DOM element
var element = $('#element').get(0);   //        at index 0

The two of these are equivalent, and will retrieve the DOM element that would be referenced as this in the .each().

alert( element.tagName );  // alert the tagName property of the DOM element
alert( element.id );       // alert the ID property of the DOM element

I’d note that it isn’t necessarily bad to use each to iterate over a single element.

The benefits are that you have easy access to the DOM element, and you can do so in a new scope so you don’t clutter the surrounding namespace with variables.

There are other ways to accomplish this as well. Take this example:

(function( $ ) {

    // Inside here, "this" will refer to the DOM element,
    //    and the "$" parameter, will reference the jQuery library.

    alert( this.tagName );

    // Any variables you create inside will not pollute the surrounding 
    //     namespace.

    var someVariable = 'somevalue'; // is local to this function

}).call( $('#element')[0], jQuery );
Categories
discuss

NoClassDefFoundError in mapActivity

i have this error in my map app.do you know what is wrong?i have checked and the package is right in my .java files and also i have put the uses-libraries of google maps into my application tag in the manifest.xml..please help,im trying hours to solve it..

Answer

Make sure you have put in

<uses-library android:name="com.google.android.maps" />

inside your <application></application> tags.

Categories
discuss

@font-face not working in mobile Webkit

I’m having trouble getting @font-face to behave in any mobile Webkit browser I’ve tested–Safari on an iPhone 3GS, the default Android 2.2 browser, and Dolphin browser on Android.

It works in all desktop browsers, from IE7 to IE9, FF3.5, Safari 4, and Opera.

The fonts and CSS are from FontSquirrel:

@font-face {
    font-family: 'LeagueGothicRegular';
    src: url('../fonts/League_Gothic-webfont.eot');
    src: local('☺'),
        url('../fonts/League_Gothic-webfont.woff') format('woff'),
        url('../fonts/League_Gothic-webfont.ttf') format('truetype'),
        url('../fonts/League_Gothic-webfont.svg#webfontFHzvtkso') format('svg');
    font-weight: normal;
    font-style: normal;
}

@font-face {
    font-family: 'LatinModernRoman10Bold';
    src: url('../fonts/lmroman10-bold-webfont.eot');
    src: local('☺'),
        url('../fonts/lmroman10-bold-webfont.woff') format('woff'),
        url('../fonts/lmroman10-bold-webfont.ttf') format('truetype'),
        url('../fonts/lmroman10-bold-webfont.svg#webfonthCDr6KZk') format('svg');
    font-weight: normal;
    font-style: normal;
}

@font-face {
    font-family: 'LatinModernRoman10BoldItalic';
    src: url('../fonts/lmroman10-bolditalic-webfont.eot');
    src: local('☺'),
        url('../fonts/lmroman10-bolditalic-webfont.woff') format('woff'),
        url('../fonts/lmroman10-bolditalic-webfont.ttf') format('truetype'),
        url('../fonts/lmroman10-bolditalic-webfont.svg#webfontegrLi3sm') format('svg');
    font-weight: normal;
    font-style: normal;
}

I’ve checked the SVG ID in the SVG font source, and they all match up.

Could it be because I’ve got some letter-spacing rules later on in the CSS?

Thanks!

Answer

As it turns out, the syntax was wrong. I stumbled across this solution via twitter:

http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax

It worked perfectly. Just checked in all major browsers, and my fonts show up, including on Android and iOS.

Now, my CSS reads like so:

@font-face {
    font-family: 'LeagueGothicRegular';
    src: url('../fonts/League_Gothic-webfont.eot#') format('eot'),
        url('../fonts/League_Gothic-webfont.woff') format('woff'),
        url('../fonts/League_Gothic-webfont.ttf') format('truetype'),
        url('../fonts/League_Gothic-webfont.svg#webfontFHzvtkso') format('svg');
    font-weight: normal;
    font-style: normal;
}

@font-face {
    font-family: 'LatinModernRoman10Bold';
    src: url('../fonts/lmroman10-bold-webfont.eot#') format('eot'),
        url('../fonts/lmroman10-bold-webfont.woff') format('woff'),
        url('../fonts/lmroman10-bold-webfont.ttf') format('truetype'),
        url('../fonts/lmroman10-bold-webfont.svg#webfonthCDr6KZk') format('svg');
    font-weight: normal;
    font-style: normal;
}

@font-face {
    font-family: 'LatinModernRoman10BoldItalic';
    src: url('../fonts/lmroman10-bolditalic-webfont.eot#') format('eot'),
        url('../fonts/lmroman10-bolditalic-webfont.woff') format('woff'),
        url('../fonts/lmroman10-bolditalic-webfont.ttf') format('truetype'),
        url('../fonts/lmroman10-bolditalic-webfont.svg#webfontegrLi3sm') format('svg');
    font-weight: normal;
    font-style: normal;
}

Glad to know there’s a better bulletproof solution out there than the dang smileyface hack.

Hope this helps somebody!

Categories
discuss

How to convert anonymous function to a regular one?

Total JS noob here. I have the following line that implements the jQuery Slider:

<script type="text/javascript">
    $(document).ready(function () {
        $("#wheelLeft").slider({ 
                 orientation: 'vertical', value: 37, 
                 min: -100, max: 100, 
                 slide: function (event, ui) { $("#lblInfo").text("left"); } });
    });
</script>

Basically on the slide event, the #lblInfo gets its text set to left. This works fine. However, I’d like to convert the inline anonymous function that handles the slide event into a regular function.

Can someone help out?

Answer

<script type="text/javascript">

function handleSlide(event, ui) { 
    $("#lblInfo").text("left"); 
}

$(document).ready(function () {
    $("#wheelLeft").slider({ 
             orientation: 'vertical', value: 37, 
             min: -100, max: 100, 
             slide: handleSlide
    });
});
</script>
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..