SlideShare a Scribd company logo
1 of 31
Download to read offline
Networking on mobile
devices
M.V.Davydov
Lviv Polytechnic National University
Supported networking means
• Cellural data
• WiFi
• Bluetooth
• SMS
• NFC
Required permissions
In order to perform network operations in your application, your
manifest must include the following permissions
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK
_STATE" />
Networking should be
performed over separate thread
To avoid creating an unresponsive UI, don't perform
network operations on the UI thread. By default,
Android 3.0 (API level 11) and higher requires you to
perform network operations on a thread other than
the main UI thread; if you don't, a
NetworkOnMainThreadException is thrown.
private String downloadUrl(URL url) throws IOException {
    InputStream stream = null;
    HttpsURLConnection connection = null;
    String result = null;
    try {
        connection = (HttpsURLConnection) url.openConnection();
        connection.setReadTimeout(3000);
        connection.setConnectTimeout(3000);
        connection.setRequestMethod("GET");
        // Already true by default but setting just in case; needs to be true since this request
        // is carrying an input (response) body.
        connection.setDoInput(true);
        // Open communications link (network traffic occurs here).
        connection.connect();
        publishProgress(DownloadCallback.Progress.CONNECT_SUCCESS);
        int responseCode = connection.getResponseCode();
        if (responseCode != HttpsURLConnection.HTTP_OK) {
            throw new IOException("HTTP error code: " + responseCode);
        }
        // Retrieve the response body as an InputStream.
        stream = connection.getInputStream();
        publishProgress(DownloadCallback.Progress.GET_INPUT_STREAM_SUCCESS, 0);
        if (stream != null) {
            // Converts Stream to String with max length of 500.
            result = readStream(stream, 500);
        }
    } finally {
        // Close Stream and disconnect HTTPS connection.
        if (stream != null) {
            stream.close();
        }
        if (connection != null) {
            connection.disconnect();
        }
    }
    return result;
}
Implementation as AsyncTask (more info
at https://developer.android.com/training/
basics/network-ops/connecting.html)
/**
 * Implementation of AsyncTask designed to fetch data from
the network.
 */
private class DownloadTask extends AsyncTask<String, Void,
DownloadTask.Result> {
    private DownloadCallback<String> mCallback;
    DownloadTask(DownloadCallback<String> callback) {
        setCallback(callback);
    }
    void setCallback(DownloadCallback<String> callback) {
        mCallback = callback;
    }
•
     /**
     * Wrapper class that serves as a union of a
result value and an exception. When the download
     * task has completed, either the result value
or exception can be a non-null value.
     * This allows you to pass exceptions to the UI
thread that were thrown during doInBackground().
     */
    static class Result {
        public String mResultValue;
        public Exception mException;
        public Result(String resultValue) {
            mResultValue = resultValue;
        }
        public Result(Exception exception) {
            mException = exception;
        }
    }
•
/**
* Cancel background network operation if we do not have
network connectivity.
*/
@Override
protected void onPreExecute() {
    if (mCallback != null) {
        NetworkInfo networkInfo =
mCallback.getActiveNetworkInfo();
        if (networkInfo == null ||
!networkInfo.isConnected() ||
(networkInfo.getType() != ConnectivityManager.TYPE_WIFI &&
networkInfo.getType() != ConnectivityManager.TYPE_MOBILE)) {
           // If no connectivity, cancel task and update
Callback with null data.
            mCallback.updateFromDownload(null);
            cancel(true);
        }
    }
}
    /**
     * Defines work to perform on the background thread.
     */
    @Override
    protected DownloadTask.Result doInBackground(String... urls) {
        Result result = null;
        if (!isCancelled() && urls != null && urls.length > 0) {
            String urlString = urls[0];
            try {
                URL url = new URL(urlString);
                String resultString = downloadUrl(url);
                if (resultString != null) {
                    result = new Result(resultString);
                } else {
                    throw new IOException("No response received.");
                }
            } catch(Exception e) {
                result = new Result(e);
            }
        }
        return result;
    }
    /**
     * Updates the DownloadCallback with the result.
     */
    @Override
    protected void onPostExecute(Result result) {
        if (result != null && mCallback != null) {
            if (result.mException != null) {
                mCallback.updateFromDownload
(result.mException.getMessage());
            } else if (result.mResultValue != null) {
                mCallback.updateFromDownload
(result.mResultValue);
            }
            mCallback.finishDownloading();
        }
    }
Another example of AsyncTask usage
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }
     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }
     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }
Usage:
new DownloadFilesTask().execute(url1, url2, url3);
Best security practices
• Minimize the amount of sensitive or personal user data that
you transmit over the network.
• Send all network traffic from your app over SSL.
• Consider creating a network security configuration, which
allows your app to trust custom CAs or restrict the set of
system CAs that it trusts for secure communication.
In cryptography, a certificate authority or certification authority
(CA) is an entity that issues digital certificates.
Networking Security configuration
The key capabilities of this feature are as follows:
• Custom trust anchors: Customize which Certificate Authorities
(CA) are trusted for an app's secure connections. For example,
trusting particular self-signed certificates or restricting the set of
public CAs that the app trusts.
• Debug-only overrides: Safely debug secure connections in an
app without added risk to the installed base.
• Cleartext traffic opt-out: Protect apps from accidental usage of
cleartext traffic.
• Certificate pinning: Restrict an app's secure connection to
particular certificates.
Adding a Security Configuration File
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    <application android:networkSecurityConfig=
"@xml/network_security_config"
                    ... >
        ...
    </application>
</manifest>
•
Configuring a Custom CA
res/xml/network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config>
        <domain includeSubdomains="true">example.com</
domain>
        <trust-anchors>
            <certificates src="@raw/my_ca"/>
        </trust-anchors>
    </domain-config>
</network-security-config>
Add the self-signed or non-public CA certificate, in PEM or DER format, to
res/raw/my_ca.
You can also provide multiple <certificates> elements instead of one.
Configuring CAs for Debugging
When debugging an app that connects over HTTPS, you may
want to connect to a local development server, which does not
have the SSL certificate for your production server. In order to
support this without any modification to your app's code, you can
specify debug-only CAs, which are trusted only when
android:debuggable is true, by using debug-overrides.
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <debug-overrides>
        <trust-anchors>
            <certificates src="@raw/debug_cas"/>
        </trust-anchors>
    </debug-overrides>
</network-security-config>
Opting Out of Cleartext Traffic
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="false">
       <domain includeSubdomains="true">secure.example.com</domain>
    </domain-config>
</network-security-config>
Check a Device's Network Connection
private static final String DEBUG_TAG =
"NetworkStatusExample";
...
ConnectivityManager connMgr = (ConnectivityManager)
        getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isWifiConn = networkInfo.isConnected();
networkInfo =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = networkInfo.isConnected();
Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);
Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);
Checking whether you are online
public boolean isOnline()
{
    ConnectivityManager connMgr =
(ConnectivityManager)
          getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    return (networkInfo != null &&
networkInfo.isConnected());
}  
Detect Connection Changes
public class NetworkReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager conn =  (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = conn.getActiveNetworkInfo();
    if (networkInfo != null &&
networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
{
        Toast.makeText(context, R.string.wifi_connected,
Toast.LENGTH_SHORT).show();
    }
. . .
}
Use broadcast receivers carefully
Setting up a BroadcastReceiver that gets called unnecessarily
can be a drain on system resources. The sample application
registers the BroadcastReceiver NetworkReceiver in
onCreate(), and it unregisters it in onDestroy(). This is
more lightweight than declaring a <receiver> in the manifest.
When you declare a <receiver> in the manifest, it can wake
up your app at any time, even if you haven't run it for weeks. By
registering and unregistering NetworkReceiver within the
main activity, you ensure that the app won't be woken up after
the user leaves the app. If you do declare a <receiver> in the
manifest and you know exactly where you need it, you can use
setComponentEnabledSetting() to enable and disable it as
appropriate.
Use Firebase Cloud Messaging as an
Alternative to Polling
Every time your app polls your server to check if an update is
required, you activate the wireless radio, drawing power
unnecessarily, for up to 20 seconds on a typical 3G connection.
Firebase Cloud Messaging (FCM) is a lightweight mechanism used
to transmit data from a server to a particular app instance. Using
FCM, your server can notify your app running on a particular device
that there is new data available for it.
Compared to polling, where your app must regularly ping the server
to query for new data, this event-driven model allows your app to
create a new connection only when it knows there is data to
download. The model minimizes unnecessary connections and
reduces latency when updating information within your app.
Using Firebase for Networking
Add Firebase to Your Android Project
Prerequisites
• A device running Android 4.0 (Ice Cream Sandwich) or newer,
and Google Play services 10.2.1 or higher
• The Google Play services SDK from the Google Repository,
available in the Android SDK Manager
• The latest version of Android Studio, version 1.5 or higher
Use the Firebase Assistant
If you're using the latest version of Android Studio (version 2.2 or
later), we recommend using the Firebase Assistant to connect
your app to Firebase. The Firebase Assistant can connect your
existing project or create a new one for you and automatically
install any necessary gradle dependencies.
To open the Firebase Assistant in Android Studio:
• Click Tools > Firebase to open the Assistant window.
• Click to expand one of the listed features (for example,
Analytics), then click the provided tutorial link (for example,
Log an Analytics event).
• Click the Connect to Firebase button to connect to Firebase
and add the necessary code to your app.
Available libs
Gradle Dependency Line Service
com.google.firebase:firebase-core:10.2.1 Analytics
com.google.firebase:firebase-database:10.2.1 Realtime Database
com.google.firebase:firebase-storage:10.2.1 Storage
com.google.firebase:firebase-crash:10.2.1 Crash Reporting
com.google.firebase:firebase-auth:10.2.1 Authentication
com.google.firebase:firebase-messaging:10.2.1 Cloud Messaging and Notifications
com.google.firebase:firebase-config:10.2.1 Remote Config
com.google.firebase:firebase-invites:10.2.1 Invites and Dynamic Links
com.google.firebase:firebase-ads:10.2.1 AdMob
com.google.firebase:firebase-appindexing:10.2.1 App Indexing
Firebase authentification for Android
Declare the FirebaseAuth and AuthStateListener objects.
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
In the onCreate() method, initialize the FirebaseAuth instance and the
AuthStateListener method so you can track whenever the user signs in or
out.
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser user = firebaseAuth.getCurrentUser();
        if (user != null) {
            // User is signed in
            Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
        } else {
            // User is signed out
            Log.d(TAG, "onAuthStateChanged:signed_out");
        }
        // ...
    }
};
Attach the listener to your FirebaseAuth instance in the onStart()
method and remove it on onStop().
@Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}
Sign up new users
Create a new createAccount method which takes in an email address and
password, validates them and then creates a new user with the
createUserWithEmailAndPassword method.
mAuth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
                // If sign in fails, display a message to the user. If sign in succeeds
                // the auth state listener will be notified and logic to handle the
                // signed in user can be handled in the listener.
                if (!task.isSuccessful()) {
                    Toast.makeText(EmailPasswordActivity.this, R.string.auth_failed,
                            Toast.LENGTH_SHORT).show();
                }
                // ...
            }
        });
EmailPasswordActivity.java
Sign in existing users
Create a new signIn method which takes in an email address and password, validates
them, and then signs a user in with the signInWithEmailAndPassword method.
mAuth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
                // If sign in fails, display a message to the user. If sign in succeeds
                // the auth state listener will be notified and logic to handle the
                // signed in user can be handled in the listener.
                if (!task.isSuccessful()) {
                    Log.w(TAG, "signInWithEmail:failed", task.getException());
                    Toast.makeText(EmailPasswordActivity.this, R.string.auth_failed,
                            Toast.LENGTH_SHORT).show();
                }
                // ...
            }
        });
Access user information
If a user has signed in successfully you can get their account data at any
point with the getCurrentUser method.
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // Name, email address, and profile photo Url
    String name = user.getDisplayName();
    String email = user.getEmail();
    Uri photoUrl = user.getPhotoUrl();
    // Check if user's email is verified
    boolean emailVerified = user.isEmailVerified();
    // The user's ID, unique to the Firebase project. Do NOT use this value to
    // authenticate with your backend server, if you have one. Use
    // FirebaseUser.getToken() instead.
    String uid = user.getUid();
}

More Related Content

What's hot

What's hot (20)

Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android
 
Android Intent.pptx
Android Intent.pptxAndroid Intent.pptx
Android Intent.pptx
 
Data Persistence in Android with Room Library
Data Persistence in Android with Room LibraryData Persistence in Android with Room Library
Data Persistence in Android with Room Library
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
Introduction to Android Fragments
Introduction to Android FragmentsIntroduction to Android Fragments
Introduction to Android Fragments
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Event handling
Event handlingEvent handling
Event handling
 
Android intents
Android intentsAndroid intents
Android intents
 
Notification android
Notification androidNotification android
Notification android
 
SQLite database in android
SQLite database in androidSQLite database in android
SQLite database in android
 
04 activities and activity life cycle
04 activities and activity life cycle04 activities and activity life cycle
04 activities and activity life cycle
 
Presentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestatePresentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestate
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
Content provider in_android
Content provider in_androidContent provider in_android
Content provider in_android
 
Google Maps in Android
Google Maps in AndroidGoogle Maps in Android
Google Maps in Android
 
Android notification
Android notificationAndroid notification
Android notification
 
Retrofit
RetrofitRetrofit
Retrofit
 
Android structure
Android structureAndroid structure
Android structure
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
OOP java
OOP javaOOP java
OOP java
 

Similar to Android Networking

Lecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesLecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesMaksym Davydov
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqelajobandesther
 
From Zero to Cloud in 12 Easy Factors
From Zero to Cloud in 12 Easy FactorsFrom Zero to Cloud in 12 Easy Factors
From Zero to Cloud in 12 Easy FactorsEd King
 
Connecting to the network
Connecting to the networkConnecting to the network
Connecting to the networkMu Chun Wang
 
Introduction to Things board (An Open Source IoT Cloud Platform)
Introduction to Things board (An Open Source IoT Cloud Platform)Introduction to Things board (An Open Source IoT Cloud Platform)
Introduction to Things board (An Open Source IoT Cloud Platform)Amarjeetsingh Thakur
 
Scaling asp.net websites to millions of users
Scaling asp.net websites to millions of usersScaling asp.net websites to millions of users
Scaling asp.net websites to millions of usersoazabir
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaYevgeniy Brikman
 
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 202010 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020Matt Raible
 
Ato2019 weave-services-istio
Ato2019 weave-services-istioAto2019 weave-services-istio
Ato2019 weave-services-istioLin Sun
 
Weave Your Microservices with Istio
Weave Your Microservices with IstioWeave Your Microservices with Istio
Weave Your Microservices with IstioAll Things Open
 
All Things Open 2019 weave-services-istio
All Things Open 2019 weave-services-istioAll Things Open 2019 weave-services-istio
All Things Open 2019 weave-services-istioLin Sun
 
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade ServerlessKatyShimizu
 
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade ServerlessKatyShimizu
 
Writing SOLID C++ [gbgcpp meetup @ Zenseact]
Writing SOLID C++ [gbgcpp meetup @ Zenseact]Writing SOLID C++ [gbgcpp meetup @ Zenseact]
Writing SOLID C++ [gbgcpp meetup @ Zenseact]Dimitrios Platis
 
Simplify Cloud Applications using Spring Cloud
Simplify Cloud Applications using Spring CloudSimplify Cloud Applications using Spring Cloud
Simplify Cloud Applications using Spring CloudRamnivas Laddad
 
Data Access Mobile Devices
Data Access Mobile DevicesData Access Mobile Devices
Data Access Mobile Devicesvenkat987
 

Similar to Android Networking (20)

Lecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile DevicesLecture 10 Networking on Mobile Devices
Lecture 10 Networking on Mobile Devices
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqela
 
From Zero to Cloud in 12 Easy Factors
From Zero to Cloud in 12 Easy FactorsFrom Zero to Cloud in 12 Easy Factors
From Zero to Cloud in 12 Easy Factors
 
Connecting to the network
Connecting to the networkConnecting to the network
Connecting to the network
 
Introduction to Things board (An Open Source IoT Cloud Platform)
Introduction to Things board (An Open Source IoT Cloud Platform)Introduction to Things board (An Open Source IoT Cloud Platform)
Introduction to Things board (An Open Source IoT Cloud Platform)
 
Web sockets in Java
Web sockets in JavaWeb sockets in Java
Web sockets in Java
 
Scaling asp.net websites to millions of users
Scaling asp.net websites to millions of usersScaling asp.net websites to millions of users
Scaling asp.net websites to millions of users
 
I os 13
I os 13I os 13
I os 13
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and Scala
 
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 202010 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
 
What Is Happening At The Edge
What Is Happening At The EdgeWhat Is Happening At The Edge
What Is Happening At The Edge
 
Java RMI
Java RMIJava RMI
Java RMI
 
Ato2019 weave-services-istio
Ato2019 weave-services-istioAto2019 weave-services-istio
Ato2019 weave-services-istio
 
Weave Your Microservices with Istio
Weave Your Microservices with IstioWeave Your Microservices with Istio
Weave Your Microservices with Istio
 
All Things Open 2019 weave-services-istio
All Things Open 2019 weave-services-istioAll Things Open 2019 weave-services-istio
All Things Open 2019 weave-services-istio
 
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
 
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless
 
Writing SOLID C++ [gbgcpp meetup @ Zenseact]
Writing SOLID C++ [gbgcpp meetup @ Zenseact]Writing SOLID C++ [gbgcpp meetup @ Zenseact]
Writing SOLID C++ [gbgcpp meetup @ Zenseact]
 
Simplify Cloud Applications using Spring Cloud
Simplify Cloud Applications using Spring CloudSimplify Cloud Applications using Spring Cloud
Simplify Cloud Applications using Spring Cloud
 
Data Access Mobile Devices
Data Access Mobile DevicesData Access Mobile Devices
Data Access Mobile Devices
 

More from Maksym Davydov

Microsoft mobile services
Microsoft mobile servicesMicrosoft mobile services
Microsoft mobile servicesMaksym Davydov
 
Mobile app design feature development
Mobile app design feature developmentMobile app design feature development
Mobile app design feature developmentMaksym Davydov
 
Android mix Java and C++
Android mix Java and C++Android mix Java and C++
Android mix Java and C++Maksym Davydov
 
Handler declaration in layout
Handler declaration in layoutHandler declaration in layout
Handler declaration in layoutMaksym Davydov
 
Interface Programming Android
Interface Programming AndroidInterface Programming Android
Interface Programming AndroidMaksym Davydov
 
Android Programming Intro
Android Programming IntroAndroid Programming Intro
Android Programming IntroMaksym Davydov
 
Lecture 02 Mobile hardware
Lecture 02 Mobile hardwareLecture 02 Mobile hardware
Lecture 02 Mobile hardwareMaksym Davydov
 
Lecture 01 Mobile operating systems
Lecture 01 Mobile operating systemsLecture 01 Mobile operating systems
Lecture 01 Mobile operating systemsMaksym Davydov
 
Lecture 13 Local Optimization on Mobile Devices
Lecture 13 Local Optimization on Mobile DevicesLecture 13 Local Optimization on Mobile Devices
Lecture 13 Local Optimization on Mobile DevicesMaksym Davydov
 
Lecture 12. iOS and Android Animations
Lecture 12. iOS and Android AnimationsLecture 12. iOS and Android Animations
Lecture 12. iOS and Android AnimationsMaksym Davydov
 
Lecture 11. Microsoft mobile services
Lecture 11. Microsoft mobile servicesLecture 11. Microsoft mobile services
Lecture 11. Microsoft mobile servicesMaksym Davydov
 
Lecture 11 Firebase overview
Lecture 11 Firebase overviewLecture 11 Firebase overview
Lecture 11 Firebase overviewMaksym Davydov
 
Lecture 09 Android Storage
Lecture 09 Android StorageLecture 09 Android Storage
Lecture 09 Android StorageMaksym Davydov
 

More from Maksym Davydov (20)

Firebase overview
Firebase overviewFirebase overview
Firebase overview
 
Microsoft mobile services
Microsoft mobile servicesMicrosoft mobile services
Microsoft mobile services
 
Design of mobile apps
Design of mobile appsDesign of mobile apps
Design of mobile apps
 
Mobile app design feature development
Mobile app design feature developmentMobile app design feature development
Mobile app design feature development
 
Android mix Java and C++
Android mix Java and C++Android mix Java and C++
Android mix Java and C++
 
Android animations
Android animationsAndroid animations
Android animations
 
Handler declaration in layout
Handler declaration in layoutHandler declaration in layout
Handler declaration in layout
 
Android Storage
Android StorageAndroid Storage
Android Storage
 
Interface Programming Android
Interface Programming AndroidInterface Programming Android
Interface Programming Android
 
Java Small Tests
Java Small TestsJava Small Tests
Java Small Tests
 
Android Programming Intro
Android Programming IntroAndroid Programming Intro
Android Programming Intro
 
Lecture 02 Mobile hardware
Lecture 02 Mobile hardwareLecture 02 Mobile hardware
Lecture 02 Mobile hardware
 
Lecture 01 Mobile operating systems
Lecture 01 Mobile operating systemsLecture 01 Mobile operating systems
Lecture 01 Mobile operating systems
 
Lecture 13 Local Optimization on Mobile Devices
Lecture 13 Local Optimization on Mobile DevicesLecture 13 Local Optimization on Mobile Devices
Lecture 13 Local Optimization on Mobile Devices
 
Lecture 12. iOS and Android Animations
Lecture 12. iOS and Android AnimationsLecture 12. iOS and Android Animations
Lecture 12. iOS and Android Animations
 
Lecture 11. Microsoft mobile services
Lecture 11. Microsoft mobile servicesLecture 11. Microsoft mobile services
Lecture 11. Microsoft mobile services
 
Lecture 11 Firebase overview
Lecture 11 Firebase overviewLecture 11 Firebase overview
Lecture 11 Firebase overview
 
Lecture 09 Android Storage
Lecture 09 Android StorageLecture 09 Android Storage
Lecture 09 Android Storage
 
Lecture 08 Xamarin
Lecture 08 XamarinLecture 08 Xamarin
Lecture 08 Xamarin
 
Lecture 07 swift
Lecture 07 swiftLecture 07 swift
Lecture 07 swift
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

Android Networking

  • 1. Networking on mobile devices M.V.Davydov Lviv Polytechnic National University
  • 2. Supported networking means • Cellural data • WiFi • Bluetooth • SMS • NFC
  • 3. Required permissions In order to perform network operations in your application, your manifest must include the following permissions <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK _STATE" />
  • 4. Networking should be performed over separate thread To avoid creating an unresponsive UI, don't perform network operations on the UI thread. By default, Android 3.0 (API level 11) and higher requires you to perform network operations on a thread other than the main UI thread; if you don't, a NetworkOnMainThreadException is thrown.
  • 5. private String downloadUrl(URL url) throws IOException {     InputStream stream = null;     HttpsURLConnection connection = null;     String result = null;     try {         connection = (HttpsURLConnection) url.openConnection();         connection.setReadTimeout(3000);         connection.setConnectTimeout(3000);         connection.setRequestMethod("GET");         // Already true by default but setting just in case; needs to be true since this request         // is carrying an input (response) body.         connection.setDoInput(true);         // Open communications link (network traffic occurs here).         connection.connect();         publishProgress(DownloadCallback.Progress.CONNECT_SUCCESS);         int responseCode = connection.getResponseCode();         if (responseCode != HttpsURLConnection.HTTP_OK) {             throw new IOException("HTTP error code: " + responseCode);         }         // Retrieve the response body as an InputStream.         stream = connection.getInputStream();         publishProgress(DownloadCallback.Progress.GET_INPUT_STREAM_SUCCESS, 0);         if (stream != null) {             // Converts Stream to String with max length of 500.             result = readStream(stream, 500);         }     } finally {         // Close Stream and disconnect HTTPS connection.         if (stream != null) {             stream.close();         }         if (connection != null) {             connection.disconnect();         }     }     return result; }
  • 6. Implementation as AsyncTask (more info at https://developer.android.com/training/ basics/network-ops/connecting.html) /**  * Implementation of AsyncTask designed to fetch data from the network.  */ private class DownloadTask extends AsyncTask<String, Void, DownloadTask.Result> {     private DownloadCallback<String> mCallback;     DownloadTask(DownloadCallback<String> callback) {         setCallback(callback);     }     void setCallback(DownloadCallback<String> callback) {         mCallback = callback;     } •
  • 7.      /**      * Wrapper class that serves as a union of a result value and an exception. When the download      * task has completed, either the result value or exception can be a non-null value.      * This allows you to pass exceptions to the UI thread that were thrown during doInBackground().      */     static class Result {         public String mResultValue;         public Exception mException;         public Result(String resultValue) {             mResultValue = resultValue;         }         public Result(Exception exception) {             mException = exception;         }     } •
  • 8. /** * Cancel background network operation if we do not have network connectivity. */ @Override protected void onPreExecute() {     if (mCallback != null) {         NetworkInfo networkInfo = mCallback.getActiveNetworkInfo();         if (networkInfo == null || !networkInfo.isConnected() || (networkInfo.getType() != ConnectivityManager.TYPE_WIFI && networkInfo.getType() != ConnectivityManager.TYPE_MOBILE)) {            // If no connectivity, cancel task and update Callback with null data.             mCallback.updateFromDownload(null);             cancel(true);         }     } }
  • 9.     /**      * Defines work to perform on the background thread.      */     @Override     protected DownloadTask.Result doInBackground(String... urls) {         Result result = null;         if (!isCancelled() && urls != null && urls.length > 0) {             String urlString = urls[0];             try {                 URL url = new URL(urlString);                 String resultString = downloadUrl(url);                 if (resultString != null) {                     result = new Result(resultString);                 } else {                     throw new IOException("No response received.");                 }             } catch(Exception e) {                 result = new Result(e);             }         }         return result;     }
  • 10.     /**      * Updates the DownloadCallback with the result.      */     @Override     protected void onPostExecute(Result result) {         if (result != null && mCallback != null) {             if (result.mException != null) {                 mCallback.updateFromDownload (result.mException.getMessage());             } else if (result.mResultValue != null) {                 mCallback.updateFromDownload (result.mResultValue);             }             mCallback.finishDownloading();         }     }
  • 11. Another example of AsyncTask usage private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {      protected Long doInBackground(URL... urls) {          int count = urls.length;          long totalSize = 0;          for (int i = 0; i < count; i++) {              totalSize += Downloader.downloadFile(urls[i]);              publishProgress((int) ((i / (float) count) * 100));              // Escape early if cancel() is called              if (isCancelled()) break;          }          return totalSize;      }      protected void onProgressUpdate(Integer... progress) {          setProgressPercent(progress[0]);      }      protected void onPostExecute(Long result) {          showDialog("Downloaded " + result + " bytes");      }  } Usage: new DownloadFilesTask().execute(url1, url2, url3);
  • 12. Best security practices • Minimize the amount of sensitive or personal user data that you transmit over the network. • Send all network traffic from your app over SSL. • Consider creating a network security configuration, which allows your app to trust custom CAs or restrict the set of system CAs that it trusts for secure communication. In cryptography, a certificate authority or certification authority (CA) is an entity that issues digital certificates.
  • 13. Networking Security configuration The key capabilities of this feature are as follows: • Custom trust anchors: Customize which Certificate Authorities (CA) are trusted for an app's secure connections. For example, trusting particular self-signed certificates or restricting the set of public CAs that the app trusts. • Debug-only overrides: Safely debug secure connections in an app without added risk to the installed base. • Cleartext traffic opt-out: Protect apps from accidental usage of cleartext traffic. • Certificate pinning: Restrict an app's secure connection to particular certificates.
  • 14. Adding a Security Configuration File <?xml version="1.0" encoding="utf-8"?> <manifest ... >     <application android:networkSecurityConfig= "@xml/network_security_config"                     ... >         ...     </application> </manifest> •
  • 15. Configuring a Custom CA res/xml/network_security_config.xml: <?xml version="1.0" encoding="utf-8"?> <network-security-config>     <domain-config>         <domain includeSubdomains="true">example.com</ domain>         <trust-anchors>             <certificates src="@raw/my_ca"/>         </trust-anchors>     </domain-config> </network-security-config> Add the self-signed or non-public CA certificate, in PEM or DER format, to res/raw/my_ca. You can also provide multiple <certificates> elements instead of one.
  • 16. Configuring CAs for Debugging When debugging an app that connects over HTTPS, you may want to connect to a local development server, which does not have the SSL certificate for your production server. In order to support this without any modification to your app's code, you can specify debug-only CAs, which are trusted only when android:debuggable is true, by using debug-overrides. <?xml version="1.0" encoding="utf-8"?> <network-security-config>     <debug-overrides>         <trust-anchors>             <certificates src="@raw/debug_cas"/>         </trust-anchors>     </debug-overrides> </network-security-config>
  • 17. Opting Out of Cleartext Traffic <?xml version="1.0" encoding="utf-8"?> <network-security-config>     <domain-config cleartextTrafficPermitted="false">        <domain includeSubdomains="true">secure.example.com</domain>     </domain-config> </network-security-config>
  • 18. Check a Device's Network Connection private static final String DEBUG_TAG = "NetworkStatusExample"; ... ConnectivityManager connMgr = (ConnectivityManager)         getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); boolean isWifiConn = networkInfo.isConnected(); networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); boolean isMobileConn = networkInfo.isConnected(); Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn); Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);
  • 19. Checking whether you are online public boolean isOnline() {     ConnectivityManager connMgr = (ConnectivityManager)           getSystemService(Context.CONNECTIVITY_SERVICE);     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();     return (networkInfo != null && networkInfo.isConnected()); }  
  • 20. Detect Connection Changes public class NetworkReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {     ConnectivityManager conn =  (ConnectivityManager)         context.getSystemService(Context.CONNECTIVITY_SERVICE);     NetworkInfo networkInfo = conn.getActiveNetworkInfo();     if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {         Toast.makeText(context, R.string.wifi_connected, Toast.LENGTH_SHORT).show();     } . . . }
  • 21. Use broadcast receivers carefully Setting up a BroadcastReceiver that gets called unnecessarily can be a drain on system resources. The sample application registers the BroadcastReceiver NetworkReceiver in onCreate(), and it unregisters it in onDestroy(). This is more lightweight than declaring a <receiver> in the manifest. When you declare a <receiver> in the manifest, it can wake up your app at any time, even if you haven't run it for weeks. By registering and unregistering NetworkReceiver within the main activity, you ensure that the app won't be woken up after the user leaves the app. If you do declare a <receiver> in the manifest and you know exactly where you need it, you can use setComponentEnabledSetting() to enable and disable it as appropriate.
  • 22. Use Firebase Cloud Messaging as an Alternative to Polling Every time your app polls your server to check if an update is required, you activate the wireless radio, drawing power unnecessarily, for up to 20 seconds on a typical 3G connection. Firebase Cloud Messaging (FCM) is a lightweight mechanism used to transmit data from a server to a particular app instance. Using FCM, your server can notify your app running on a particular device that there is new data available for it. Compared to polling, where your app must regularly ping the server to query for new data, this event-driven model allows your app to create a new connection only when it knows there is data to download. The model minimizes unnecessary connections and reduces latency when updating information within your app.
  • 23. Using Firebase for Networking Add Firebase to Your Android Project Prerequisites • A device running Android 4.0 (Ice Cream Sandwich) or newer, and Google Play services 10.2.1 or higher • The Google Play services SDK from the Google Repository, available in the Android SDK Manager • The latest version of Android Studio, version 1.5 or higher
  • 24. Use the Firebase Assistant If you're using the latest version of Android Studio (version 2.2 or later), we recommend using the Firebase Assistant to connect your app to Firebase. The Firebase Assistant can connect your existing project or create a new one for you and automatically install any necessary gradle dependencies. To open the Firebase Assistant in Android Studio: • Click Tools > Firebase to open the Assistant window. • Click to expand one of the listed features (for example, Analytics), then click the provided tutorial link (for example, Log an Analytics event). • Click the Connect to Firebase button to connect to Firebase and add the necessary code to your app.
  • 25. Available libs Gradle Dependency Line Service com.google.firebase:firebase-core:10.2.1 Analytics com.google.firebase:firebase-database:10.2.1 Realtime Database com.google.firebase:firebase-storage:10.2.1 Storage com.google.firebase:firebase-crash:10.2.1 Crash Reporting com.google.firebase:firebase-auth:10.2.1 Authentication com.google.firebase:firebase-messaging:10.2.1 Cloud Messaging and Notifications com.google.firebase:firebase-config:10.2.1 Remote Config com.google.firebase:firebase-invites:10.2.1 Invites and Dynamic Links com.google.firebase:firebase-ads:10.2.1 AdMob com.google.firebase:firebase-appindexing:10.2.1 App Indexing
  • 26. Firebase authentification for Android Declare the FirebaseAuth and AuthStateListener objects. private FirebaseAuth mAuth; private FirebaseAuth.AuthStateListener mAuthListener;
  • 27. In the onCreate() method, initialize the FirebaseAuth instance and the AuthStateListener method so you can track whenever the user signs in or out. mAuth = FirebaseAuth.getInstance(); mAuthListener = new FirebaseAuth.AuthStateListener() {     @Override     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {         FirebaseUser user = firebaseAuth.getCurrentUser();         if (user != null) {             // User is signed in             Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());         } else {             // User is signed out             Log.d(TAG, "onAuthStateChanged:signed_out");         }         // ...     } };
  • 28. Attach the listener to your FirebaseAuth instance in the onStart() method and remove it on onStop(). @Override public void onStart() {     super.onStart();     mAuth.addAuthStateListener(mAuthListener); } @Override public void onStop() {     super.onStop();     if (mAuthListener != null) {         mAuth.removeAuthStateListener(mAuthListener);     } }
  • 29. Sign up new users Create a new createAccount method which takes in an email address and password, validates them and then creates a new user with the createUserWithEmailAndPassword method. mAuth.createUserWithEmailAndPassword(email, password)         .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {             @Override             public void onComplete(@NonNull Task<AuthResult> task) {                 Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());                 // If sign in fails, display a message to the user. If sign in succeeds                 // the auth state listener will be notified and logic to handle the                 // signed in user can be handled in the listener.                 if (!task.isSuccessful()) {                     Toast.makeText(EmailPasswordActivity.this, R.string.auth_failed,                             Toast.LENGTH_SHORT).show();                 }                 // ...             }         }); EmailPasswordActivity.java
  • 30. Sign in existing users Create a new signIn method which takes in an email address and password, validates them, and then signs a user in with the signInWithEmailAndPassword method. mAuth.signInWithEmailAndPassword(email, password)         .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {             @Override             public void onComplete(@NonNull Task<AuthResult> task) {                 Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());                 // If sign in fails, display a message to the user. If sign in succeeds                 // the auth state listener will be notified and logic to handle the                 // signed in user can be handled in the listener.                 if (!task.isSuccessful()) {                     Log.w(TAG, "signInWithEmail:failed", task.getException());                     Toast.makeText(EmailPasswordActivity.this, R.string.auth_failed,                             Toast.LENGTH_SHORT).show();                 }                 // ...             }         });
  • 31. Access user information If a user has signed in successfully you can get their account data at any point with the getCurrentUser method. FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user != null) {     // Name, email address, and profile photo Url     String name = user.getDisplayName();     String email = user.getEmail();     Uri photoUrl = user.getPhotoUrl();     // Check if user's email is verified     boolean emailVerified = user.isEmailVerified();     // The user's ID, unique to the Firebase project. Do NOT use this value to     // authenticate with your backend server, if you have one. Use     // FirebaseUser.getToken() instead.     String uid = user.getUid(); }