Tuesday, August 02, 2011

onDestoy-onCreate kikapcsolasa forgatasnal

Innen

Configuration Changes

If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.

Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(),onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

This is done because any application resource, including layout files, can change based on any configuration value. Thus the only safe way to handle a configuration change is to re-retrieve all resources, including layouts, drawables, and strings. Because activities must already know how to save their state and re-create themselves from that state, this is a convenient way to have an activity restart itself with a new configuration.

In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.


Es ez azert volt erdekes, mert a recorder activity OnCreate-jeben hoztam letre foldereket amiket az onDestroy-ban toroltem, ha nem kellettek, de amig ezt nem kapcsoltam be, addig minden device forgatasnal jott az onDestroy es onCreate.
Ezeknel az eventeknel lehet meg kikapcsolni az automatikus activity ujrainditast:

public static final int configChanges

Specify one or more configuration changes that the activity will handle itself. If not specified, the activity will be restarted if any of these configuration changes happen in the system. Otherwise, the activity will remain running and itsActivity.onConfigurationChanged method called with the new configuration.

Note that all of these configuration changes can impact the resource values seen by the application, so you will generally need to re-retrieve all resources (including view layouts, drawables, etc) to correctly handle any configuration change.

These values must be kept in sync with those in ActivityInfo and include/utils/ResourceTypes.h.

Must be one or more (separated by '|') of the following constant values.

ConstantValueDescription
mcc0x0001The IMSI MCC has changed, that is a SIM has been detected and updated the Mobile Country Code.
mnc0x0002The IMSI MNC has changed, that is a SIM has been detected and updated the Mobile Network Code.
locale0x0004The locale has changed, that is the user has selected a new language that text should be displayed in.
touchscreen0x0008The touchscreen has changed. Should never normally happen.
keyboard0x0010The keyboard type has changed, for example the user has plugged in an external keyboard.
keyboardHidden0x0020The keyboard or navigation accessibility has changed, for example the user has slid the keyboard out to expose it. Note that despite its name, this applied to any accessibility: keyboard or navigation.
navigation0x0040The navigation type has changed. Should never normally happen.
orientation0x0080The screen orientation has changed, that is the user has rotated the device.
screenLayout0x0100The screen layout has changed. This might be caused by a different display being activated.
uiMode0x0200The global user interface mode has changed. For example, going in or out of car mode, night mode changing, etc.
screenSize0x0400The current available screen size has changed. If applications don't target at least HONEYCOMB_MR2 then the activity will always handle this itself (the change will not result in a restart). This represents a change in the currently available size, so will change when the user switches between landscape and portrait.
smallestScreenSize0x0800The physical screen size has changed. If applications don't target at least HONEYCOMB_MR2 then the activity will always handle this itself (the change will not result in a restart). This represents a change in size regardless of orientation, so will only change when the actual physical screen size has changed such as switching to an external display.
fontScale0x40000000The font scaling factor has changed, that is the user has selected a new global font size.

Constant Value: 16842783 (0x0101001f)

logcat data torlese

adb logcat -c

Monday, August 01, 2011

Android Logging

Hugo:

Logging in any type of software is a useful tool to debug and improve your software. Typically you'll have different types of stuff you want to log: errors, information, traceing etc. So most logging systems have a concept of levels that allow you to specify how important a message is, so that at a later time you can enable a certain type of logging to inspect.

In Android it is handled by the Log class. It too allows for runtime configuration of loggers and it involves setting Android system properties using the setprop command, which is nice if you are building an app as part of the OS but maybe overkill or even clunky if you just want to create your next great app. And what if you don't want your shipping app to have any logging whatsoever? You certainly want it during development, trust me :) Well enter this little utility class:

  1. package com.neenbedankt.rainydays.util;
  2. public class Logging {
  3. public static final boolean LOGV = true;
  4. public static final boolean LOGD = false || LOGV;
  5. public static final boolean LOGI = false || LOGD;
  6. public static final boolean LOGW = false || LOGI;
  7. public static final boolean LOGE = false || LOGW;
  8. }

What this does is setting some flags that indicate what level of logging you want. If you want all logging, just set LOGV to true. If you only want the "info" level and up just enable info. Easy! Now you still have to reference this from your code like this:

  1. // you can also use a static import to drop the Logging part...
  2. if (Logging.LOGD) {
  3. Log.d(TAG, 'This is my debug statement');
  4. }

Tip: you can setup an Eclipse template to generate these kinds of code blocks very fast.

Now the beauty of this all is that since the LOGD variable is a static final (a constant) the compiler will strip out the entire if statement if the LOGD variable is false, hence no logging in your final app and no bytecode generated for it either.

So by introducing a simple class and wrapping the log statements in an if block you'll still be able to enable particular levels of logging, while in your shipping product the logging is stripped out.

Thursday, July 14, 2011

Utolso bootolas idejen Windowson

Innen.

From a command prompt window run the following (the 'S' in 'Statistics' must be capitalized):
net statistics workstation | find "Statistics"
You can shortcut it as well using:
net stats work | find "Stat"
Or ever shorter use either of the following:
net stats work
netstats work |more
This next method uses the command 'systeminfo.' Again from a command prompt run (make sure to capitalize 'S', 'B' and 'T':
systeminfo | find "System Boot Time"
You can use the following on XP, Windows 2003 and earlier (however this will give only the length of uptime and not the system boot time):
systeminfo | find "Up Time"
The third method uses WMI, more specifically wmic (Windows Management Instrumentation Command-line), but the output is a little cryptic:
wmic OS Get LastBootUpTime
You can always use the system event log (this only works on 2003/XP or older). Of course, you could go to Control Panel and browse through the system event log, but let's do it through the command line with:
cscript c:\windows\system32\eventquery.vbs /fi "ID eq 6005" /l system
Finally, you can use this handy PowerShell script:

Get-WmiObject Win32_NTLogEvent -filter "LogFile='System' and EventCode=6005" | Format-Table ComputerName, EventCode, Message, TimeWritten

Saturday, April 09, 2011

Executing a Background Task with Android

Itt van.

alertDialog Android-ban

Innen:

Simple message box with no buttons
AlertDialog alertDialog; 
alertDialog = new AlertDialog.Builder(this).create(); 
alertDialog.setTitle("Packing List"); 
alertDialog.setMessage("Could not find the file."); 
alertDialog.show(); 

Message box with buttons
AlertDialog deleteAlert = new AlertDialog.Builder(this).create(); 
deleteAlert.setTitle("Delete List"); 
deleteAlert.setMessage("Are you sure you want to delete this list?"); 
deleteAlert.setButton("Yes", new OnClickListener(){  
@Override 
public void onClick(DialogInterface dialog, int which) {
//... 
} 
}); 
deleteAlert.setButton2("No", new OnClickListener(){  
@Override 
public void onClick(DialogInterface dialog, int which) { 
//... 
} 
}); 
deleteAlert.show();

Friday, April 08, 2011

Brad Fitzpatrick: Writing zippy Android apps


• What is an ANR? Why do you see them?
• Quantifying responsiveness: “jank”
• Android SDK features to use to avoid jankiness & ANRs
• Numbers to know
• War stories from optimizing Froyo (2.2)
• New performance instrumentation available in Froyo

"Come hear tips & war stories on making fast, responsive (a.k.a. "non-janky") Android apps. No more ANRs! Eliminate event loop stalls! Fast start-ups! Optimized database queries with minimal I/O! Also, learn about the tools and techniques we use to find performance problems across the system and hear what's coming in the future."

A service-eket deklaralni kell a manifest-ben

Ezt kihagytam es ezert nem hivodott meg az IntentService-em, bar latvanyos hiba nem tortent csak a log-ban lattam, hogy "Unable to start service Intent XXX: not found"

Monday, April 04, 2011

Ha az Activity-t csak Landscape akarjuk latni

Akkor az onCreate(Bundle savedInstances)-be kell beleirni ezt:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mondjuk a a setContentView(..) ele.

Persze mas screenOrientation-t is valaszthatunk.

Friday, April 01, 2011

Create folder/file on Android

Legalabbis a Samsung Galaxy S-en ezt tapasztaltam:

Az Eclipse File Explorer-eben is lathato az /mnt/sdcard/ folder. Ezen belul van egy external_sd ami mindig jelen van, de csak akkor hasznalhato, ha tenyleg beteszunk egy kulcso SD Card-ot a telefonba.
Ami videokat/kepeket a default Camera app-al rogzitunk az a
/mnt/sdcard/DCIM/Camera/video-yyyy-mm-dd-hh-mm-ss.mp4
illetve
/mnt/sdcard/DCIM/Camera/yyy-mm-dd hh.mm.ss.jpg
file-ba kerulnek.

Ha kodbol letrehuzzuk az "/folder/foo" folder-t, akkor az itt lesz:
/mnt/sdcard/foo
es nem torlodik, ha letoroljuk az app-ot ami csinlata.
Ezzel a koddal keszult:
File newDirectory = new File("/sdcard/foo/");
newDirectory.mkdirs();

getFilesDir() azt a folder-t adja vissza ahova az app irhat. Ez a folder az app torlesenel automatikusan torlodik es csak az adott app fer hozza.
Valahogy igy nez ki: /data/data/com.example.foo/files/

getExternalFilesDir(String type) is egy olyan folder-t ad vissza, ahova az app irhat csak ez az external SD kartyan. A dokumentacio szerint "These files are private to the applications, and not typically visible to the user as media" de nem tudom a "typically" itt mit jelent. A getFiles()-al szemben viszont "There is no security enforced with these files. All applications can read and write files placed here."
Valahogy igy nez ki: /mnt/sdcard/external_sd/Android/data/com.example.foo/files/
Tehat az /mnt/sdcard/external_sd/ ezert el akkor is, ha nincs a telefonban kulso SD kartya es nyugodtan lehet is hasznalni ezzel a method-dal. Peldaul letrehozni benne sajat bar konyvtarat a foo app konyvtara ala:
File efd = getExternalFilesDir(null);
File barFolder = new File(efd.getAbsolutePath() + "/bar/");
barFolder.mkdirs();
Ez igy rendben mukodik, de amikor uninstallaltam a foo app-ot, akkor sem torlodott az /mnt/sdcard/external_sd/Android/data/com.example.foo/files folder. Pedig a dokumentacio szerint: "This is like getFilesDir() in that these files will be deleted when the application is uninstalled"
A type parameter pedig erre jo: "The type of files directory to return. May be null for the root of the files directory or one of the following Environment constants for a subdirectory: DIRECTORY_MUSIC,DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, or DIRECTORY_MOVIES"

getExternalStorageDirectory(): "don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer."

"Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled. Other shared files should be placed in one of the directories returned by getExternalStoragePublicDirectory(String)."

Tuesday, March 29, 2011

Michael Galpin: Working with XML on Android

Itt van.

+

startActivityForResult-nal a requestCode nem lehet negativ

Pontosabban lehet, de: "Using negative requestCode is the same as calling startActivity(Intent) (the activity is not launched as a sub-activity)." Azaz nem fog meghivodni az onActivityResult(..)

Thursday, March 24, 2011

Android: masik Activity megnyitasa

Intent myIntent = new Intent();
myIntent.setClassName(packageName, className);
startActivity(myIntent);

Ha pedig parametereket akarunk atadni, akkor meg a startActivity elott:
myIntent.putExtra("name", "John Doe");

amit a masik oldalon (mondjuk az onCreate-ben) igy kapunk meg:
Bundle extras = getIntent().getExtras();
if (null != extras) {
String name = extras.getString("name");
}

Sokmindent at lehet adni extrakban, de object-et nem. Ezt ki kell parsolni mondjuk egy stringtombbe vagy egy json-ba (GSON).

Itt viszont azt irjak, hogy legjobb, ha Parcelable az object-unk, mert azt at lehet adni extra-kent es sokkal gyorsabb mint a parsolgatas.

Ha pedig vissza is akarunk adni valamit akkor:
- startActivityForResult(Intent, int) a sima startActivity(Intent) helyett
- a masik Activity-ben setResult(int)

ListView onListitemClick() nem hivodik meg, ha van focusable View a ListView-ban

Egyik megoldas, hogy az Adapter getView-jaban beallitjuk ennek az View-n, hogy ne legyen Focusable:

myButton.setFocusable(false);

Itt azt is irjak masik megoldasnak, hogy az xml-ben beallitjuk a android:focusable="false" es android:focusableInTouchMode="false" property-ket, de nekem ez nem mukodott.