3-4 Android
3-4 Android
We can play and control the audio files in android by the help of MediaPlayer
class.
MediaPlayer class we can easily fetch, decode and play both audio and video
files with minimal setup.
The android media framework provides built-in support for playing a variety of
common media types, such as audio or video. We have multiple ways to play
audio or video but the most important component of media framework is
MediaPlayer class.
MediaPlayer class we can access audio or video files from application (raw)
resources, standalone files in file system or from a data stream arriving over a
network connection and play audio or video files with the multiple playback
options such as play, pause, forward, backward, etc.
MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.baitikochi_chuste);
mPlayer.start();
The second parameter in create() method is the name of the song that we want
to play from our application resource directory (res/raw). create a new raw
folder under res directory and add a properly encoded and formatted media
files in it.
1. public void setDataSource(String path)- sets the data source (file path
or http url) to use.
2. public void prepare()-prepares the player for playback synchronously.
3. public void start()-it starts or resumes the playback.
4. public void stop()-it stops the playback.
5. public void pause()-it pauses the playback.
6. public void seekTo(int millis)- seeks to specified time in miliseconds.
7. public int getDuration()-returns duration of the file.
Fade In animation
public ScaleAnimation(
float fromX, float toX,
float fromY, float toY,
int pivotXType, float pivotXValue,
int pivotYType, float pivotYValue)
Let us understand these parameters. Description of these parameters as given in Android SDK
documentation:
The End
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(MyPREFERENCES, Context.MODE_PRIVATE);
MODE_APPEND
This will append the new preferences with the already existing preferences
MODE_PRIVATE
It is a default mode. MODE_PRIVATE means that when any preference file is
created with private mode then it will not be accessible outside of your
application. This is the most common mode which is used.
MODE_WORLD_READABLE
If developer creates a shared preference file using mode world readable then it
can be read by anyone who knows it’s name, so any other outside application
can easily read data of your app. This mode is very rarely used in App.
MODE_WORLD_WRITEABLE
It’s similar to mode world readable but with both kind of accesses i.e read and
write. This mode is never used in App by Developer. 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.
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. clear()
It will remove all values from the editor
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n); editor.putString(Phone, ph);
editor.putString(Email, e); editor.commit();
Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_LONG).show();
Writing file
In order to use internal storage to write some data in the file, call the
openFileOutput() method with the name of the file and the mode. The mode
could be private , public e.t.c.
Reading file
In order to read from the file you just created , call the openFileInput() method
with the name of the file. It returns an instance of FileInputStream.
After that, you can call read method to read one character at a time from the file
and then you can print it.
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
//string temp contains all the data of the file.
fin.close();
Apart from the the methods of write and close, there are other methods
provided by the FileOutputStream class for better writing files.
External Storage
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.
The data files saved in external storage are word-readable and can be modified
by the user when they enable USB mass storage to transfer files on a computer.
To read or write files on the external storage, our app must acquire the
WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system
permissions. For that, we need to add the following permissions in the android
manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
we are creating and writing a file in device public Downloads folder by using
getExternalStoragePublicDirectory method. We used write() method to
write the data in file and used close() method to close the stream. Read a
File from External Storage
By using the android FileInputStream object and
getExternalStoragePublicDirectory method, we can easily read the file from
external storage.
SQLiteOpenHelper class
The android.database.sqlite.SQLiteOpenHelper class is used for database
creation and version management. For performing any database operation, you
have to provide the implementation of onCreate() and onUpgrade() methods
of SQLiteOpenHelper class.
@Override
public void onCreate(SQLiteDatabase db) { db.execSQL("create table " +
TABLE_NAME +" (ID INTEGER PRIMARY KEY
AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); onCreate(db);
}
public boolean insertData(String name,String surname,String marks) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks); long result =
db.insert(TABLE_NAME,null ,contentValues); if(result == -
1) return false; else return true;
}
Typically you work with content providers in one of two scenarios: implementing
code to access an existing content provider in another application or creating a new
content provider in your application to share data with other applications.
This page covers the basics of working with existing content providers. To learn
about implementing content providers in your own applications, see Create a
content provider.
• The API you use to insert, update, or delete data in a content provider.
Content URI
Following are the steps which are essential to follow in order to create a
Content Provider:
1. Create a class in the same directory where the that MainActivity file
resides and this class must extend the ContentProvider base class.
Remote databases
A remote database means that you can access the data from this database in
a remote location. A lot of applications are used to send the data to the
remote database module. I will show you how to send the data to the remote
database in an Android Application, using Android Studio.
Web application, however, is intended for interaction between the user and
the application. To explain it more clearly, let’s take a social media platform
like Instagram. What will you usually do on Instagram? You scroll through
various posts and reels, send messages to your friends, and share your day.
These activities are the interactions from your side with the web application.
This usually is not possible on websites.
There are a Examples of web applications we are not aware of and still are
using every day. Some of them are:
Pros:
• Web applications are flexible. They can be accessed via any browser on
mobile and desktop.
• Web applications need not need to be updated manually as the web
application updates on its own.
• They don’t require to be installed on mobile; thus, the memory and
data are also saved.
• The applications are cross-platform and can be run on any OS.
Cons:
• Web applications are accessed via browsers; hence they rely on the
internet and cannot be accessed offline.
• If any website of the web application experiences even a slight error,
the whole application will likely experience performance lag.
• Web applications run at a relatively slower speed.
• Web applications are highly likely to experience security breaches.
3. key: A JSONObject contains a key that is in string format. A pair of key and
value creates a JSONObject.
4. Value: Each key has a value that could be primitive datatype(integer, float,
String etc).