Professional Documents
Culture Documents
Marker
Hello Map
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
Location
How?
Abstracted by LocationManager which allows you:
1. Query for the list of all LocationProviders for the last known
user location.
Note: You always need the INTERNET permission to be able to reach the Google Maps
servers and render the map on your app.
Location
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
1.Start application.
To prevent Google Play Store from displaying your app on devices that
don't support OpenGL ES version 2, add this to the AndroidManifest.xml:
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
Architecture 101
Most of the apps you can find online…
(or at WIT a couple of years ago)…
8. Keep in memory
Activity Application
1. startTask(params) 7. Callback to activity
AsyncTask
2. start (params) 6. Operation complete callback
Processor
3. start (params) 5. REST method complete callback
REST Method
4. GET/POST
The ones which need database…
8. Keep in memory
Activity Application
1. startTask(params) 7. Callback to activity 9. Store to database/file
Processor
3. start (params) 5. REST method complete callback
REST Method
4. GET/POST
So…? Anything wrong?
So what?
Reality:
Problems:
Create a block.
Carefully shaped. LIKE LEGO.
You connect a LEGO piece into another.
They COUPLE PERFECTLY.
- Mark data
Database
…if app is killed and resumed it
knows its last current state.
- Minimize network usage: avoid getting the same data all the time)
Database
Ladies and gentleman, the Android Service:
- Create specifically for long running tasks. If you use binders you can define interfaces
- If a service is running the OS will only kill it in last resort (so be friendly…stop it when not
using it)
- Can have a ThreadPool inside (or use AsyncTasks TP), to queue requests
Database
Fragments
Fragments
The Activity is responsible for one or The fragments can always call getActivity() to
multiple fragments. obtain the current activity which is it’s parent
(if there is one).
The activity by default can only get the
fragments by the FragmentManager Everytime you should validate:
- If null (an Activity can detach)
- The instanceof before casting to a specif
type (maybe do a generic Activity with an
Remember: Use a fragment to make interface that all implement?)
smaller “LEGO blocks” that are easier to
maintain an can be reused.
Fragments
public abstract class AbstractActivity implements public abstract class AbstractFragment extends Fragment implements
ControllerInterface { FragmentInterface {
public interface ControllerInterface {
Private List<FragmentInterface> mMyFragments = new public void registerFrag(FragmentInterface delegate);
ArrayList<FragmentInterface>();
public void unregisterFrag(FragmentInterface delegate);
@Override }
public void registerFrag(FragmentInterface delegate)
{ public void onActivityCreated(Bundle savedInstanceState) {
mMyFragments.add(delegate); super.onActivityCreated(savedInstanceState);
} final ControllerInterface controller =
(ControllerInterface) getActivity();
@Override controller.registerFrag(this);
public void unregisterFrag(FragmentInterface delegate) }
{
mMyFragments.remove(delegate); public void onDestroy() {
} super.onDestroy();
final ControllerInterface controller =
(ControllerInterface) getActivity();
controller.unregisterFrag(this);
}
BL Manager
BL Manager
The Activity has the logic to control the The BL Manager is the core of the Application.
Views but does not have the Business It contains the logic specific of the application
logic. you are developing.
Basically the Activity should pass the For bigger apps there should be multiple
user-interaction to the BL Manager and managers.
receive from it the output or data to
handle the views. It is normally a Singleton which lives in the
ApplicationSingleton…
It should run on the UI Thread when …yeah I don’t like singletons too.. but let me
changing the Views. explain.
bus.post(login);
Or whatever you
} like!
BL Manager
BL Manager
@Override super.onCreate();
public void onServiceConnected(ComponentName }
className, IBinder service) {
if(service != null) { private void doAwesomeStuff(){
mServiceBinder = (ServiceBinder) service; // do stuff
mServiceBounded = true; }
mServiceBinder.registerManager(this);
} public class ServiceBinder extends Binder implements
} ServiceInterface {
@Override @Override
public void onServiceDisconnected(ComponentName public void doAwesomeStuff() {
className) { Service.this. doAwesomeStuff();
mServiceBounded = false;
} }
}; }
public class TutorialProvider extends ContentProvider { @Override
public static final String CONTENT_DATABASE_TABLE = public Cursor query(Uri uri, String[] projection, String