Download - What's New in Android
![Page 1: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/1.jpg)
What’s New in AndroidGetting up to speed with HC/ICS
Robert CooperReach Health@kebernet / +Robert Cooper
![Page 2: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/2.jpg)
Why are you here?
![Page 3: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/3.jpg)
You never upgraded your app for Honeycomb
You think cloning your iOS app to Android is a keen idea
You are completely new to Android(most of this will pass you buy, but try and osmotically absorb some of it)
Android is neat
Your boss said you need to be on this track
![Page 4: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/4.jpg)
Who is this guy?
![Page 5: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/5.jpg)
I wrote this... (not related)
![Page 6: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/6.jpg)
I work here…
![Page 7: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/7.jpg)
I also do this stuff…
![Page 8: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/8.jpg)
So what is new?
![Page 9: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/9.jpg)
New UI metaphors ActionBar Onscreen, adaptive menus
Spec Hardware changes No hard buttons
New look and feel Mandatory for ICS across OEMs to render unmodified
![Page 10: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/10.jpg)
So what is new?
![Page 11: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/11.jpg)
![Page 12: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/12.jpg)
![Page 13: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/13.jpg)
What does Logical Up Mean?
It means back to the list for alternate or sub views.
![Page 14: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/14.jpg)
New LaF
![Page 15: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/15.jpg)
Holographic look and feel added
Make it more TRON-ish…
… but not too TRON-ish
Lots of glows, varied depth line markers, 3D transitions
Improved text ops mechanics
![Page 16: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/16.jpg)
![Page 17: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/17.jpg)
New APIs
![Page 18: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/18.jpg)
Fragments Sub-Activities
ActionBar New Menuing and Nav System.
Enhanced Interaction for Widgets and Notifications
Drag and Drop
P2P Networking NFC (Android Beam) WiFi Direct
![Page 19: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/19.jpg)
Fragments
![Page 20: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/20.jpg)
Fragments are Sub-Activities that can be recomposed based on UI factors (screen size, orientation, etc)
Introduced with 3.0 tablets now global in 4.0
Available as a build-in backport as far back as 1.6 with the “Android Compatibility Package” (Available in the SDK/AVD Manager)
![Page 21: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/21.jpg)
Mimics Activity Lifecycle
onCreate()
onStart()
onCreateView() (this is new!)
onPause()
onStop()
![Page 22: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/22.jpg)
Fragments are Layout parts
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.example.news.ArticleListFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.news.ArticleReaderFragment" android:id="@+id/viewer" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /></LinearLayout>
![Page 23: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/23.jpg)
Fragments are Sub-Activities that can be Each Fragment becomes unique in the application
Can move between Activities with different combinations of Fragments by passing Fragment model/URI information using the FragmentManager API.
FragmentTransaction can be used to manipulate fragment state and “back” behavior
![Page 24: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/24.jpg)
FragmentTransaction manipulates the back state
Fragment newFragment = new ExampleFragment();FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);transaction.addToBackStack(null);
transaction.commit();
![Page 25: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/25.jpg)
ActionBar(Know it. Love it.)
![Page 26: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/26.jpg)
What is it?
New nav metaphor Home Logical “UP” Activity global tabs
New menuing system Toolbar Overflow menuing
![Page 27: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/27.jpg)
How to Use It
NOT part of the compatibility package Check out ActionBarSherlock.com for a backport
You MUST request the Holographic theme android:theme="@android:style/Theme.Holo” android:theme="@android:style/Theme.Holo.Light” setTheme(android.R.style.Theme_Holo); setTheme(android.R.style.Theme_Holo_Light);
![Page 28: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/28.jpg)
Menus on the ActionBar
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_add" android:icon="@drawable/ic_menu_save" android:title="@string/menu_save" android:showAsAction="ifRoom|withText" /></menu>
![Page 29: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/29.jpg)
![Page 30: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/30.jpg)
Custom Views in the ActionBar
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_search" android:title="Search" android:icon="@drawable/ic_menu_search" android:showAsAction="ifRoom”
android:actionLayout="@layout/searchview"
android:actionViewClass="android.widget.SearchView” /></menu>
![Page 31: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/31.jpg)
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search) .getActionView();
![Page 32: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/32.jpg)
Getting the “Home” icon view
View home = a.findViewById(android.R.id.home);
home.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
a.finish();
}
});
Adding “Up” marker
ActionBar actionBar = this.getActionBar(); //or SherlockActivity.getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
![Page 33: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/33.jpg)
ActionBar Tabs final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS);
// remove the activity title to make space for tabsactionBar.setDisplayShowTitleEnabled(false);
Fragment artistsFragment = new ArtistsFragment();actionBar.addTab(actionBar.newTab()
.setText(R.string.tab_artists) .setTabListener(new TabListener(artistsFragment)));
Fragment albumsFragment = new AlbumsFragment();actionBar.addTab(actionBar.newTab()
.setText(R.string.tab_albums) .setTabListener(new TabListener(albumsFragment)));
![Page 34: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/34.jpg)
![Page 35: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/35.jpg)
Spinner/List/Drop down Nav ActionBar actionBar = getActionBar();
actionBar.setNavigationMode( ActionBar.NAVIGATION_MODE_LIST); actionBar.setListNavigationCallbacks( new SpinnerAdapter(){ public View getDropDownView(int position, View convertView, View Group parent){ // … }
}, new OnNavigationListener(){
public boolean onNavigationItemSelected( int itemPosition, long itemId){ //… } });
![Page 36: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/36.jpg)
![Page 37: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/37.jpg)
Notifications(They do stuff
now)
![Page 38: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/38.jpg)
Notifications can now use RemoteViews to allow interaction with the popup notification, rather than just launch an intent.
RemoteViews layout = new RemoteViews( getPackageName(), R.layout.notification);
notification.contentView = layout;
layout.setOnClickPendingIntent( R.id.my_button, getDialogPendingIntent( "You pressed it!"));
![Page 39: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/39.jpg)
PendingIntent getDialogPendingIntent( String dialogText) { return PendingIntent.getActivity( this, // send back to the creating Act. dialogText.hashCode(), new Intent(ACTION_DIALOG) .putExtra(Intent.EXTRA_TEXT, dialogText) .addFlags( Intent.FLAG_ACTIVITY_NEW_TASK), 0); }
![Page 40: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/40.jpg)
Handling the PendingIntent:
if (ACTION_DIALOG.equals(intent.getAction())) { showDialog(
intent.getStringExtra(
Intent.EXTRA_TEXT))
}
PendingIntent then becomes an invisible call back into your Activity.
![Page 41: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/41.jpg)
Drag and Drop
![Page 42: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/42.jpg)
Any View can now be dragged about the screen.
To begin a drag action call:myView.startDrag( dragData, dragShadowBuilder, localData, 0 /*unused int flags */);
Can be called from you OnClick/OnLongClick listeners…
localData is just any Object that will be sent with each DragEvent.
![Page 43: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/43.jpg)
Create the DrawShadowBuilder. This returns the view that is dragged about under the pointer.
This class takes a View as an argument and looks a lot like the stock View paint lifecycle.@Overridepublic void onProvideShadowMetrics(Point size, Point touch)@Overridepublic void onDrawShadow(Canvas canvas)
The first method sets the bounds, the second paints to the canvas.
You can use the View’s existing draw() method then mutate it (read: opacity)
![Page 44: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/44.jpg)
DragEvents onDragEvent(DragEvent)
or View.OnDragListener on any view (These are really for
Drop Targets)
DragEvent.getAction() returns one of the possible event action types.
![Page 45: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/45.jpg)
ACTION_DRAG_STARTED Sent to all active Views – check here for drop target validity!
ACTION_DRAG_ENTERED Sent when the touch enters the box of the View
ACTION_DRAG_LOCATION Sent on each move while in the box of the View
ACTION_DRAG_EXITED Sent when the touch leaves the box.
ACTION_DROP Sent on drop event *ONLY* when the View/Listener returned
“true” from the ACTION_DRAG_STARTED event.
![Page 46: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/46.jpg)
Android Beam
![Page 47: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/47.jpg)
You App has Data
People like to share data
People like to move data between devices
The Cloud is Magic™
NFC is complex. Android Beam is Simple
![Page 48: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/48.jpg)
Sending a Beam
Call NfcAdapter.getDefaultAdapther(ctx)
Check for null.
Call .setNdefPushMessage() with your Activity and your data
Profit!
![Page 49: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/49.jpg)
final byte[] langBytes = locale.getLanguage().getBytes(Charsets.US_ASCII);
final Charset utfEncoding = encodeInUtf8 ? Charsets.UTF_8 : Charset.forName("UTF-16");
final byte[] textBytes =text.getBytes(utfEncoding);
final int utfBit = encodeInUtf8 ? 0 : (1 << 7);
final char status = (char) (utfBit + langBytes.length);
final byte[] data = Bytes.concat(new byte[] {(byte) status}, langBytes, textBytes);
new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
![Page 50: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/50.jpg)
RTD_URI is a lot easier!
![Page 51: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/51.jpg)
final byte[] typeBytes = "image/jpeg” .getBytes(Charsets.US_ASCII);
final byte[] data = getData();
new NdefRecord(NdefRecord.TNF_MIME_MEDIA, typeBytes, new byte[0], data);
![Page 52: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/52.jpg)
Getting a Beam
Call NfcAdapter. setNdefPushMessageCallback() with your activity(ies)
OR
Handle ACTION_NDEF_DISCOVERED in your Manifest for URLs or Mime Types
Profit!
![Page 53: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/53.jpg)
Too easy NOT to do.
![Page 54: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/54.jpg)
WiFi Direct(or JINI with
Sockets)
![Page 55: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/55.jpg)
WiFi Direct
Based on the idea of Channels, Groups, and Peers Channels – managed by the OS, like a WiFi Channel
ID Groups – Ad host client-server networks. One device
is the master, and other machines can connect to it Peers – Devices attached to the Group
You can monitor peer presence within your channel
Once you identify peers, you can use “Plain Old Sockets” to talk to them
![Page 56: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/56.jpg)
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver { private WifiP2pManager manager; private Channel channel; private MyWiFiActivity activity;
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel, ApplicationActivity activity) { super();
this.manager = manager; this.channel = channel; this.activity = activity; }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { } else if
(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { } }}
![Page 57: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/57.jpg)
manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = manager.initialize(this, getMainLooper(), null); receiver = new WiFiDirectBroadcastReceiver(manager,
channel, this);
// ...
@Override protected void onResume() { super.onResume(); registerReceiver(receiver, intentFilter); manager.createGroup(channel, new ActionListener(){ //...
} } @Override protected void onPause() { super.onPause(); unregisterReceiver(receiver); }
![Page 58: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/58.jpg)
Talking to Others
Request a list of peers
Select a Device from the list of peers
Connect to the Device – this is Data Link connect, not Application connect
Open your sockets
Once connected
![Page 59: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/59.jpg)
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() {
manager.requestPeers( new PeerListListener(){ public void onPeersAvailable(WiFiP2pDeviceList devices){ // do stuff here } } }
@Override public void onFailure(int reasonCode) { }});
//…
WifiP2pConfig config = new WifiP2pConfig();config.deviceAddress = device.deviceAddress;manager.connect(channel, config, new ActionListener() {
@Override public void onSuccess() { }
@Override public void onFailure(int reason) { }});
![Page 60: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/60.jpg)
WifiP2pConfig config = new WifiP2pConfig();config.deviceAddress = device.deviceAddress;manager.connect(channel, config, new ActionListener() {
@Override public void onSuccess() { }
@Override public void onFailure(int reason) { }});
// from ConnectionInfoListener…
@Override public void onConnectionInfoAvailable(
WifiP2pInfo info) { info.groupOwnerAddress.getHostAddress();
![Page 61: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/61.jpg)
Check the Demo App
http://developer.android.com/resources/samples/WiFiDirectDemo/index.html
In the SDK samples
![Page 62: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/62.jpg)
What to Remember
![Page 63: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/63.jpg)
Make your app Androidish(but not too Androidish)
Use Fragments to recompose your app for various form factors
Android Beam support is brain free. Your app *should* support it.
WiFi direct can take the pain out of true on-the-go P2P networking. (Save your users WAN transfers)
![Page 64: What's New in Android](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555ab31ad8b42a405b8b4f1e/html5/thumbnails/64.jpg)
Thanks!
Robert CooperReach Health@kebernet / +Robert Cooper