Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Introduction

Android Introduction

John Ericksen

November 06, 2012
Tweet

More Decks by John Ericksen

Other Decks in Programming

Transcript

  1. Activity public class BigButtonActivity extends Activity { @Override public void

    onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.Button01); button.setOnLongClickListener(new LongClickLogger(button)); } }
  2. Layout public class TestCreate extends Activity { public void onCreate(Bundle

    savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); ll.addView(new TextView(this)); ll.addView(new EditText(this)); ll.addView(new Button(this)); setContentView(ll); }
  3. R Class public final class R { public static final

    class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int Button01=0x7f050000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } }
  4. Manifest <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.apis"> <uses-permission android:name="android.permission.INTERNET" /> <application android:name="ApiDemosApplication"> <activity

    android:name="ApiDemos"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
  5. PreferenceActivity public class ExamplePreferences extends PreferenceActivity { @Override protected void

    onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } }
  6. ListActivity public class MyListAdapter extends ListActivity { @Override protected void

    onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.custom_list_activity_view); ArrayAdapter<ActivityListItem> adapter = ... setListAdapter(adapter); } }
  7. Single Threaded UI Model UI Event System Event Queue Looper

    Activity1 Activity2 Broadcast Receiver ...
  8. Activity Lifecycle public class BigButtonActivity extends Activity { @Override public

    void onCreate(Bundle savedInstanceState) { ... } @Override public void onPause() { ... } @Override public void onResume() { ... } }
  9. Intents public class BigButtonActivity extends Activity { @Override public void

    onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String name = saveInstanceState.getStringExtra(“name”); int age = saveInstanceState.getIntExtra(“age”); } }
  10. public class BigButtonActivity extends Activity { @Override public void onCreate(Bundle

    savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locMan = (LocationManager)getSystemService(LOCATION_SERVICE); } } Managers
  11. Background Tasks public class AsyncTaskExample extends AsyncTask<...> { @Override protected

    Result doInBackground(Params... params) { //do background work return new Result(); } @Override protected void onPostExecute(Result result) { //return result to caller } }
  12. Fragments <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" >

    <fragment android:id="@+id/listFragment" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:layout_marginTop="?android:attr/actionBarSize" class="com.example.android.rssfeed.MyListFragment" ></fragment> <fragment android:id="@+id/detailFragment" android:layout_width="0dp" android:layout_weight="2" android:layout_height="match_parent" class="com.example.android.rssfeed.DetailFragment" > <!-- Preview: layout=@layout/details --> </fragment> </LinearLayout>
  13. Fragments public class DetailFragment extends Fragment { @Override public View

    onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.rssitem,container, false); return view; } public void setText(String item) { TextView view = (TextView) getView().findViewById(R.id.detailsText); view.setText(item); } }
  14. Service public class LocationPollingService extends Service { private Timer timer;

    private LocationPollingTimerTask task; private static final int INTERVAL = 60000; public void onCreate(){ timer = new Timer(); task = new LocationPollingTimerTask(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { timer.scheduleAtFixedRate(task , INTERVAL, INTERVAL); return Service.MODE_PRIVATE; } @Override public void onDestroy() { timer.cancel(); }
  15. Broadcast Receiver public class AppStartupBR extends BroadcastReceiver { @Override public

    void onReceive(Context context, Intent intent) { Log.i("Startup", "Startup logged"); } } <receiver android:name=".AppStartupBR"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
  16. NoSQL? Object Databases: NeoDatis Sport sport = new Sport("volley-ball"); //

    Open the database ODB odb = NeoDatis.open("test.neodatis"); // Store the object odb.store(sport); // Close ODB odb.close(); http://neodatis.wikidot.com/
  17. RoboGuice class RoboWay extends RoboActivity { @InjectView(R.id.name) TextView name; @InjectView(R.id.thumbnail)

    ImageView thumbnail; @InjectResource(R.drawable.icon) Drawable icon; @InjectResource(R.string.app_name) String myName; @Inject LocationManager loc; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); name.setText( "Hello, " + myName ); } }
  18. AndroidAnnotations @EActivity(R.layout.bookmarks) public class BookmarksToClipboardActivity extends Activity { BookmarkAdapter adapter;

    @ViewById ListView bookmarkList; @ViewById EditText search; @AfterViews void initBookmarkList() { adapter = new BookmarkAdapter(this); bookmarkList.setAdapter(adapter); }
  19. Dagger class CoffeeApp implements Runnable { @Inject CoffeeMaker coffeeMaker; @Override

    public void run() { coffeeMaker.brew(); } public static void main(String[] args) { ObjectGraph objectGraph = ObjectGraph.create( new DripCoffeeModule()); CoffeeApp coffeeApp = objectGraph.get(CoffeeApp.class); ... } }
  20. Otto @Subscribe public void answerAvailable(AnswerEvent event) { // TODO: React

    to the event somehow! } bus.post(new AnswerAvailableEvent(42));
  21. Robolectric @RunWith(RobolectricTestRunner.class) public class MyActivityTest { private Activity a; private

    Button pressMeButton; private TextView results; @Before public void setUp() throws Exception { a = new MyActivity(); a.onCreate(null); pressMeButton = (Button) a.findViewById(R.id.press_me_button); results = (TextView) a.findViewById(R.id.results_text_view); } @Test public void shouldUpdateResultsWhenButtonIsClicked() throws Exception{ pressMeButton.performClick(); String resultsText = results.getText().toString(); assertThat(resultsText, equalTo("Testing Android Rocks!")); } }
  22. Transfuse @Activity(label = "@string/app_name") @Layout(R.layout.main) public class HelloTransfuse { @Inject

    @View(R.id.textview) private TextView textView; @Inject @Resource(R.string.hello) private String helloText; @OnCreate public void hello() { textView.setText(helloText); } }