Recipe Starting a Service When the Camera Button Is Pressed

This recipe shows how to start a service based on a broadcasted event, such as when the camera button is pressed. The broadcast receiver is needed to listen for the specified event(s) and subsequently launch the service.The broadcast receiver itself is started in another component. (Here, it is implemented as a standalone activity, SimpleActivity.)

The activity shown in Listing 3.14 sets up a broadcast receiver and sets up an intent with the filter for the camera button. The filter for package-added messages is also added for illustration purposes. Then, the broadcast receiver is started and this intent filter is passed to it using the registerReceiver() method.

Listing 3.14 src/com/cookbook/simple_receiver/ package com.cookbook.simple_receiver;

import; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle;

public class SimpleActivity extends Activity { SimpleBroadcastReceiver intentReceiver = new SimpleBroadcastReceiver();

/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

IntentFilter intentFilter =

new IntentFilter(Intent.ACTION_CAMERA_BUTTON); intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); registerReceiver(intentReceiver, intentFilter);

@Override protected void onDestroy() {



Note the receiver is unregistered if the activity is ever destroyed.This is unnecessary, but useful.The BroadcastReceiver component is shown in Listing 3.15.The single lifecycle method onReceive() is overridden to check for any broadcasted event. If it matches the specified event (here, it is the ACTION_CAMERA_BUTTON event), a service is started in the original context.

Listing 3.15 src/com/cookbook/simple_receiver/SimpleBroadcastReceiver .java package com.cookbook.simple_receiver;

import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent;

public class SimpleBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context rcvContext, Intent rcvlntent) { String action = rcvIntent.getAction(); if (action.equals(Intent.ACTION_CAMERA_BUTTON)) { rcvContext.startService(new Intent(rcvContext, SimpleService2.class));

The service that is started in the SimpleBroadcastReceiver of Listing 3.15 is shown in Listing 3.16.The service simply shows whether it was started or stopped using Toast.

Listing 3.16 src/com/cookbook/simple_receiver/ package com.cookbook.simple_receiver;

import; import android.content.Intent; import android.os.IBinder; import android.widget.Toast;

public class SimpleService2 extends Service { @Override public IBinder onBind(Intent arg0) { return null;

@Override public void onCreate() { super.onCreate();

Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();

@Override public void onDestroy() { super.onDestroy();

Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


  • naomi
    How start camera in service android api?
    6 years ago

Post a comment