Date post: | 18-Jan-2017 |
Category: |
Education |
Upload: | skumartarget |
View: | 241 times |
Download: | 0 times |
BROADCAST RECEIVER
CONFIDENTIAL INFORMATIONThis document is confidential and proprietary information of Target Soft Systems. Confidential Information includes, but is not limited to, the following: Corporate, Employee and Infrastructure Information about Target Soft Systems.
Target Soft Systems implementation , Training methodology, cost, project management and quality processes. Any disclosure of Confidential Information to, or use of it by a third party (i.e., a party other than authorised , will be damaging to Target Soft Systems). Ownership of all Confidential Information, no matter in what media it resides, remains with Target Soft Systems( TSS ). Confidential Information in this document shall not be disclosed outside the buyer’s proposal evaluators and shall not be duplicated, used, or disclosed – in whole or in part – for any purpose other than to evaluate this proposal without specific written permission of an authorized representative of Target Soft Systems.
Broadcast Receiver
• Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself.
• These messages are sometime called events or intents.
• For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.
• There are following two important steps to make BroadcastReceiver works for the system broadcasted intents. < Creating the Broadcast Receiver.
< Registering Broadcast Receiver
Creating the Broadcast Receiver:
A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter.
public class MyReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent
intent) { Toast.makeText(context, "Intent Detected.",
Toast.LENGTH_LONG).show(); }
}
Registering Broadcast Receiver:An application listens for specific broadcast intents by registering a broadcast receiver inAndroidManifest.xml file.
•Consider we are going to register MyReceiver for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process.
<receiver android:name="MyReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"> </action> </intent-filter> </receiver>
• An Intent-based publish-subscribe mechanism.
• Great for listening system events such as SMS messages.
Android System
Broadcast
Receiver
1.Register for
Broadcast Intent
2.OnReceive()
STORAGE FEATURE
Internal Storage
Internal Storage• Android provides many kinds of storage for applications to
store their data. • These storage places are shared preferences , internal and
external storage , SQLite storage , and storage via network connection.
• Internal storage is the storage of the private data on the device memory.
• By default these files are private and are accessed by only your application and get deleted , when user delete your application.
• Internal Storage are private to your application and other applications cannot access them(nor can the user). When the user uninstalls your application, these files are removed.
How to Use Internal Storage?• To create and write a private file to the internal
storage:> Call openFileOutput() with the name of the file and the
operating mode. This returns a FileOutputStream object> Write to the file with write().> Close the stream with close().• To read a file from internal storage> Call openFileInput() and pass it the name of the file to read.
This returns a FileInputStream.> Read bytes from the file with read().> Then close the stream with close().
Other Useful Methods• getFilesDir()> Gets the absolute path to the filesystem
directory where your internal files are saved.• getDir()> Creates (or opens an existing) directory within
your internal storage space.• deleteFile()> Deletes a file saved on the internal storage.• fileList()> Returns an array of files currently saved by your
application.
External Storage
External Storage• Store public data on storage media, like SD cards.• Like Internal storage, we are able to save or read from the device external
storage such as sdcard.
• FileInputStream and FileOutputStream classes are used to read and write data into the file.
• External storage such as SD card can also store application data, there’s no security enforced upon files you save to the external storage.
• All applications can read and write files placed on the external storage and the user can remove them.
• Files saved to the external storage are world- readable and can be modified by the user when they enable USB mass storage to transfer files on a computer.
Checking Media Availability• Before you do any work with the external storage,
you should always call getExternalStorageState() to check the state of the media
> Mounted> Missing> Read-only> Some other state
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
• FileOutputStream fOut = openFileOutput("file name here",MODE_WORLD_READABLE);
• The method openFileOutput() returns an instance of FileOutputStream. So you recieve it in the object of FileInputStream. After that you can call write method to write data on the file.
• String str = "data"; fOut.write(str.getBytes()); fOut.close();
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.
• FileInputStream fin = openFileInput(file);
• After that, you can callr 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();
Internal Vs. External Storage
• Internal storage is the storage of the private data on the device memory
• External storage is not private and may not always be available. If for example the android device is connected with a computer, the computer may mount the external system via USB and that makes this external storage not available for android applications.
BLUETOOTH
Bluetooth
Bluetooth is a way to exchange data with other devices wirelessly. Android provides Bluetooth API to perform several tasks such as:•scan bluetooth devices•connect and transfer data from and to other devices•manage multiple connections etc.
By the help of BluetoothAdapter class, we can perform fundamental tasks such as initiate device discovery, query a list of paired (bonded) devices, create a BluetoothServerSocket instance to listen for connection requests etc.
Constants of Bluetooth Adapter classBluetooth Adapter class provides many
constants. Some of them are as follows:String ACTION_REQUEST_ENABLEString ACTION_REQUEST_DISCOVERABLEString ACTION_DISCOVERY_STARTEDString ACTION_DISCOVERY_FINISHED
Methods of Bluetooth Adapter class
Commonly used methods of Bluetooth Adapter class are as follows:
static synchronized BluetoothAdapter getDefaultAdapter() returns the instance of BluetoothAdapter.boolean enable() enables the bluetooth adapter if it is disabled.boolean isEnabled() returns true if the bluetooth adapter is enabled.
boolean disable() disables the bluetooth adapter if it is enabled.String getName() returns the name of the bluetooth adapter.boolean setName(String name) changes the bluetooth name.int getState() returns the current state of the local bluetooth adapter.
Set<BluetoothDevice> getBondedDevices() returns a set of paired (bonded) BluetoothDevice objects.boolean startDiscovery() starts the discovery process.
• Android provides BluetoothAdapter class to communicate with Bluetooth. Create an object of this calling by calling the static method getDefaultAdapter(). Its syntax is given below.
private BluetoothAdapter BA; BA = BluetoothAdapter.getDefaultAdapter();
• In order to enable the Bluetooth of your device, call the intent with the following Bluetooth constant ACTION_REQUEST_ENABLE. Its syntax is.
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Now select Turn On to turn on the bluetooth
Now just select the Get Visible button to turn on your visibiltiy
Select your mobile device as an option and then check your mobile device which will display following screen:
Now just select the List Devices option. It will list down the paired devices in the list view
Now just select the Turn off button to switch off the Bluetooth
THANK YOU