Saturday, April 09, 2011
alertDialog Android-ban
Innen:
Message box with buttons
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
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)."
Wednesday, March 30, 2011
Tuesday, March 29, 2011
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(..)
Monday, March 28, 2011
Thursday, March 24, 2011
Android: masik Activity megnyitasa
Intent myIntent = new Intent();
myIntent.setClassName(packageName, className);
startActivity(myIntent);
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.
Create new Activity in Eclipse
Vagy new Class.. vagy pedig:
- Double click on AndroidManifest.xml in the package explorer.
- Click on the "Application" tab of the manifest editor
- Click on "Add.." under the "Application Nodes" heading (bottom left of the screen)
- Choose Activity from the list in the dialog that pops up (if you have the option, you want to create a new top-level element)
- Click on the "Name*" link under the "Attributes for" header (bottom right of the window) to create a class for the new activity.
Wednesday, March 23, 2011
Tuesday, March 22, 2011
JSON parsing on Android
innen
...
- org.json – Not very convenient to use. Doesn’t support stream parsing out of the box.
- jackson – very convenient to use, excellent mapping features, custom stream deserializers… but don’t use it on Android. Like Facebook and Twitter did… This will put you into the grave:) Everything is excellent before you will take a look into the ddms log.
- gson – GSON stream is very nice and small. It doesn’t produce so much garbage as Jackson. But it is not so convenient to use. Good news that GSON stream parser is included in API 11.
Android ListView examples
Krysztof Grajek: Custom ListView items and adapters
AppFulcrum:
2 dologra erdemes odafigyelni a sajat ArrayAdapterunkben. Egyreszt amikor a ListView egy sora lekerul a kijelzorol ezt a view-t megkapja az adapater es a getView(..) method convertView-jaban ujra megapjuk es fel tudjuk hasznalni. Masreszt a getView-ban hivott findViewByID() eleg koltseges ezert erdemes egy ViewHolder class-ban tarolni a findeViewByID() alltal talalt view-kat es ezt a convertView tag-jeben tarolni. Ezzel Lars Vogel szerint kb 175%-os performacianovekedeset erhetunk el. Valahogy igy:
// ViewHolder will buffer the assess to the individual fields of the row layout
static class ViewHolder {
public ImageView imageView;
public TextView textView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (null == convertView) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.rowlayout, null, true);
holder = new ViewHolder();
holder.textView = (TextView) rowView.findViewById(R.id.label);
holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.textView.setText("whatever text");
holder.imageView.setImageResource(R.drawable.testicon);
return convertView;
}
Monday, March 21, 2011
dp to pixel (Android)
private int dpToPixel(float dp) {
int result;
final float scale = getResources().getDisplayMetrics().density;
result = (int) (dp * scale + 0.5f);
return result;
}
Thursday, March 17, 2011
Android Location
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (null != loc) {
Double longitude = loc.getLongitude();
Double latitude = loc.getLatitude();
}
A manifiest-be pedig:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
Ez az utolso csak akkor kell, ha LocationManager.GPS_PROVIDER helyett valami mock location provider-t akarunk hasznalni. Ezt meg valahogy igy tehejuk meg:
String MOCK_LOC_PROVIDER_NAME = "mockLocationProvider";
...
lm.addTestProvider(MOCK_LOC_PROVIDER_NAME, false, false, false, false, false, false, false, 0, 5);
Location location = new Location(MOCK_LOC_PROVIDER_NAME);
location.setLatitude(32.23434);
location.setLongitude(-16.32525);
location.setTime(new Date().getTime());
lm.setTestProviderLocation(MOCK_LOC_PROVIDER_NAME, location);
lm.setTestProviderEnabled(MOCK_LOC_PROVIDER_NAME, true);
Es igy nezhetjuk meg, hogy mar hozzaadtuk-e a sajat location provider-unket (2-szer ugyanazt nem lehet hozzaadni mert akkor hiba lesz):
List providers = lm.getAllProviders();
if (providers.contains(MOCK_LOC_PROVIDER_NAME)) {...}
Wednesday, March 16, 2011
Friday, March 11, 2011
Readonly sdcard AVD-hez
Letrehoztam Eclipse-ben egy AVD-t (Android Virtual Device), de az sdcard-ja readonly volt. Probalkoztam mindenfelevel, de vegul az segitett, ha csinaltam egy uj AVD-t. Nem tudom mit rontottam el elsore, de ez a masodik mar nem volt read only. Itt viszont eloszor tul kicsi-t csinaltam (128 MB) amire nem tudtam felmasolni egy 7 MB-os mp4-et. Amikor csinaltam egy masik AVD-t 1024 MB-os sdcard-dal, akkor arra mar felment ez a file. (Viszont maga az AVD sokkal lassabban keszult el, mert sokaig tartott letrehozni az sdcard.img-t. Raadasul az AVD indulasanal is lassabban tolti be az sdcard-ot, ezert egy ideig itt is readonly-nak tunik.)
Tuesday, March 08, 2011
Monday, March 07, 2011
Friday, March 04, 2011
Subscribe to:
Posts (Atom)