Tampilkan postingan dengan label Broadcast. Tampilkan semua postingan
Tampilkan postingan dengan label Broadcast. Tampilkan semua postingan

Android Application Fundamentals or Components

Posted by Unknown Senin, 06 Mei 2013 0 komentar

See more basics on Android along with interview questions

Android is written in java programming language.Android application are composed of some of the main components such as:
  • Activities
  • Services
  • Content providers
  • Broadcast receivers
Intents, Resources(such as strings and graphics), Notification signalings(light,sound,dialog), are also part of the components.

All these components play different role in the overall application and each one can be activated individually.

Activities :
          An activity represents a single screen with a user interface.Suppose if there is a listview and if clicking on each row it traverse to another page ,then the screen showing listview is an activity and the next page is another activity,where each one is independent of the others.
An activity is implemented as a subclass of Activity ,that why we use

see activity life cycle here :

public class MainActivity extends Activity {
}
Services :
          A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface.For example : Running an alarm in background
A service is implemented as a subclass of Service.

Content providers :
          They manages a shared set of application data.Data can be stored in SQLite Database,File systems,web or any other medium where application can access.
Content Providers allows to modify and use these stored data.For example : storing users contacts details,where proper permission one can get the contacts.
A content provider is implemented as a subclass of ContentProvider.

Broadcast receivers :
          They responds to systems  broadcast announcements.Some of the native Broadcast are volume control, light, switch on and off, screen on and off,bluetooth etc.A broadcast receiver is just a "gateway" to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event.
A broadcast receiver is implemented as a subclass of BroadcastReceiver.

Who Activates A component ?
         The above components are activated by a message called Intents.
Intents bind individual components to each other at runtime, whether the component belongs to your application or another.
An intent is created with an Intent object, which defines a message to activate either a specific component or a specific type of component—an intent can be either explicit or implicit, respectively.

What is a Content Resolver ?
         The Content Provider, is not activated by intents. It is activated when there is request from ContentResolver. These Resolvers handles direct transaction with the providers so they donot need further any method calls. This leaves a layer of abstraction between the content provider and the component requesting information (for security).

What is an AndroidManifest file ?
          This is a root directory of Android application where all the components are declared.Other than this they also have
  • user permissions the application requires, such as read and write external storage
  • Declare the minimum API level
  • Declare hardware and software features such as bluetooth or a printer
  • API libraries etc...
 




Baca Selengkapnya ....

BroadcastReceiver for Screen On/Off in ANDROID

Posted by Unknown Senin, 22 April 2013 0 komentar
If wanted to call a broadcast for screen on/off you can get the code sniffet from here.
Here I have used dynamic calling of Broadcast and the java source code is given below.

get more details about broadcast from :
in java file:
 
public class MainActivity extends Activity {
//Create broadcast object
BroadcastReceiver mybroadcast = new BroadcastReceiver() {

//When Event is published, onReceive method is called
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("[BroadcastReceiver]", "MyReceiver");

if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
Log.i("[BroadcastReceiver]", "Screen ON");
}
else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
Log.i("[BroadcastReceiver]", "Screen OFF");
}
}};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
}

Baca Selengkapnya ....

BroadcastReceiver to get Battery Level - simple Example using dynamic calling of BroadCast in ANDROID

Posted by Unknown Senin, 08 April 2013 0 komentar
See Introduction for Broadcast Receiver here:

 When you need to call a broadcast (suppose here battery value) you need to register the broadcast and when the event is published it will call onReceive method which have context and particular intent as parameter.

 
public class Broadcast extends Activity {
//Create broadcast object
BroadcastReceiver mybroadcast = new BroadcastReceiver() {

//When Event is published, onReceive method is called
@Override
public void onReceive(Context context, Intent intent) {
//Get battery percentage
int batterylevel = intent.getIntExtra("level", 0);
//get progressbar
ProgressBar myprogressbar = (ProgressBar) findViewById(R.id.progressbar);
myprogressbar.setProgress(batterylevel);
TextView tv = (TextView) findViewById(R.id.textfield);
//Set TextView with text
tv.setText("Battery Level: " + Integer.toString(batterylevel) + "%");
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broadcast);
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
}
The arguments of the registerReceiver() method
receiver :: The BroadcastReceiver you want to register
filter :: The IntentFilter object that specifies which event your receiver should listen to.

To learn more on event and usages click here...

Baca Selengkapnya ....
Trik SEO Terbaru support Online Shop Baju Wanita - Original design by Bamz | Copyright of android populer.