Developing in android
Upcoming SlideShare
Loading in...5
×
 

Developing in android

on

  • 1,621 views

IEEE Seminar notes

IEEE Seminar notes

Statistics

Views

Total Views
1,621
Slideshare-icon Views on SlideShare
1,615
Embed Views
6

Actions

Likes
1
Downloads
75
Comments
0

1 Embed 6

http://localhost 6

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Developing in android Developing in android Presentation Transcript

    • Developing in Android What to do after you have the SDK installed
    • Outline
        • Creating a new project
        • XML/pragmatically constructing layouts
        •   Initializing the UI (listeners, ect)
        • Permissions
        • Calling other activities
        • Overview of other functionality (bluetooth, maps, Camera, ect, preferances)
        • Additional Resources (google is your best friend)
        • Workshop
    • Creating a project
        • file->new->project->android project
    • Project Creation Fields
      • Project Name This is the Eclipse Project name — the name of the directory that will contain the project files.
      • Application Name This is the human-readable title for your application — the name that will appear on the Android device.
      • Package Name This is the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity will be generated.
      • Create Activity This is the name for the class stub that will be generated by the plugin. This will be a subclass of Android's  Activity  class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn't need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.
      • Min SDK Version This value specifies the minimum API Level required by your application. 
    • New Project
    • Activity Lifecycle
        • onCreate()
        • onRestart()
        • onStart()
        • onResume()
        • onPause()
        • onStop()
        • onDestroy()
      • More in Depth Explanation:
      • http://developer.android.com/guide/topics/fundamentals.html
    • UI Creation
    • Location of XML
    • XML Layout <? xml version = &quot;1.0&quot; encoding = &quot;utf-8&quot; ?> <RelativeLayout xmlns:android = &quot;http://schemas.android.com/apk/res/android&quot;     android:layout_width = &quot;fill_parent&quot;     android:layout_height = &quot;fill_parent&quot; >     <TextView         android:id = &quot;@+id/label&quot;         android:layout_width = &quot;fill_parent&quot;         android:layout_height = &quot;wrap_content&quot;         android:text = &quot;Type here:&quot; />     <EditText         android:id = &quot;@+id/entry&quot;         android:layout_width = &quot;fill_parent&quot;         android:layout_height = &quot;wrap_content&quot;         android:background = &quot;@android:drawable/editbox_background&quot;         android:layout_below = &quot;@id/label&quot; />     <Button         android:id = &quot;@+id/ok&quot;         android:layout_width = &quot;wrap_content&quot;         android:layout_height = &quot;wrap_content&quot;         android:layout_below = &quot;@id/entry&quot;         android:layout_alignParentRight = &quot;true&quot;         android:layout_marginLeft = &quot;10dip&quot;         android:text = &quot;OK&quot; />     <Button         android:layout_width = &quot;wrap_content&quot;         android:layout_height = &quot;wrap_content&quot;         android:layout_toLeftOf = &quot;@id/ok&quot;         android:layout_alignTop = &quot;@id/ok&quot;         android:text = &quot;Cancel&quot; /> </RelativeLayout>
    • XML vs Programmatically
      • <Button         android:id = &quot;@+id/ok&quot;         android:layout_width = &quot;wrap_content&quot;         android:layout_height = &quot;wrap_content&quot;         android:layout_below = &quot;@id/entry&quot;         android:layout_alignParentRight = &quot;true&quot;         android:layout_marginLeft = &quot;10dip&quot;         android:text = &quot;OK&quot; />
      • //intiate button
      • Button ok= new Button(this);
      • //assign ID for later referance
      • ok.setId(R.id.ok);
      • //sets paramiters, wraps content
      • LayoutParams params = new LayoutParams (ViewGroup.Layout   
      •        Params.WRAP_CONTENT,  
      •        ViewGroup.LayoutParams.WRAP_CONTENT);
      • //sets position within the layout
      • params.addRule(RelativeLayout.layout_below, R.id.entry );
      • params.addRule(RelativeLayout.layout_alignParentRight);
      • //sets the left margin
      • params.setMargins(10,0,0,0);
      • //sets the params to the button
      • ok.setLayoutParams(params);
      • //sets the text
      • ok.setText(&quot;OK&quot;);
      • //adds the button as a child
      • layout.addView(ok);
    • Initializing the UI
      •   public void onCreate(Bundle savedInstanceState) {
      •          super.onCreate(savedInstanceState);
      •          setContentView(R.layout.view_name);
      •          //add listeners
      •      }
    • Button Listener
      • Button btnName = (Button) findViewById(R.id.btnId);
      • btnNamet.setOnClickListener(new View.OnClickListener() {
      •      public void onClick(View v) {
      •    //sets what the button dose when clicked       
      •      } 
      • });
    • onTouchListener
      • FrameLayout main = (FrameLayout) findViewById(R.id.main_view);      
      • //set touch listener
      • main.setOnTouchListener(new View.OnTouchListener() {
      •      public boolean onTouch(View v, MotionEvent e) {
      •          //x and y  coordinates
      •         float x = e.getX();
      •          float y = e.getY();
      •    
      •          
      •          //MotionEvent.ACTION_DOWN=finger presses down
      •          //MotionEvent.ACTION_UP=finger lifts up
      •          //MotionEvent.ACTION_MOVE=change in postion between UP and DOWN
      •          if(e.getAction()==MotionEvent.ACTION_DOWN){
      •              //do something
      •          }
      •      return true;
      •       }
      • });
    • Manifest.XML
      • <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
      • <manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      •        package=&quot;com.RIT.exersize2&quot;
      •        android:versionCode=&quot;1&quot;
      •        android:versionName=&quot;1.0&quot;>
      •        <uses-permission android:name=&quot;android.permission.CAMERA&quot; />
      •      <application android:icon=&quot;@drawable/icon&quot; 
      •              android:label=&quot;@string/app_name&quot;>
      •          <activity android:name=&quot;.exersize2&quot;
      •                    android:label=&quot;@string/app_name&quot;>
      •              <intent-filter>
      •                  <action android:name=&quot;android.intent.action.MAIN&quot; />
      •                  <category android:name=&quot;android.intent.category.LAUNCHER&quot; />
      •              </intent-filter>
      •          </activity>
      •      </application>
      • </manifest> 
    • Some Common Permissions ACCESS_FINE_LOCATION Allows an application to access fine (e.g., GPS) location ACCESS_NETWORK_STATE Allows applications to access information about networks BATTERY_STATS Allows an application to collect battery statistics BLUETOOTH Allows applications to connect to paired bluetooth devices CALL_PHONE Allows an application to initiate a phone call without going through the Dialer user interface for the user to confirm the call being placed. CAMERA Required to be able to access the camera device. EXPAND_STATUS_BAR Allows an application to expand or collapse the status bar. FLASHLIGHT Allows access to the flashlight INTERNET Allows applications to open network sockets. MODIFY_AUDIO_SETTINGS Allows an application to modify global audio settings REBOOT Required to be able to reboot the device. RECEIVE_SMS Allows an application to monitor incoming SMS messages, to record or perform processing on them. SEND_SMS Allows an application to send SMS messages. SET_ALARM Allows an application to broadcast an Intent to set an alarm for the user. SET_ORIENTATION Allows low-level access to setting the orientation (actually rotation) of the screen. SET_TIME Allows applications to set the system time VIBRATE Allows access to the vibrator WRITE_SMS Allows an application to write SMS messages.
    • Calling Other Apps
      • Intent intent = new Intent(&quot;classpath&quot;);
      • startActivity(intent);
      •          or:
      • startActivityForResult(scan, 0);
      •   public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      •          if (requestCode == 0) {
      •              if (resultCode == RESULT_OK) {
      •                  //name value pair retrieval
      •                  String contents = intent.getStringExtra(&quot;SCAN_RESULT&quot;);
      •                 
      •              } else if (resultCode == RESULT_CANCELED) {
      •                  //fail
      •              }
      •          }
      •      }
    • Bluetooth
        • Ensure that the devise supports Bluetooth
        • Turn Bluetooth on if not already
        •   Find Device
            •   Query Pared Devices
            •   Discover Device
        •   Connecting to a device is almost identical to connecting via TCP
        • Communication between devices is almost identical to TCP as well
      • Bluetooth Tutorial:
      • http://developer.android.com/guide/topics/wireless/bluetooth.html
    • Location
      • // Acquire a reference to the system Location Manager 
      • LocationManager locationManager = (LocationManager)   
      •      this.getSystemService(Context.LOCATION_SERVICE);
      •  
      •   // Define a listener that responds to location updates 
      • LocationListener locationListener = new LocationListener() { 
      •       public void onLocationChanged(Location location) { 
      •           // Called when a new location is found by the network location provider.         
      •          makeUseOfNewLocation(location); 
      •       }
      •       public void onStatusChanged(String provider, int status, Bundle extras) {} 
      •       public void onProviderEnabled(String provider) {} 
      •       public void onProviderDisabled(String provider) {} 
      •   };
      •   //Register the listener with the Location Manager to receive location updates locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, 
      •              locationListener); 
      • //for GPS use LocationManager.GPS_PROVIDER
      • Tutorial: 
      • http://developer.android.com/guide/topics/location/obtaining-user-location.html
    • Using the Camera: Activity
      • package android.cameratest;
      • import java.io.IOException;
      • import android.app.Activity;
      • import android.graphics.PixelFormat;
      • import android.hardware.Camera;
      • import android.os.Bundle;
      • import android.view.SurfaceHolder;
      • import android.view.SurfaceView;
      • import android.view.View;
      • import android.view.Window;
      • import android.view.WindowManager;
      • public class CameraTest extends Activity implements SurfaceHolder.Callback, View.OnClickListener{
      • Bundle bundle;
      • SurfaceView mSurfaceView;
      • SurfaceHolder mSurfaceHolder;
      • Camera mCamera;
      • boolean mPreviewRunning;
      •      /** Called when the activity is first created. */
      •      @Override
      •      public void onCreate(Bundle savedInstanceState) {
      •      super.onCreate(bundle);
      •      getWindow().setFormat(PixelFormat.TRANSLUCENT);
      •      requestWindowFeature(Window.FEATURE_NO_TITLE);
      •      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
      •      WindowManager.LayoutParams.FLAG_FULLSCREEN);
      •      setContentView(R.layout.camera_surface);
      •      mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
      •      mSurfaceView.setOnClickListener((android.view.View.OnClickListener) this);
      •      mSurfaceHolder = mSurfaceView.getHolder();
      •      mSurfaceHolder.addCallback(this);
      •      mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      •      }
    • Using The Camera: Activity
      • /**
      • * When the surface is first created.
      • * Open the camera harware.
      • */
      • public void surfaceCreated(SurfaceHolder holder) {
      • mCamera = Camera.open();
      • }
      • /**
      •      * Called when the surface/format of the surface changes. Can be change in orientation or screen off.
      •      * Called after surfaceCreated. Set up camera settings.
      •      */
      • public void surfaceChanged(SurfaceHolder holder, int format, int width,
      • int height) {
      • if (mPreviewRunning) {
      • mCamera.stopPreview();
      • }
      • Camera.Parameters p = mCamera.getParameters();
      • p.setPreviewSize(width, height);
      • mCamera.setParameters(p);
      • try {
      • mCamera.setPreviewDisplay(holder);
      • } catch (IOException e) {
      • e.printStackTrace();
      • }
      • mCamera.startPreview();
      • mPreviewRunning = true;
      • }
      • /**
      • * Called when surface is destroyed.  When screen is black.
      • */
      • public void surfaceDestroyed(SurfaceHolder holder) {
      • mCamera.stopPreview();
      • mPreviewRunning = false;
      •                  mCamera.release();
      • }
    • Using The Camera: Activity
      • /**
      • * The camera callback. Put code that deals with the image returned in byte form.
      • */
      • Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
      • public void onPictureTaken(byte[] imageData, Camera c) {
      • }
      • /*
      • * What is done with the data (imageData) byte stream. 
      • * Can save it to files or edit it. 
      • */
      • };
      • /**
      • * Takes the picture when the screen is touched.
      • * @param v
      • */
      • public void onClick(View v) {
      • //Call on the camera  hardware to take a picture.
      • mCamera.takePicture(null, mPictureCallback, mPictureCallback);
      • mCamera.startPreview();
      • mPreviewRunning = true;
      • }
      •    
      • }
    •      Flow of Taking a Picture
    • Additional Resources 
      • API: 
      • http://developer.android.com/reference/packages.html
      • Developers guide: 
      • http://developer.android.com/guide/index.html
      • http://stackoverflow.com  
      • is a great place to ask questions
      • When in doubt, Google knows all
    • Workshop
      • Implement an application which reads a URL from a QR code and then opens the page in a browser.
      • API: 
      • http://developer.android.com/reference/packages.html
      • Barcode Reader:
      • http://code.google.com/p/zxing/
      Normal Project: http://people.rit.edu/cld8812/Exersize2.zip Easy Project: http://people.rit.edu/cld8812/Exersize2Easy.zip
    • Thanks for Coming!
      • The completed workshop can be found at:
      • http://people.rit.edu/cld8812/ExersizeFinal.zip