0% found this document useful (0 votes)
29 views30 pages

Unit Iv

Uploaded by

aloneabhi1919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views30 pages

Unit Iv

Uploaded by

aloneabhi1919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

UNIT-IV

Android Data & Android APIs


Storage options or API’s in Android

• Android provides several options for you to save


persistent application data.
• The solution you choose depends on your
specific needs, such as whether the data should be
private to your application or accessible to other
applications (and the user) and how much space
your data requires.
data storage options :
a)SQLite Databases
Store structured data in a private database.

b) Shared Preferences
Store private primitive data in key-value pairs.

b)Internal Storage
Store private data on the device memory.

c) External Storage
Store public data on the shared external storage.

d) Network Connection
Store data on the web with your own network server.
Introduction to SQLite
SQLite
• SQLite is an Open Source Database which is
embedded into Android.
• SQLite supports standard relational database
features like SQL syntax, transactions and
prepared statements.
• In addition it requires only little memory at
runtime approx 250kb.
• SQLite supports the data types TEXT, INTEGER,
REAL.
• All other types must be converted into one of
these fields before saving them in the database.
• SQLiteDatabase is available on every Android
device .
• Using an SQLiteDatabase in Android does not
require any database setup or administration.
SQLiteOpenHelper
• To create and upgrade a database in your Android
application we will use a class called
SQLiteOpenHelper.
• In the constructor of your subclass you call
the super() method ofSQLiteOpenHelper,
specifying the database name and the current
database version.
In this class you need to override
the onCreate() and onUpgrade() methods.

•onCreate() is called by the framework, if the


database does not exists.

•onUpgrade() is called, if the database version is


increased in your application code. This method
allows you to update the database schema.
SQLiteOpenHelper provides the methods:

a) getReadableDatabase(): For reading operations

b) getWriteableDatabase():For writing operations


SQLiteDatabase

SQLiteDatabase is the base class for working


with a SQLite database in Android and provides
methods to open, query, update and close the
database.
Methods in SQLiteDatabase class
• insert():For insertion operation

• update() : For update operations

• delete() : For delete operation

• execSQL(): Which allows to execute an SQL


statement directly.
Shared Preferences
• If you don't need to store a lot of data and it doesn't
require structure, you should use
SharedPreferences.
• The SharedPreferences APIs allow you to read
and write persistent key-value pairs of primitive
data types: booleans, floats, ints, longs, and strings.
• The key-value pairs are written to XML files that
persist across user sessions, even if your app is
killed.
• You can manually specify a name for the file or
use per-activity files to save your data.
To get a SharedPreferences object for your
application, use one of two methods:

• getSharedPreferences() :Use this if you need


multiple preferences files identified by name,
which you specify with the first parameter.
• getPreferences() :Use this if you need only
one preferences file for your Activity. Because
this will be the only preferences file for your
Activity, you don't supply a name
To write values:

• Call edit() to get a SharedPreferences.Editor.


• Add values with methods such as putBoolean
() and putString().
• Commit the new values with commit()
• Example:

public class MainActivity extends AppCompatActivity {


Button bg;
ConstraintLayout constraintLayout;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bg=findViewById(R.id.bg);
constraintLayout=findViewById(R.id.cl);
sharedPreferences=getSharedPreferences("bg",MODE_PRIVATE);
int myvalue=sharedPreferences.getInt("mykey",0);
if(myvalue==1)
{
constraintLayout.setBackgroundColor(Color.RED);
}

bg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor=sharedPreferences.edit();
editor.putInt("mykey",1);
constraintLayout.setBackgroundColor(Color.RED);
editor.apply();
editor.commit();

}
});
}
Internal
and
External Storages
Android uses a file system that's similar to disk-based
file systems on other platforms.

A File object works well for reading or writing large


amounts of data in start-to-finish order without skipping
around.

For example, it's good for image files or anything


exchanged over a network.
To view files on a device, you can log the file location
provided by methods such as File.getAbsolutePath(), and
then browse the device files with Android Studio's
Device File Explorer
Choose internal or external storage

•All Android devices have two file storage areas:


"internal" and "external" storage.
Internal storage:

• It's always available.


• Files saved here are accessible by only your app.
• When the user uninstalls your app, the system
removes all your app's files from internal storage.

Internal storage is best when you want to be sure that


neither the user nor other apps can access your files.
External storage:
• It's not always available, because the user can
mount the external storage as USB storage and in
some cases remove it from the device.
• It's world-readable, so files saved here may be read
outside of your control.
• When the user uninstalls your app, the system
removes your app's files from here only if you save
them in the directory from getExternalFilesDir().
External storage is the best place for files that
don't require access restrictions and for files that
you want to share with other apps or allow the
user to access with a computer
Request external storage permissions
To write to the public external storage, you must request the
WRITE_EXTERNAL_STORAGE permission in your manifest
file

<manifest ...>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" /
>
...
</manifest>
If your app only needs to read the external
storage (but not write to it), then you need to
declare the READ_EXTERNAL_STORAGE
permission.

<manifest ...>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
...
</manifest>
Telephony APIs
• The android.telephony.TelephonyManager class
provides information about the telephony services
such as subscriber id, sim serial number, phone
network type etc.
• Moreover, you can determine the phone state etc
• Telephony APIs Provides to access the
information about the telephony services on the
device.
• Applications can use the methods in this class
to determine telephony services and states, as
well as to access some types of subscriber
information.
• Applications can also register a listener to
receive notification of telephony state changes.
To call an API for a specific subscription, use
createForSubscriptionId(int)

telephonyManager =
defaultSubTelephonyManager.createForSubscriptionId(subId)
public class MainActivity extends AppCompatActivity
{

TextView textView;
String details="";
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.tv);
TelephonyManager telephonyManager=(TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
details+=telephonyManager.getDeviceId();
details+=telephonyManager.getImei();
details+=telephonyManager.getDeviceSoftwareVersion();
details+=telephonyManager.getSimOperator();
details+=telephonyManager.getNetworkOperator();
details+=telephonyManager.getSubscriberId();
textView.setText(""+details);

You might also like