Sarah
Sarah

Reputation: 2893

What is missing in order to make my android camera flash turn on?

I am trying to create a basic find my phone application and I'm running into a bit of an issue. So far I am able to text a code word to my phone and have it play a song. Next, I would like to be able to text a different key word and have it turn on the camera flash either in a steady light or pulsating. I have seen tons of example code so far but I can't figure out why it won't work for me.

I have included in my manifest permissions to access the camera and camera flash. I have tried both of the examples from this link Setting Parameters.FLASH_MODE_TORCH doesn't work on Droid X 2.3

However several other options I have seen for turning on the camera flash on require entire classes instead of a small function.

Here is the code I have so far:

import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Camera;
import android.hardware.*;
import android.hardware.Camera.Parameters;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {

    public static final String SMS_EXTRA_NAME = "pdus";
    public static final String SMS_URI = "content://sms";

    public void onReceive(Context context, Intent intent) {
        // Get SMS map from Intent
        Bundle extras = intent.getExtras();

        String messages = "";

        if (extras != null) {
            // Get received SMS array
            Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);

            // Get ContentResolver object for pushing encrypted SMS to incoming
            // folder
            ContentResolver contentResolver = context.getContentResolver();

            for (int i = 0; i < smsExtra.length; ++i) {
                SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);

                String body = sms.getMessageBody().toString();
                String address = sms.getOriginatingAddress();

                messages += "SMS from " + address + " :\n";
                messages += body + "\n";

            }

            // Display SMS message
            Toast.makeText(context, "WE GOT A MESSAGE", Toast.LENGTH_SHORT)
                    .show();

            // plays eye of the tiger for 45 seconds
            if (messages.contains("Hey Sarah")) {
                final MediaPlayer mp = MediaPlayer.create(context,
                        R.raw.sound_file_1);
                mp.start();

                // play ring tone for 45 seconds
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        mp.stop();
                    }
                }, 45000);
            }

            //turn the flash on 
            if (messages.contains("Hey Geoff")) {

                Camera mCamera = Camera.open();
                mCamera.startPreview();
                Camera.Parameters params = mCamera.getParameters();
                if (params.getFlashMode() != null) {
                        params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                }
                mCamera.setParameters(params);
            }
        }
    }
}

It appears there are issues with camera.open() (the error: The method open() is undefined for the type Camera) with startPrevies() (the error: add cast to mCamera) with Camera.Parameters (the error: Camera.Parameters cannot be resolved to a type) Etc.

Is the issue that this code snippet is trying to access android.graphics.Camera instead of android.hardware.Camera? I am brand new to android so it's been a steep learning curve for me.

Upvotes: 0

Views: 2035

Answers (1)

Sarah
Sarah

Reputation: 2893

import android.graphics.Camera needs to be removed - otherwise it is unclear which camera is being used.

Learning new things all the time!

Upvotes: 1

Related Questions