• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Widget that can open an app by itself?

Mike Sp

Lurker
Hello!

I am working on a project where our primary goal is to have your phone take a photo of you at random times throughout the day. It would be something that the user would know about and install willfully.

I know you can't have an app running in the background that takes a picture. The only solution we can conceive to be possible would be to have a widget that opens a camera app takes the picture and closes the camera app.

Is this something that is possible? Or do we need to scrap this idea?

Thank you!
Mike
 
I don't think the best route would be to open the camera app, this would see very hacky and you would have to try to mimic keypresses. I think the solution you would want is to talk directly to the android.hardware.camera class. On that page it provides step by step on how you would use the camera.

Good luck!
 
Would I be able to take a photo of the user without opening an app? Directly via the widget? For some reason I was under the impression this was not possible.
 
Would I be able to take a photo of the user without opening an app? Directly via the widget? For some reason I was under the impression this was not possible.

What you would need to do is create a service that programmatically unlocks the device, captures and saves the pictures. I am not 100% that you need to unlock the screen, or have the screen just turn on. If it is possible to control the camera hardware without a screen on and unlocked, this most likely wouldn't work on all devices due to the fact different OEMs pick what they want to implement or not.

However, you can test a background service (start sticky so it keeps running) to unlock and take a picture.

To unlock device:
Java:
KeyguardManager keyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
  mLock = mKeyGuardManager.newKeyguardLock("activity_classname");
  mLock.disableKeyguard();

This requires this permission: <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

To take a picture you need a surfaceView like this:
Java:
SurfaceView view = new SurfaceView(this);
c.setPreviewDisplay(view.getHolder());
c.startPreview();
c.takePicture(shutterCallback, rawPictureCallback, jpegPictureCallback);
Take a look at this blog post to see some testing this one dev already did.

This will require all the normal camera permissions too:
<uses-permissionandroid:name="android.permission.CAMERA"/>
<uses-featureandroid:name="android.hardware.camera"/>


Again, this code may work on some devices but not all for the same reasons. Also keep in mind that 3rd party camera apps don't all implement the same features, which means you may just test some things above and control the camera hardware yourself to avoid those issues.
 
Thank you so much Steve and Lordvincent!

I would only need this to run/work when the user is using the phone. It would take their photo (with the front facing camera) at random intervals. It sounds like what I'm trying to do is very possible.

Thank you so much!
Michael
 
Back
Top Bottom