1.1. Service
A service is a component which runs in the background, without interacting with the user. The Android platform provides a lot of pre-defined services, usually exposed via a Manager class. In your activity you access services via the method getSystemService(). Own Services must be declared via the "AndroidManifest.xml". They run the main thread of their hosting process. Therefore you should run performance intensive tasks in the background .
public class MyPhoneReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); if (extras != null) { String state = extras.getString(TelephonyManager.EXTRA_STATE); Log.w("DEBUG", state); if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { String phoneNumber = extras .getString(TelephonyManager.EXTRA_INCOMING_NUMBER); Log.w("DEBUG", phoneNumber); } }
} }
If you install your application and receive a call, e.g simulated by the DDMS perspective in Eclipse, then your receiver will be called and lot a message to the console.
Create the following broadcast receiver class. This class will get the Vibrator service.
package de.vogella.android.alarm; import import import import import android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.os.Vibrator; android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Don't panik but your time is up!!!!.", Toast.LENGTH_LONG).show(); // Vibrate the mobile phone Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(2000); } }
Maintain this class as broadcast receiver in "AndroidManifest.xml" and allow the vibrate authorization.
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.vogella.android.alarm" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="9" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".AlarmActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="MyBroadcastReceiver"></receiver> </application> <uses-permission android:name="android.permission.VIBRATE"></uses-permission> </manifest>
Change the code of your Activity "AlarmActivity" to the following. This activity will create an Intent for the Broadcast receiver and get the AlarmManager service.
package de.vogella.android.alarm; import import import import import import import import android.app.Activity; android.app.AlarmManager; android.app.PendingIntent; android.content.Intent; android.os.Bundle; android.view.View; android.widget.EditText; android.widget.Toast;
public class AlarmActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void startAlert(View view) { EditText text = (EditText) findViewById(R.id.time); int i = Integer.parseInt(text.getText().toString()); Intent intent = new Intent(this, MyBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( this.getApplicationContext(), 234324243, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent); Toast.makeText(this, "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show(); } }
Run your application on the device. Set your time and start the alarm. After the defined number of seconds a Toast should be displayed. The vibrator alarm does not work on the simulator.
import android.os.Binder; import android.os.IBinder; import android.util.Log; public class WordService extends Service { private Timer timer = new Timer(); private static final long UPDATE_INTERVAL = 5000; private final IBinder mBinder = new MyBinder(); private ArrayList<String> list = new ArrayList<String>(); private String[] fixedList = { "Linux", "Android", "iPhone", "vogella.de", "helpful", "stuff" }; private int index = 0; public void onCreate() { super.onCreate(); pollForUpdates(); } private void pollForUpdates() { timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { // Imagine here a freaking cool web access ;-) if (list.size() >= 6) { list.remove(0); } list.add(fixedList[index++]); if (index >= fixedList.length) { index = 0; } } }, 0, UPDATE_INTERVAL); Log.i(getClass().getSimpleName(), "Timer started."); } @Override public void onDestroy() { super.onDestroy(); if (timer != null) { timer.cancel(); } Log.i(getClass().getSimpleName(), "Timer stopped."); } // We return the binder class upon a call of bindService @Override public IBinder onBind(Intent arg0) { return mBinder; } public class MyBinder extends Binder { WordService getService() { return WordService.this; } } public List<String> getWordList() { return list; }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.vogella.android.ownservice" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ServiceConsumer" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="WordService"></service> </application> </manifest>
import android.widget.Toast; public class ServiceConsumer extends Activity { private WordService s; private ArrayList<String> values; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); doBindService(); values = new ArrayList<String>(); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values); ListView list = (ListView) findViewById(R.id.list); list.setAdapter(adapter); // List<String> wordList = s.getWordList(); // Toast.makeText(this, wordList.get(0), Toast.LENGTH_LONG).show(); } private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { s = ((WordService.MyBinder) binder).getService(); Toast.makeText(ServiceConsumer.this, "Connected", Toast.LENGTH_SHORT).show(); } public void onServiceDisconnected(ComponentName className) { s = null; } }; private ArrayAdapter<String> adapter; void doBindService() { bindService(new Intent(this, WordService.class), mConnection, Context.BIND_AUTO_CREATE); } public void showServiceData(View view) { if (s != null) { List<String> wordList = s.getWordList(); values.clear(); values.addAll(wordList); adapter.notifyDataSetChanged(); } } }
Services can be started when a client calls the Context.startService(Intent) method. If the service isn't already running, Android starts it and calls its onCreate method followed by the onStart method. If the service is already running, its onStart method is invoked again with the new intent. So it's quite possible and normal for a service's onStart method to be called repeatedly in a single run of the service. onResume, onPause, and onStop are not needed Recall that a service generally has no user interface, so there isn't any need for the onPause, onResume, or onStop methods. Whenever a service is running, it is always in the background. onBind If a client needs a persistent connection to a service, it can call the Context.bindService method. This creates the service if it is not running, and calls onCreate but not onStart. Instead, the onBind method is called with the client's intent, and it returns an IBind object that the client can use to make further calls to the service. It's quite normal for a service to have clients starting it and clients bound to it at the same time. onDestroy As with an activity, the onDestroy method is called when the service is about to be terminated. Android will terminate a service when there are no more clients starting or bound to it. As with activities, Android may also terminate a service when memory is getting low. If that happens, Android will attempt to restart the service when the memory pressure passes, so if your service needs to store persistent information for that restart, it's best to do so in the onStart method.
Your activity monitors and reacts to these events by instantiating methods that override the Activity class methods for each event: onCreate Called when your activity is first created. This is the place you normally create your views, open any persistent datafiles your activity needs to use, and in general initialize your activity. When calling onCreate, the Android framework is passed a Bundle object that contains any activity state saved from when the activity ran before. onStart Called just before your activity becomes visible on the screen. Once onStart completes, if your activity can become the foreground activity on the screen, control will transfer to onResume. If the activity
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
cannot become the foreground activity for some reason, control transfers to the onStop method. onResume Called right after onStart if your activity is the foreground activity on the screen. At this point your activity is running and interacting with the user. You are receiving keyboard and touch inputs, and the screen is displaying your user interface. onResume is also called if your activity loses the foreground to another activity, and that activity eventually exits, popping your activity back to the foreground. This is where your activity would start (or resume) doing things that are needed to update the user interface (receiving location updates or running an animation, for example). onPause Called when Android is just about to resume a different activity, giving that activity the foreground. At this point your activity will no longer have access to the screen, so you should stop doing things that consume battery and CPU cycles unnecessarily. If you are running an animation, no one is going to be able to see it, so you might as well suspend it until you get the screen back. Your activity needs to take advantage of this method to store any state that you will need in case your activity gains the foreground againand it is not guaranteed that your activity will resume. If the mobile device you are running on runs out of memory, there is no virtual memory on disk to use for expansion, so your activity may have to make way for a system process that needs memory. Once you exit this method, Android may kill your activity at any time without returning control to you. onStop Called when your activity is no longer visible, either because another activity has taken the foreground or because your activity is being destroyed. onDestroy The last chance for your activity to do any processing before it is destroyed. Normally you'd get to this point because the activity is done and the framework called its finish method. But as mentioned earlier, the method might be called because Android has decided it needs the resources your activity is consuming. It is important to take advantage of these methods to provide the best user experience possible. This is the first place in this book we've discussed how programming for mobile devices is different from programming for desktop devices, and there will be many more such places as you go through later chapters. Your users will appreciate it if you write your activities with the activity lifecycle in mind, and you will ultimately benefit.