You are on page 1of 11

Notes: Mobile Application Development Class: B.Sc. CS.

TY,
Unit VI: WORKING WITH INTERNET, DATABASES AND PUBLISHING APPS

UNIT VI WORKING WITH INTERNET, DATABASES AND PUBLISHING APPS

6.1 Shared preferences


6.2 Downloading and Parsing Internet Resources, Using the Download Manager.
6.3 Files access
6.4 Introducing Android Databases, Introducing SQLite, Content Values and Cursors,
Working with SQLite Databases.
6.5 Preparing for publishing
6.6 Publishing to the Android Market

6.1 SHARED PREFERENCES

Android provides many ways of storing data of an application. One of this way is called
Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of
key,value pair.

In order to use shared preferences, you have to call a method getSharedPreferences() that
returns a SharedPreference instance pointing to the file that contains the values of
preferences.
SharedPreferences sharedpreferences = getSharedPreferences(“pref1”,
Context.MODE_PRIVATE);
The first parameter is the key and the second parameter is the MODE. Apart from private
there are other modes available that are listed below −

Sr.No Mode & description

1 MODE_APPEND
This will append the new preferences with the already existing preferences

4 MODE_PRIVATE
By setting this mode, the file can only be accessed using calling application

5 MODE_WORLD_READABLE
This mode allow other application to read the preferences

6 MODE_WORLD_WRITEABLE
This mode allow other application to write the preferences

You can save something in the sharedpreferences by using SharedPreferences.Editor class.


You will call the edit method of SharedPreference instance and will receive it in an editor
object. Its syntax is −

Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 1
Notes: Mobile Application Development Class: B.Sc. CS. TY,
Unit VI: WORKING WITH INTERNET, DATABASES AND PUBLISHING APPS

Editor editor = sharedpreferences.edit();


editor.putString("key", "value");
editor.commit();

Apart from the putString method , there are methods available in the editor class that
allows manipulation of data inside shared preferences. They are listed as follows −

Sr. NO Mode & description

1 clear()
It will remove all values from the editor

2 remove(String key)
It will remove the value whose key has been passed as a parameter

3 putLong(String key, long value)


It will save a long value in a preference editor

4 putInt(String key, int value)


It will save a integer value in a preference editor

5 putFloat(String key, float value)


It will save a float value in a preference editor

To read or retrieve values from the Shared Preferences file, we need to call methods such
as getInt(), getString(), etc. by providing the key for the value which we want to get like
as shown below.

SharedPreferences pref = getSharedPreferences("pref1",Context.MODE_PRIVATE);


pref.getString("keyname",null);
pref.getInt("keyname",0);

6.2 DOWNLOADING AND PARSING INTERNET RESOURCES, USING THE DOWNLOAD


MANAGER

The download manager is a system service that handles long-running HTTP downloads.
Clients may request that a URI be downloaded to a particular destination file. The
download manager will conduct the download in the background, taking care of HTTP

Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 2
Notes: Mobile Application Development Class: B.Sc. CS. TY,
Unit VI: WORKING WITH INTERNET, DATABASES AND PUBLISHING APPS

interactions and retrying downloads after failures or across connectivity changes and
system reboots.

Apps that request downloads through this API should register a broadcast receiver
for ACTION_NOTIFICATION_CLICKED to appropriately handle when the user clicks on a
running download in a notification or from the downloads UI.

Note that the application must have the Manifest.permission.INTERNET permission to use
this class.

6.3 FILES ACCESS

In android, External Storage is useful to store the data files publically on the shared
external storage using the FileOutputStream object. After storing the data files on
external storage, we can read the data file from external storage media using
a FileInputStream object.

In android, by using getExternalStoragePublishDirectory() method we can access the


files from appropriate public directory by passing the type of directory we want, such as
DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES, etc. based on our
requirements.

Write a File to External Storage


By using android FileOutputStream object
and getExternalStoragePublicDirectory method, we can easily create and write data to
the file in external storage public folders.

Following is the code snippet to create and write a public file in the
device Downloads folder.

String FILENAME = "myfile.txt";


String name = "COCSIT Latur";

File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_


DOWNLOADS);
File myFile = new File(folder, FILENAME);
FileOutputStream fstream = new FileOutputStream(myFile);
fstream.write(name.getBytes());
fstream.close();

Read a File from External Storage


By using the android FileInputStream object
and getExternalStoragePublicDirectory method, we can easily read the file from
external storage.

Following is the code snippet to read data from a file that is in the downloads folder.

Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 3
Notes: Mobile Application Development Class: B.Sc. CS. TY,
Unit VI: WORKING WITH INTERNET, DATABASES AND PUBLISHING APPS

String FILENAME = "user_details";


File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_
DOWNLOADS);
File myFile = new File(folder, FILENAME);
FileInputStream fstream = new FileInputStream(myFile);
StringBuffer sbuffer = new StringBuffer();
int i;
while ((i = fstream.read())!= -1){
sbuffer.append((char)i);
}
fstream.close();

6.4 INTRODUCING ANDROID DATABASES, INTRODUCING SQLITE, CONTENT VALUES


AND CURSORS, WORKING WITH SQLITE DATABASES.

The database is the most common way of storing and managing data. Sometime before,
databases are handled on server-side or cloud and mobile devices only communicate with
them through the network. However, to make applications more responsive and less
dependent on network connectivity, the trend of offline usage or less dependency on the
network is gaining popularity.

Nowadays, applications keep DB locally or make a copy of DB over the cloud onto local
devices and sync with it once in a day or whenever there is network connectivity.

This will help in faster and responsive applications that are functional even when there is
no or limited internet connectivity.

Databases for mobiles need to be:

 Lightweight as storage is limited on mobile devices.


 No server requirement.
 In the form of the library with no or minimal dependency (embeddable) so that it
can be used when needed
 Fast and secure.
 Easy to handle through code, and the option to make it private or shared with
other applications.
 Low memory and power consumption.

There are lots of mobile databases coming into the market some of them are listed below

 SQLite
 Realm DB
 ORMLite
 Berkeley DB

Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 4
Notes: Mobile Application Development Class: B.Sc. CS. TY,
Unit VI: WORKING WITH INTERNET, DATABASES AND PUBLISHING APPS

Introducing SQLite

SQLite is an open source, embedded relational database. SQLite originally released in


2000, it is designed to provide a suitable way for applications to manage data without
the overhead that often comes with dedicated relational database management
systems

SQLite is a free, open source, public-domain software package that provides a RDBMS
(relational database management system). RDBMS are used to store user-defined
records in large tables. In data storage and management system, a database engine can
process complex query commands that combine data from multiple tables to generate
reports and data summary.

Working with SQLite Databases.


The main package is android.database.sqlite that contains the classes to manage your own
databases
Database - Creation
In order to create a database you just need to call this method openOrCreateDatabase with
your database name and mode as a parameter. It returns an instance of SQLite database
which you have to receive in your own object.Its syntax is given below
SQLiteDatabase mydatabase = openOrCreateDatabase("your database
name",MODE_PRIVATE,null);
Apart from this, there are other functions available in the database package that does this
job.
Database - Insertion
We can create table or insert data into table using execSQL method defined in
SQLiteDatabase class. Its syntax is given below
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS user (Username
VARCHAR,Password VARCHAR);");
mydatabase.execSQL("INSERT INTO user VALUES('admin','admin');");
Database - Fetching
We can retrieve anything from database using an object of the Cursor class. We will call a
method of this class called rawQuery and it will return a resultset with the cursor pointing
to the table. We can move the cursor forward and retrieve the data.
Cursor resultSet = mydatbase.rawQuery("Select * from user",null);
resultSet.moveToFirst();
String username = resultSet.getString(0);
String password = resultSet.getString(1);
There are other functions available in the Cursor class that allows us to effectively retrieve
the data. That includes

Sr.No Method & Description

Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 5
Notes: Mobile Application Development Class: B.Sc. CS. TY,
Unit VI: WORKING WITH INTERNET, DATABASES AND PUBLISHING APPS

1 getColumnCount()
This method return the total number of columns of the table.

2 getColumnIndex(String columnName)
This method returns the index number of a column by specifying the name of the
column

3 getColumnName(int columnIndex)
This method returns the name of the column by specifying the index of the column

4 getColumnNames()
This method returns the array of all the column names of the table.

5 getCount()
This method returns the total number of rows in the cursor

6.5 PREPARING FOR PUBLISHING

Android application publishing is a process that makes your Android applications available
to users. Infect, publishing is the last phase of the Android application development
process.

Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 6
Notes: Mobile Application Development Class: B.Sc. CS. TY,
Unit VI: WORKING WITH INTERNET, DATABASES AND PUBLISHING APPS

Google has made it relatively easy to publish your Android application so that it can be
quickly distributed to end users. The steps to publishing your Android application
generally involve the following:

1. Export your application as an APK (Android Package) file.

2. Generate your own self-signed certificate and digitally sign your application with it.

3. Deploy the signed application.

4. Use the Android Market for hosting and selling your application.

6.6 PUBLISHING TO THE ANDROID MARKET

Publishing is the general process that makes your Android applications available to users.
When you publish an Android application you perform two main tasks:

 You prepare the application for release.


During the preparation step you build a release version of your application, which users
can download and install on their Android-powered devices.
 You release the application to users.
During the release step you make public, sell, and distribute the release version of your
application to users.

Generating APK or Android App Bundle for Publishing

Important part of publishing is to export the application as an Android Package


(APK) file or android app bundle. To export an application, we need to follow the steps
below:

Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 7
Notes: Mobile Application Development Class: B.Sc. CS. TY,
Unit VI: WORKING WITH INTERNET, DATABASES AND PUBLISHING APPS

Step1: Open the application in Android Studio.

Step 2: Select Build and then click on “Generate Signed APK” from Android Studio.

Step 3: Now we would need to fill the details accordingly, and press Next.
Key store path and key store password is needed for generating APK or aab file that is
required to upload on Google play store.

Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 8
Notes: Mobile Application Development Class: B.Sc. CS. TY,
Unit VI: WORKING WITH INTERNET, DATABASES AND PUBLISHING APPS

Step 4: And after that, we will choose the Variant and version according to choice and
requirement. After that, we will press Finish.

Step 5: And then Gradle will complete the execution and generate the APK.

After this Google Play developer account need to create on


https://play.google.com/console/developers/?pli=1

Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 9
Notes: Mobile Application Development Class: B.Sc. CS. TY,
Unit VI: WORKING WITH INTERNET, DATABASES AND PUBLISHING APPS

After you've created your Google Play developer account, you can create apps and set
them up using Play Console.

Create your app

1. Open Play Console.


2. Select All apps > Create app.
3. Select a default language and add the name of your app as you want it to appear
on Google Play. You can change this later.
4. Specify whether your application is an app or a game. You can change this later.
5. Specify whether your application is free or paid.
6. Add an email address that Play Store users can use to contact you about this
application.
7. In the "Declarations" section:
8. Acknowledge the “Developer Program Policies” and “US export laws” declarations.
9. Accept the Play App Signing Terms of Service.

Set up your app

After you create your app, you can start setting it up. Your app’s dashboard will guide
you through all the most important steps to get your app available on Google Play.

Manage your app and app bundles

Google Play uses Android App Bundles to generate and deliver APKs that are optimized
for each device configuration, providing users with more efficient apps. This means you
only need to build, sign, and upload a single app bundle to support optimized APKs for a
wide variety of device configurations. Google Play then manages and serves your app's
distribution APKs for you.

Package names for app files are unique and permanent, so please name them carefully.
Package names can't be deleted or re-used in the future.

Scan the QrCode for Video Tutorials

Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 10
Notes: Mobile Application Development Class: B.Sc. CS. TY,
Unit VI: WORKING WITH INTERNET, DATABASES AND PUBLISHING APPS

Question Sets:
For Three (3) Marks

1. Write a note on Mobile Programming


2. What are the android development tools
3. What are steps for installing android studio
4. Write the steps for creating projects
5. What is activity
6. What is Intents
7. What is toast notifications
8. What is shared preferences
9. What is SQLite
10. Write the syntax of creating TextView
11. What is the use of Alert Dialog
12. Write the Attributes of ImageButton

For Five (5) Marks


1. Write a note on Open Platform for Mobile Development
2. Explain the different Application Development Framework
3. Explain Different Operating system used on different mobile device.
4. Write a note on Android Operating system Its Features and versions
5. What is android stack
6. Explain android application structure
7. Explain configuring the Android Manifest file
8. What is Activity? Explain in detail.
9. Explain the Components or Layout of Screen
10. Explain Activity Life Cycle
11. What is Intent? Explain with Example
12. Explain Linking Activity using Intents
13. What is Fragments? Explain Fragment life cycle.
14. Write a note on Text Controls
15. Explain RadioButton and RadioGroup Views
16. Write a note on AutoCompleteTextView
17. Explain Analog and Digital Clock Views
18. Explain WebView with Example.
19. What is Localization? Explain in Detail.
20. Explain the procedure for creating and using Option Menu
21. Explain the procedure for creating and using Context Menu
22. Write a Note on SMS Messaging
23. Explain Creating and using ListView
24. Write a note on Displaying Maps
25. What is Shared Preferences? Explain Creating and Using Shared Preferences
26. Write a note on File Access in Android
27. Explain the procedure for creating and using SQLite Database in android
28. Write a note on Publishing App in android Market-Google Play Store

Prepared By: Mr. D. R. Somwanshi (M.Sc.CS, M.Phil, CS) COCSIT Latur Page 11

You might also like