Share
Embed
Transcript
56 slides
-
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)); } } -
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); } -
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; } } -
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> -
PreferenceActivity public class ExamplePreferences extends PreferenceActivity { @Override protected void
onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } } -
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); } } -
Single Threaded UI Model UI Event System Event Queue Looper
Activity1 Activity2 Broadcast Receiver ... -
Activity Lifecycle public class BigButtonActivity extends Activity { @Override public
void onCreate(Bundle savedInstanceState) { ... } @Override public void onPause() { ... } @Override public void onResume() { ... } } -
Intents Intent targetIntent = new Intent(context, TargetActivity.class); targetIntent.putExtra(“name”, “Andy”) targetIntent.putExtra(“age”,
42); activity.startActivity(targetIntent); Explicit Intent -
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”); } } -
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 -
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 } } -
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> -
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); } } -
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(); } -
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> -
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/ -
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 ); } } -
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); } -
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); ... } } -
Otto @Subscribe public void answerAvailable(AnswerEvent event) { // TODO: React
to the event somehow! } bus.post(new AnswerAvailableEvent(42)); -
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!")); } } -
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); } }