Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Ing. Stella Gabriele Practice 11-25-2014 -Overview Android Manifest.xml Application Components Resources Activity LifeCycle Application: Android Features Android Manifest -AndroidManifest.xml Main File of every Android Application Inform the system about the application's components Must be at the root of the application project directory Defines the “entry point” of the Application Identify any user permissions the application requires, such as Internet access or read-access to the user's contacts Declare the minimum API Level required by the application, based on which APIs the application uses Declare hardware and software features used or required by the application, such as a camera, bluetooth services, or a multitouch screen API libraries the application needs to be linked against (other than the Android framework APIs) -AndroidManifest.xml 2 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.flash" android:versionCode="1" android:versionName="1.3" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> …… -AndroidManifest.xml 3 ……… inform the system about the application's components <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name="com.android.flash.FlashLightActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> entry point </manifest> -AndroidManifest.xml 4 You MUST declare every Application’s component: <activity> for activities <service> for services <receiver> for broadcast receivers <provider> for content providers Activities, services, and content providers that you include in your source but do not declare in the manifest are not visible to the system and, consequently, can never run. If you try to call a component not declared in the Manifest the application crasches Application Components -Android Application Components Application components are the essential building blocks of an Android application Activities Services Content Provider Broadcast Receiver Each one is a unique building block that helps define your application's overall behavior -Appl Components: Activities An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails An activity is implemented as a subclass of Activity -Appl Components: Services A service is a component that runs in the background to perform long-running operations or to perform work for remote processes A service does not provide a user interface For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity A service is implemented as a subclass of Service -Appl Components: Content Provider A content provider manages a shared set of application data You can store the data in the file system, an SQLite database, on the web, or any other persistent storage location your application can access Through the content provider, other applications can query or even modify the data For example, the Android system provides a content provider that manages the user's contact information A content provider is implemented as a subclass of ContentProvider and must implement a standard set of APIs that enable other applications to perform transactions -Appl Components: Receivers A broadcast receiver is a component that responds to system-wide broadcast announcements For example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured Although broadcast receivers don't display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object. -Android Application Components A unique aspect of the Android system design is that any application can start another application’s component For example, if you want the user to capture a photo with the device camera, there's probably another application that does that and your application can use it, instead of developing an activity to capture a photo yourself You can simply start the activity in the camera application that captures a photo. When complete, the photo is returned to your application so you can use it. To the user, it seems as if the camera is actually a part of your application -Android Application Components Because the system runs each application in a separate process with file permissions that restrict access to other applications, your application cannot directly activate a component from another application The Android system, however, can. To activate a component in another application, you must deliver a message to the system that specifies your intent to start a particular component The system then activates the component for you Resources -Resources An Android application requires resources that are separate from the source code Drawables Audio Files Animations Menus Styles Colors Strings Layouts Defined in XML and referenced in the source code You can optimize the app for different devices and configurations For every resource that you include in your Android project, the SDK build tools define a unique integer ID (R class) Es. R.drawable.logo -Resources – devices -Resources Qualifiers: Let the system use a different file, layout or string optimized for particular device features: Screen resolution Screen dimension User Language API level Enhance portability and application manteinance The resources can be switched real-time if something changes qualifiers -Resources – ex. screen resolution -Tricks for designers Elements’ dimensions http://developer.android.com/design/style/iconography.html Qualifier DPI Scaling factor Launcher icon Action bar, tab icon Notification Notification Notification icon (API 11) icon (API 9) icon (older) ldpi 120 0.75 36 x 36 32 x 32 24 x 24 18 x 18 18 x 18 16 x 16 12 x 19 12 x 12 19 x 19 16 x 16 mdpi 160 1.0 48 x 48 42 x 42 32 x 32 24 x 24 24 x 24 22 x 22 16 x 25 16 x 16 25 x 25 21 x 21 hdpi 240 1.5 72 x 72 64 x 64 48 x 48 36 x 36 36 x 36 33 x 33 24 x 38 24 x 24 38 x 38 32 x 32 xhdpi 320 2.0 96 x 96 84 x 84 64 x 64 48 x 48 48 x 48 44 x 44 32 x 50 32 x 32 50 x 50 42 x 42 xxhdpi 480 3.0 144 x 144 126 x 126 96 x 96 72 x 72 72 x 72 66 x 66 48 x 75 48 x 48 75 x 75 63 x 63 Activity Lifecycle -Activity An application usually consists of multiple activities that are loosely bound to each other Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application for the first time In your Activity, you need to implement callback methods that the system calls when the activity transitions between various states of its lifecycle, such as when the activity is being created, stopped, resumed, or destroyed -Activity: Ciclo di vita onCreate() You must implement this method The system calls this when creating your activity Here you should initialize the essential components of your activity this is where you must call setContentView() to define the layout for the activity's user interface -Activity: Ciclo di vita onStart() Called just before the activity becomes visible to the user Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden -Activity: Ciclo di vita onResume() Called just before the activity starts interacting with the user At this point the activity is at the top of the activity stack, with user input going to it. Always followed by onPause() -Activity: Ciclo di vita onPause() The system calls this method as the first indication that the user is leaving your activity Here you should commit any changes that should be persisted beyond the current user session (because the user might not come back). -Activity: Ciclo di vita onStop() Called when the activity is no longer visible to the user This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it -Activity: Ciclo di vita onDestroy() Called before the activity is destroyed This is the final call that the activity will receive It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method. Application: Android Features -Android Features: overview It is a complete Android Application You must import the Project into the workspace as an existing Project: AndroidFeatures.zip -Android Features: features Permissions (AndroidManifest.xml) Splash Screen: schedule a task Notifications StartUp BroadcastReceiver Activities Fragments Layouts: from XML and dynamically from code Send a Sms Accelerometer Sensor GPS Sensor Android Preferences Application Menu -Android Features: FlashLight We want to create an Activity that Turns on and off the flashlight (led) of the phone The Activity should check if the hardware feature is present on the device and manage the exceptions -Android Features: FlashLight STEPS TO DO 1. Create a New Activity called FlashLightActivity.java 2. Add the new Activity in the Manifest 3. Add the permissions in the Manifest to access Camera 4. Add an Intent from the application (MainFragment) that opens the Activity when the image is clicked (onClickListener) 5. Create the resources and the strings 6. Create a Layout for the Activity 7. Implements the callback methods 8. Test and Debug the Activity -Android Features: FlashLight -1package com.android.features; import android.app.Activity; import android.os.Bundle; public class FlashLightActivity extends Activity { …….. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ………… } } -Android Features: FlashLight -2<activity android:name="com.android.features.FlashLightActivity" android:label="@string/title_section4" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize" > </activity> Add the title to strings.xml -Android Features: FlashLight -3<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> Hardware feature Android permission -Android Features: FlashLight -4@Override public void onClick(View v) { ……….. if (v==flashImage) { //open Activity Intent intent = new Intent(getActivity(), FlashLightActivity.class); startActivity(intent); } } -Android Features: FlashLight -5 Create the resources and the strings used in the layout file Strings in Images in res/values/strings.xml res/drawable -Android Features: FlashLight -6<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/flashLinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/black" > <ImageView android:id="@+id/flashImageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="32dp" android:src="@drawable/flashlight" /> <Button android:id="@+id/buttonFlashlight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/flashImageView1" android:textColor="@color/white" android:layout_centerHorizontal="true" android:layout_marginTop="33dp" android:text="@string/flash_button" /> </RelativeLayout> -Android Features: FlashLight -7FlashLightActivity.java public class FlashLightActivity extends Activity { //flag to detect flash is on or off private boolean isLighOn = false; private Camera camera; private Button button; @Override protected void onStop() { super.onStop(); if (camera != null) { camera.release(); } } …….. -Android Features: FlashLight -7@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.flashlight_view); button = (Button) findViewById(R.id.buttonFlashlight); Context context = this; PackageManager pm = context.getPackageManager(); //the device support camera? if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) { Log.e("err", "Device has no camera flash!"); button.setEnabled(false); button.setText("NO FLASH"); return; } camera = Camera.open(); if (camera == null) { return; } -Android Features: FlashLight -7final Parameters p = camera.getParameters(); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (isLighOn) { Log.i("info", "torch is turn off!"); p.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(p); camera.stopPreview(); isLighOn = false; Toast.makeText(getApplicationContext(), "Flash Turning Off", Toast.LENGTH_LONG).show(); } else { Log.i("info", "torch is turn on!"); p.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(p); camera.startPreview(); isLighOn = true; Toast.makeText(getApplicationContext(), "Flash Turning On", Toast.LENGTH_LONG).show(); } } }); } } -Android Features: FlashLight -8 Build the project Create the .apk file Install the application on a Virtual device (without flash feature) Test the App Install the application on a real device (with flash) Test the App Debug exceptions