Tag Archives: Android

Dynamic and Static Broadcast Recievers

As usual broadcast receivers can be configured in the manifest fileAndroidManifest.xml. A BroadcastReceiver that is configured in this way is called statically registered.

You can register your receiver in the manifest file by using the <receiver>element:

<receiver
   android:name=".ConnectivityChangeReceiver">
   <intent-filter>
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
   </intent-filter>
</receiver>

The nested element is used to specify the event the receiver should react to.

Dyanmic Broadcast Recievers

As an alternative you can register your BroadcastReceiver implementation dynamically in your code. You just need to call the registerReceiver() method on your Context object.

The registerReceiver() method takes two parameters:

The arguments of the registerReceiver() method
Argument Meaning
receiver The BroadcastReceiver you want to register
filter The IntentFilter object that specifies which event your receiver should listen to.

When you register your receiver in this way, it lives for as long as the component livesand Android sends events to this receiver until the creating component itself gets destroyed.

It’s your task to handle the lifecycle correctly. Thus when you add a receiver dynamically, take care to unregister the same receiver in the onPause() method of your Activity!

I suggest to register the receiver in the onResume() method of your Activity and to unregister it in your onPause() method:

@Override
protected void onPause() {
   unregisterReceiver(mReceiver);
   super.onPause();
}

@Override
protected void onResume() {
   this.mReceiver = new ConnectivityChangeReceiver();
   registerReceiver(
         this.mReceiver, 
         new IntentFilter(
               ConnectivityManager.CONNECTIVITY_ACTION));
   super.onResume();
}

When to use which method to register

Which method to use for registering your BroadcastReceiver depends on what your app does with the system event. I think there are basically two reasons why your app wants to know about system-wide events:

  • Your app offers some kind of service around these events
  • Your app wants to react graciously to state changes

Examples for the first category are apps that need to work as soon as the device is booted or that must start some kind of work whenever an app is installed. Battery Widget Pro or App2SD are good examples for these kinds of apps. For this type you must register the BroadcastReceiver in the Manifest file.

Examples for the second category are events that signal a change to circumstances your app might rely on. Say your app depends on an established Bluetooth connection. You have to react to a state change – but only when your app is active. In this case there is no need for a statically registered broadcast receiver. A dynamically registered one would be more reasonable.

There are also a few events that you are not even allowed to statically register for. An example for this is the Intent.ACTION_TIME_TICK event which is broadcast every minute. Which is a wise decision because a static receiver would unnecessarily drain the battery.

 

9-Patch image and its Use.

ninepatch_raw

NinePatch: defines one stretchable area with the left and top lines and the drawable area with the bottom and right lines. In the top image, the dotted grey lines identify the regions of the image that will be replicated in order to stretch the image. The pink rectangle in the bottom image identifies the region in which the contents of the View are allowed. If the contents don’t fit in this region, then the image will be stretched so that they do.

Usage:  NinePatchDrawable graphic is a stretchable bitmap image, which Android will automatically resize to accommodate the contents of the View in which you have placed it as the background. An example use of a NinePatch is the backgrounds used by standard Android buttons — buttons must stretch to accommodate strings of various lengths. A NinePatch drawable is a standard PNG image that includes an extra 1-pixel-wide border. It must be saved with the extension .9.png, and saved into the res/drawable/ directory of your project.

What is Android OS

Android is an operating system based on the Linux kernel, and designed primarily for touchscreen mobile devices such as smartphones and tablet computers. Initially developed by Android, Inc., which Google backed financially and later bought in 2005,[12] Android was unveiled in 2007 along with the founding of the Open Handset Alliance—​a consortium of hardware, software, and telecommunication companies devoted to advancing open standards for mobile devices.[13]The first publicly available smartphone running Android, the HTC Dream, was released on October 22, 2008.[14]

The user interface of Android is based on direct manipulation, using touch inputs that loosely correspond to real-world actions, like swiping, tapping, pinching, and reverse pinching to manipulate on-screen objects. Internal hardware—​such as accelerometersgyroscopes, and proximity sensors—​is used by some applications to respond to additional user actions, for example adjusting the screen from portrait to landscape depending on how the device is oriented. Android allows users to customize their home screens with shortcuts to applications and widgets, which allow users to display live content, such as emails and weather information, directly on the home screen. Applications can further send notifications to the user to inform them of relevant information, such as new emails and text messages. Despite being primarily designed for phones and tablets, it also has been used in televisions, games consolesdigital cameras, and other electronics.

Android is the most popular mobile OS and as of 2013, its devices also sell more than Windows, iOS and Mac OS devices combined.[15][16][17] In the third quarter of 2013, Android’s share of the global smartphone shipment market was 81.3%, the highest ever.[18] As of July 2013 the Google Play store has had over 1 million Android apps published, and over 50 billion apps downloaded.[19] A developer survey conducted in April–May 2013 found that Android is used by 71% of mobile developers.[20] The operating system’s success has made it a target for patent litigation as part of the so-called “smartphone wars” between technology companies.[21][22] As of September 2013, one billion Android devices have been activated.[23]

Android’s source code is released by Google under open source licenses, although most Android devices ultimately ship with a combination of open source and proprietary software.[3] Android is popular with technology companies which require a ready-made, low-cost and customizable operating system for high-tech devices.[24] Android’s open nature has encouraged a large community of developers and enthusiasts to use the open-source code as a foundation for community-driven projects, which add new features for advanced users[25] or bring Android to devices which were officially released running other operating systems.