I have a MediaRecorder in my IntentService class.
I want to
Run an IntentService to start recording audio
Run an IntentService to stop recording audio
I use IntentService because I want the audio recording to run even when my phone screen is turned off. Service runs in the background, so this is a good approach, right?
Anyways, I initiate my MediaRecorder in my IntentService at Step #1.
At Step#2, my instantiated MediaRecorder is NULL. It seems that all variable values inside the IntentService are reset, because the service ends and calls onDestroy.
What should I do? How do I keep a reference to my MediaRecorder ?
Update: Pasting my IntentService class
package com.dan190.enregistreur.BackgroundService;
import android.app.IntentService;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import com.dan190.enregistreur.RecordedFilesActivity;
import com.dan190.enregistreur.Util.RecordingStatus;
import java.io.IOException;
import java.util.Calendar;
/**
* Created by Dan on 17/03/2017.
*/
public class RecorderService extends IntentService {
private static final String TAG = RecorderService.class.getName();
private MediaRecorder mMediaRecorder;
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* #param name Used to name the worker thread, important only for debugging.
*/
public RecorderService(String name) {
super(name);
}
public RecorderService() {
super("RecorderService");
}
/**
* Handles intent.
* Unwraps intent to find out if we need to start, pause, or stop
* #param intent
*/
#Override
protected void onHandleIntent(#Nullable Intent intent) {
Log.d(TAG, "onHandleIntent");
Bundle bundle = intent.getExtras();
int status = bundle.getInt(RecordingStatus.RECORDING_STATUS_KEY);
switch (status) {
case RecordingStatus.STANDBY:
//do nothing
break;
case RecordingStatus.RECORDING:
//if mediaPlayer is null, initiate
//set datasource
//prepare
//record
Log.d(TAG, "start recording");
startRecording();
break;
case RecordingStatus.PAUSED:
//pause
Log.d(TAG, "pause recording");
break;
case RecordingStatus.STOPPED:
//stop
Log.d(TAG, "stop recording");
stopRecording();
Intent newIntent = new Intent(this, RecordedFilesActivity.class);
startActivity(newIntent);
break;
case RecordingStatus.OPENDIR:
//open recorded files
Log.d(TAG, "open directory");
getFileName();
Intent newIntent2 = new Intent(this, RecordedFilesActivity.class);
startActivity(newIntent2);
break;
default:
break;
}
}
/**
* File stuff
*/
private static String pathName,
fileName;
public static String getPath() {
return pathName;
}
public static String getFilePath() {
return fileName;
}
private String getFileName() {
Calendar calendar = Calendar.getInstance();
PackageManager m = getPackageManager();
pathName = getPackageName();
PackageInfo p = null;
try {
p = m.getPackageInfo(pathName, 0);
} catch(PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
pathName = p.applicationInfo.dataDir;
// fileName = Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM + "/Recordings";
fileName = pathName;
fileName += String.format("/%d_%d_%d_%d_%d_%d.3gp", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY) + 1, calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
return fileName;
}
/**
* Recorder stuff
*/
private void startRecording() {
String fileName = getFileName();
Log.d(TAG, String.format("file name is %s", fileName));
if (mMediaRecorder == null) mMediaRecorder = new MediaRecorder();
//NOTE that mediaRecorder cannot pause for API < 24, which is Android 7.0
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setOutputFile(getFileName());
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mMediaRecorder.prepare();
} catch(IOException e) {
Log.e(TAG, "prepare() failed");
e.printStackTrace();
}
try {
mMediaRecorder.start();
} catch(IllegalStateException e) {
Log.e(TAG, e.getStackTrace().toString());
}
}
private void stopRecording() {
try {
mMediaRecorder.stop();
mMediaRecorder.reset();
} catch(RuntimeException e) {
e.printStackTrace();
}
releaseMediaRecorder();
}
private void releaseMediaRecorder() {
if (mMediaRecorder != null) {
mMediaRecorder.release();
mMediaRecorder = null;
} else {
Log.w(TAG, "mediaRecroder is already Null");
}
}
/**
* Lifecycle stuff
*/
#Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
}
Fixed it by using Service instead of Intent service.
This post proved helpful. Service.onDestroy() is called directly after creation, anyway the Service does its work
Basically, IntentService is designed so that it will immediately stop all services after the code within onHandleIntent is finished. Thus, I was never able to keep my MediaRecorder alive.
With a normal Service, I am able to have more control over the lifecycle of my service. I can start and stop it when I want to. With IntentService, this is not possible.
Related
I am trying to send the selected image the user selected from their Google Drive to another function. I would like retrieve the content the user selected and send the photo uri or a bitmap of that photo to a function. Can anyone help me out?
This is my code:
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFile;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.MetadataChangeSet;
import com.google.android.gms.drive.OpenFileActivityBuilder;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "Google Drive Activity";
private static final int REQUEST_CODE_RESOLUTION = 1;
private static final int REQUEST_CODE_OPENER = 2;
private GoogleApiClient mGoogleApiClient;
private boolean fileOperation = false;
private DriveId mFileId;
public DriveFile file;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Inside the onCreate Method");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* Called when the activity will start interacting with the user.
* At this point your activity is at the top of the activity stack,
* with user input going to it.
*/
#Override
protected void onResume() {
super.onResume();
Log.i(TAG, "Inside the onResume method");
if (mGoogleApiClient == null) {
/**
* Create the API client and bind it to an instance variable.
* We use this instance as the callback for connection and connection failures.
* Since no account name is passed, the user is prompted to choose.
*/
Log.i(TAG, "Creating the API client");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
Log.i(TAG,"About to call connect method().");
mGoogleApiClient.connect();
}
#Override
protected void onStop() {
super.onStop();
Log.i(TAG,"inside the onStop method");
if (mGoogleApiClient != null) {
// disconnect Google API client connection
mGoogleApiClient.disconnect();
}
super.onPause();
}
#Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Inside onConnectionFailed()");
// Called whenever the API client fails to connect.
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
// show the localized error dialog.
GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
return;
}
/**
* The failure has a resolution. Resolve it.
* Called typically when the app is not yet authorized, and an authorization
* dialog is displayed to the user.
*/
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
/**
* It invoked when Google API client connected
* #param connectionHint
*/
#Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "Inside onConnected Method()");
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
}
/**
* It invoked when connection suspend
* #param cause
*/
#Override
public void onConnectionSuspended(int cause) {
Log.i(TAG, "GoogleApiClient connection suspended");
}
public void onClickCreateFile(View view){
fileOperation = true;
// create new contents resource
Drive.DriveApi.newDriveContents(mGoogleApiClient)
.setResultCallback(driveContentsCallback);
}
public void onClickOpenFile(View view){
fileOperation = false;
Log.i(TAG, "Selected the onClickOpenFile button");
// create new contents resource
Drive.DriveApi.newDriveContents(mGoogleApiClient)
.setResultCallback(driveContentsCallback);
}
/**
* Open list of folder and file of the Google Drive
*/
public void OpenFileFromGoogleDrive(){
Log.i(TAG, "Inside OpenFIleFromGoogleDrive method.");
IntentSender intentSender = Drive.DriveApi
.newOpenFileActivityBuilder()
//Restricting the user to only select txt and image support files for Tesseract.
.setMimeType(new String[] { "text/plain", "image/png", "image/jpeg", "image/bmp", "jpg" })
.build(mGoogleApiClient);
try {
startIntentSenderForResult(
intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.w(TAG, "Unable to send intent", e);
}
}
/**
* This is Result result handler of Drive contents.
* this callback method call CreateFileOnGoogleDrive() method
* and also call OpenFileFromGoogleDrive() method,
* send intent onActivityResult() method to handle result.
*/
final ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback =
new ResultCallback<DriveApi.DriveContentsResult>() {
#Override
public void onResult(DriveApi.DriveContentsResult result) {
Log.i(TAG, "Inside onResultCallback()");
if (result.getStatus().isSuccess()) {
if (fileOperation == true) {
CreateFileOnGoogleDrive(result);
} else {
OpenFileFromGoogleDrive();
}
}
}
};
/**
* Create a file in root folder using MetadataChangeSet object.
* #param result
*/
public void CreateFileOnGoogleDrive(DriveApi.DriveContentsResult result){
Log.i(TAG, "Inside createfileongoogledrive method");
final DriveContents driveContents = result.getDriveContents();
// Perform I/O off the UI thread.
new Thread() {
#Override
public void run() {
// write content to DriveContents
OutputStream outputStream = driveContents.getOutputStream();
Writer writer = new OutputStreamWriter(outputStream);
try {
writer.write("Hello Joseph, you created me(: !");
writer.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("test2")
.setMimeType("text/plain")
.setStarred(true).build();
// create a file in root folder
Drive.DriveApi.getRootFolder(mGoogleApiClient)
.createFile(mGoogleApiClient, changeSet, driveContents)
.setResultCallback(fileCallback);
}
}.start();
}
/**
* Handle result of Created file
*/
final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
ResultCallback<DriveFolder.DriveFileResult>() {
#Override
public void onResult(DriveFolder.DriveFileResult result) {
if (result.getStatus().isSuccess()) {
Toast.makeText(getApplicationContext(), "file created: "+""+
result.getDriveFile().getDriveId(), Toast.LENGTH_LONG).show();
}
return;
}
};
/**
* Handle Response of selected file
* #param requestCode
* #param resultCode
* #param data
*/
#Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_CODE_OPENER:
if (resultCode == RESULT_OK) {
mFileId = (DriveId) data.getParcelableExtra(
OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
Log.e("file id", mFileId.getResourceId() + "");
String url = "https://drive.google.com/open?id="+ mFileId.getResourceId();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
}
I wanted to reduce the image size to kb's, before uploading to server, basically what my code does it, capture a image store it locally, then upload it to the server.
just wanted to reduce image size while it stores locally on the External Storage. Found a good answer here
But the problem is, I really don't know how to add up the code in the link with my code below.
New to android, please help!
package com.project.camera;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends Activity {
// LogCat tag
private static final String TAG = MainActivity.class.getSimpleName();
// Camera activity request codes
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private Uri fileUri; // file url to store image
private Button btnCapturePicture;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
/**
* Capture image button click event
*/
btnCapturePicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// capture picture
captureImage();
}
});
// Checking camera availability
if (!isDeviceSupportCamera()) {
Toast.makeText(getApplicationContext(),
"Sorry! Your device doesn't support camera",
Toast.LENGTH_LONG).show();
// will close the app if the device does't have camera
finish();
}
}
/**
* Checking device has camera hardware or not
* */
private boolean isDeviceSupportCamera() {
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
/**
* Launching camera app to capture image
*/
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
/**
* Here we store the file url as it will be null after returning from camera
* app
*/
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on screen orientation
// changes
outState.putParcelable("file_uri", fileUri);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file url
fileUri = savedInstanceState.getParcelable("file_uri");
}
/**
* Receiving activity result method will be called after closing the camera
* */
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image
// launching upload activity
launchUploadActivity(true);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
}
private void launchUploadActivity(boolean isImage){
Intent i = new Intent(MainActivity.this, UploadActivity.class);
i.putExtra("filePath", fileUri.getPath());
i.putExtra("isImage", isImage);
startActivity(i);
}
/**
* ------------ Helper Methods ----------------------
* */
/**
* Creating file uri to store image
*/
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/**
* returning image
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
Config.IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "Oops! Failed create "
+ Config.IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "Selfie_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
}
Use this, (I edited onPictureTaken() function to fit your needs)
Will return false if the input path does not exist and no error occured on compression.
public boolean resizeImage(String originalFilePath, String compressedFilePath) {
InputStream in = null;
try {
in = new FileInputStream(originalFilePath);
} catch (FileNotFoundException e) {
Log.e("TAG","originalFilePath is not valid", e);
}
if (in == null) {
return false;
}
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap preview_bitmap = BitmapFactory.decodeStream(in, null, options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
preview_bitmap.compress(Bitmap.CompressFormat.JPEG, 60, stream);
byte[] byteArray = stream.toByteArray();
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(compressedFilePath);
outStream.write(byteArray);
outStream.close();
} catch (Exception e) {
Log.e("TAG","could not save", e);
}
return true;
}
I'm trying to get user location in the background.
Everything works great on my phone (htc one m7), but from some reason it's not working on two devices I tested:
Samsung galaxy s3
Sony Xperia Z1
btw: I added everything to the manifest as it should be.
this is my code:
**BackgroundLocationService **
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationRequest;
public class BackgroundLocationService extends Service implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
IBinder mBinder = new LocalBinder();
private LocationClient mLocationClient;
private LocationRequest mLocationRequest;
// Flag that indicates if a request is underway.
private boolean mInProgress;
private Boolean servicesAvailable = false;
public class LocalBinder extends Binder {
public BackgroundLocationService getServerInstance() {
return BackgroundLocationService.this;
}
}
#Override
public void onCreate() {
super.onCreate();
mInProgress = false;
// Create the LocationRequest object
mLocationRequest = LocationRequest.create();
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
// Set the update interval to 5 seconds
mLocationRequest.setInterval(0);
// Set the fastest update interval to 1 second
mLocationRequest.setFastestInterval(1);
servicesAvailable = servicesConnected();
/*
* Create a new location client, using the enclosing class to
* handle callbacks.
*/
mLocationClient = new LocationClient(this, this, this);
}
private boolean servicesConnected() {
// Check that Google Play services is available
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
return true;
} else {
return false;
}
}
public int onStartCommand (Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
if(!servicesAvailable || mLocationClient.isConnected() || mInProgress)
return START_STICKY;
setUpLocationClientIfNeeded();
if(!mLocationClient.isConnected() || !mLocationClient.isConnecting() && !mInProgress)
{
mInProgress = true;
mLocationClient.connect();
}
return START_STICKY;
}
/*
* Create a new location client, using the enclosing class to
* handle callbacks.
*/
private void setUpLocationClientIfNeeded()
{
if(mLocationClient == null)
mLocationClient = new LocationClient(this, this, this);
}
// Define the callback method that receives location updates
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public void appendLog(String text, String filename)
{
File logFile = new File(filename);
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onDestroy(){
// Turn off the request flag
mInProgress = false;
if(servicesAvailable && mLocationClient != null) {
//mLocationClient.removeLocationUpdates(callbackIntent);
// Destroy the current location client
mLocationClient = null;
}
// Display the connection status
// Toast.makeText(this, DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
/*
* Called by Location Services when the request to connect the
* client finishes successfully. At this point, you can
* request the current location or start periodic updates
*/
#Override
public void onConnected(Bundle bundle) {
// Request location updates using static settings
Intent intent = new Intent(this, LocationReceiver.class);
PendingIntent locationIntent = PendingIntent.getBroadcast(getApplicationContext(), 14872, intent, PendingIntent.FLAG_CANCEL_CURRENT);
mLocationClient.requestLocationUpdates(mLocationRequest, locationIntent);
}
/*
* Called by Location Services if the connection to the
* location client drops because of an error.
*/
#Override
public void onDisconnected() {
// Turn off the request flag
mInProgress = false;
// Destroy the current location client
mLocationClient = null;
// Display the connection status
// Toast.makeText(this, DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
}
/*
* Called by Location Services if the attempt to
* Location Services fails.
*/
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
mInProgress = false;
/*
* Google Play services can resolve some errors it detects.
* If the error has a resolution, try sending an Intent to
* start a Google Play services activity that can resolve
* error.
*/
if (connectionResult.hasResolution()) {
// If no resolution is available, display an error dialog
} else {
}
}
}
**LocationReceiver **
import java.util.List;
import java.util.Locale;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.util.Log;
import com.google.android.gms.location.LocationClient;
public class LocationReceiver extends BroadcastReceiver {
public static double lat;
public static double alt;
public static String address;
#Override
public void onReceive(Context context, Intent intent) {
Location location = (Location) intent.getExtras().get(LocationClient.KEY_LOCATION_CHANGED);
Log.d("New Location Reciver", "location "+location.toString());
lat = location.getLatitude();
alt = location.getAltitude();
address = getAddressByCord(lat, alt, context);
}
public static String getAddressByCord(double lat, double longi, Context context) {
try {
Geocoder geo = new Geocoder(context, Locale.getDefault());
List<Address> addresses = geo.getFromLocation(lat, longi, 1);
if (addresses.isEmpty()) {
return "Waiting for Location";
} else {
if (addresses.size() > 0) {
String s = "";
if (addresses.get(0).getFeatureName() != null)
s += addresses.get(0).getFeatureName();
if (addresses.get(0).getThoroughfare() != null)
s += "," + addresses.get(0).getThoroughfare();
if (addresses.get(0).getLocality() != null)
s += "," + addresses.get(0).getLocality();
if (addresses.get(0).getAdminArea() != null)
s += "," + addresses.get(0).getAdminArea();
if (addresses.get(0).getCountryName() != null)
s += "," + addresses.get(0).getCountryName();
return s;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
if someone has any idea, Thank you!
code to check for google play services.
private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 94378;
private boolean isGooglePlay() {
int mIsGooglePlayServicesAvailable = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(context);
switch (mIsGooglePlayServicesAvailable) {
case ConnectionResult.SUCCESS:
return true;
default:
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
mIsGooglePlayServicesAvailable, this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);
// If Google Play services can provide an error dialog
if (errorDialog != null) {
// Create a new DialogFragment for the error dialog
ErrorDialogFragment errorFragment = new ErrorDialogFragment();
// Set the dialog in the DialogFragment
errorFragment.setDialog(errorDialog);
// Show the error dialog in the DialogFragment
errorFragment.show(getSupportFragmentManager(),
"Location Updates");
}
// case ConnectionResult.SERVICE_MISSING:
// case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
// case ConnectionResult.SERVICE_DISABLED:
// case ConnectionResult.SERVICE_INVALID:
// case ConnectionResult.DATE_INVALID:
}
return false;
}
/*
* This is a class used to resolve errors with google play services It is
* copied code that doesn't run durring normal operation.
*/
public static class ErrorDialogFragment extends DialogFragment {
// Global field to contain the error dialog
private Dialog mDialog;
// Default constructor. Sets the dialog field to null
public ErrorDialogFragment() {
super();
mDialog = null;
}
// Set the dialog to display
public void setDialog(Dialog dialog) {
mDialog = dialog;
}
// Return a Dialog to the DialogFragment.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return mDialog;
}
}
I found the answer.
It was the devices own definitions.
The device was not enabling apps to use location .
thank you anyway!
I wrote code to capture image automatically at background.
For this, I made a thread and taking picture in that.
My code looks like
package com.camsharp;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private int cameraId = 0;
private Camera mCamera;
private CameraPreview mPreview;
String fileName = "tempImage.jpeg";
File file;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("EXEC ", "EXECUTED ");
setContentView(R.layout.activity_main);
// Create an instance of Camera
mCamera = getCameraInstance(cameraId);
if (mCamera == null) {
Toast.makeText(
getApplicationContext(),
"The camera service is currently unavailable, please try again!",
Toast.LENGTH_LONG).show();
finish();
} else {
// Create our Preview view and set it as the content of our
// activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.camera_preview);
frameLayout.addView(mPreview);
}
// start thread for these
MyTimerTask myTask = new MyTimerTask();
Timer myTimer = new Timer();
// public void schedule (TimerTask task, long delay, long period)
// Schedule a task for repeated fixed-delay execution after a specific
// delay.
//
// Parameters
// task the task to schedule.
// delay amount of time in milliseconds before first execution.
// period amount of time in milliseconds between subsequent executions.
myTimer.schedule(myTask, 3000, 1500);
}
class MyTimerTask extends TimerTask {
public void run() {
try {
Log.e("SUCCESS ", "IT IS OKAY ");
//mCamera.takePicture(null, null, null, mPictureCallback);
mCamera.takePicture(null, null, mPictureCallback);
file = new File(getFilesDir(), fileName);
System.out.println(file);
} catch (Exception e) {
Log.e("Error ", "EXCEPTION ");
e.printStackTrace();
}
Log.e("LOG ", "timer testing");
}
}
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
Log.e("Callback TAG", "Here in jpeg Callback");
if (imageData != null) {
FileOutputStream outputStream;
try {
outputStream = openFileOutput(fileName,
Context.MODE_PRIVATE);
outputStream.write(imageData);
outputStream.close();
// Intent intent = new Intent(SnapScreen.this,
// PreviewScreen.class);
// if (fromMessageReview == true) {
// intent.putExtra("fromMessageReview", "true");
// }
// startActivity(intent);
// overridePendingTransition(R.anim.slide_in,
// R.anim.slide_out);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
#Override
protected void onDestroy() {
super.onDestroy();
releaseCamera();
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(int cameraId) {
Camera c = null;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
c = Camera.open(cameraId);
} else {
c = Camera.open();
}
} catch (Exception e) {
c = null;
}
return c; // returns null if camera is unavailable
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
}
Here, when Thread runs, it gives exception, java.lang.NullPointerException
I tried.
mCamera.startPreview();
before taking new image, but not use.
Can anyone identify where I am doing mistake and why?
I am getting the following exception when I run the texter application in android.
STACK_TRACE :
java.lang.NullPointerException at
com.texter.messenger.SmsReceiverService$ServiceHandler.handleMessage(SmsReceiverService.java:116)
at android.os.Handler.dispatchMessage(Handler.java:99) at
android.os.Looper.loop(Looper.java:123) at
android.os.HandlerThread.run(HandlerThread.java:60) , PHONE_MODEL :
GT-I9000 , ANDROID_VERSION : 2.2 , APP_VERSION_CODE : 10
30.Nov.2011 00:33:50 AM , CUSTOM_DATA : , STACK_TRACE : java.lang.IllegalArgumentException: Receiver not registered:
android.widget.ViewFlipper$1#40597cc0 at
android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:634) at
android.app.ContextImpl.unregisterReceiver(ContextImpl.java:881) at
android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:331)
at
android.widget.ViewFlipper.onDetachedFromWindow(ViewFlipper.java:104)
at android.view.View.dispatchDetachedFromWindow(View.java:6235) at
android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1250)
at
android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1248)
at
android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1248)
at
android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1248)
at
android.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1838)
at android.view.ViewRoot.doDie(ViewRoot.java:2916) at
android.view.ViewRoot.die(ViewRoot.java:2886) at
android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:254)
at
android.view.Window$LocalWindowManager.removeViewImmediate(Window.java:445)
at
android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3182)
at
android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3287)
at android.app.ActivityThread.access$1600(ActivityThread.java:132)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1042)
at android.os.Handler.dispatchMessage(Handler.java:99) at
android.os.Looper.loop(Looper.java:150) at
android.app.ActivityThread.main(ActivityThread.java:4293) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:507) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at
dalvik.system.NativeStart.main(Native Method).
I am new to these errors. How to solve these errors.
package com.texter.messenger;
import java.util.List;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.Process;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.telephony.SmsMessage.MessageClass;
import android.telephony.TelephonyManager;
import android.util.Log;
import com.texter.common.TexterConstants;
import com.texter.common.TexterNotification;
import com.texter.common.Utils;
import com.texter.data.TexterDB;
import com.texter.data.TexterDB.Schedule;
import com.texter.preferences.TexterPreferenceManager;
import com.texterpro.app.ConversationList;
import com.texterpro.app.R;
import com.texterpro.app.SmsPopupView;
public class SmsReceiverService extends Service {
private static final String LOG_TAG = TexterConstants.COMMON_TAG;
private static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String ACTION_MMS_RECEIVED = "android.provider.Telephony.WAP_PUSH_RECEIVED";
private static final String ACTION_MESSAGE_RECEIVED = "net.everythingandroid.smspopup.MESSAGE_RECEIVED";
private static final String MMS_DATA_TYPE = "application/vnd.wap.mms-message";
// https://android.googlesource.com/platform/packages/apps/Mms/+/master/src/com/android/mms/transaction/SmsReceiverService.java
public static final String MESSAGE_SENT_ACTION = "com.android.mms.transaction.MESSAGE_SENT";
/*
* This is the number of retries and pause between retries that we will keep
* checking the system message database for the latest incoming message
*/
private static final int MESSAGE_RETRY = 8;
private static final int MESSAGE_RETRY_PAUSE = 1000;
private Context context;
private ServiceHandler mServiceHandler;
private Looper mServiceLooper;
private int mResultCode;
private static final Object mStartingServiceSync = new Object();
private static PowerManager.WakeLock mStartingService;
private static final int TOAST_HANDLER_MESSAGE_SENT = 0;
private static final int TOAST_HANDLER_MESSAGE_SEND_LATER = 1;
private static final int TOAST_HANDLER_MESSAGE_FAILED = 2;
#Override
public void onCreate() {
HandlerThread thread = new HandlerThread("Log.LOGTAG",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
context = getApplicationContext();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
Log.v(LOG_TAG, "Oncreate");
}
#Override
public void onStart(Intent intent, int startId) {
Log.v(LOG_TAG, "OnStart");
mResultCode = intent != null ? intent.getIntExtra("result", 0) : 0;
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
#Override
public void onDestroy() {
mServiceLooper.quit();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
#Override
public void handleMessage(Message msg) {
Log.v(LOG_TAG, "handlemessage:");
Log.v(LOG_TAG, msg.toString());
int serviceId = msg.arg1;
Intent intent = (Intent) msg.obj;
String action = intent.getAction();
String dataType = intent.getType();
if (ACTION_SMS_RECEIVED.equals(action)) {
handleSmsReceived(intent);
} else if (ACTION_MMS_RECEIVED.equals(action)
&& MMS_DATA_TYPE.equals(dataType)) {
handleMmsReceived(intent);
} else if (MESSAGE_SENT_ACTION.equals(action)) {
handleSmsSent(intent);
} else if (ACTION_MESSAGE_RECEIVED.equals(action)) {
handleMessageReceived(intent);
}
// NOTE: We MUST not call stopSelf() directly, since we need to
// make sure the wake lock acquired by AlertReceiver is released.
finishStartingService(SmsReceiverService.this, serviceId);
}
}
/**
* Handle receiving a SMS message
*/
private void handleSmsReceived(Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
SmsMessage[] messages = Utils.getMessagesFromIntent(intent);
if (messages != null) {
notifyMessageReceived(new SmsMmsMessage(context, messages,
System.currentTimeMillis()));
}
}
}
private void notifyMessageReceived(SmsMmsMessage message) {
// Class 0 SMS, let the system handle this
if (message.getMessageType() == SmsMmsMessage.MESSAGE_TYPE_SMS
&& message.getMessageClass() == MessageClass.CLASS_0) {
return;
}
TelephonyManager mTM = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
boolean callStateIdle = mTM.getCallState() == TelephonyManager.CALL_STATE_IDLE;
/*
* If popup is enabled for this user -AND- the user is not in a call
* -AND- -AND- phone is not docked -AND- (screen is locked -OR- (setting
* is OFF to only show on keyguard -AND- user is not in messaging app:
* then show the popup activity, otherwise check if notifications are on
* and just use the standard notification))
*/
boolean isApp = TexterPreferenceManager.getInstance(this)
.isAppEnabled();
boolean showPop = TexterPreferenceManager.getInstance(this)
.isPopupEnabled();
boolean showNotify = TexterPreferenceManager.getInstance(this)
.isNotifyEnabled();
// Log.v(LOG_TAG," App = "+ isApp );
// Log.v(LOG_TAG," pop = "+showPop );
// Log.v(LOG_TAG," notify = "+showNotify );
// if conversationList is visible then do not show pop
if (!ConversationList.isVisible()) {
if (isApp && callStateIdle && showPop) {
// Log.v(LOG_TAG," showing popup = " );
if (!SmsPopupView.isPopupVisible())
context.startActivity(message.getPopupIntent());
}
}
if (isApp && showNotify) {
TexterNotification.ShowMessageNotification(this, message);
}
}
/**
* Handle receiving a MMS message
*/
private void handleMmsReceived(Intent intent) {
}
/**
* Handle receiving an arbitrary message (potentially coming from a 3rd
* party app)
*/
private void handleMessageReceived(Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
}
}
/*
* Handler to deal with showing Toast messages for message sent status
*/
public Handler mToastHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
if (msg != null) {
switch (msg.what) {
case TOAST_HANDLER_MESSAGE_SENT:
// TexterNotification.showToast(SmsReceiverService.this,R.string.quickreply_sent_toast);
break;
case TOAST_HANDLER_MESSAGE_SEND_LATER:
TexterNotification.showToast(SmsReceiverService.this,
R.string.quickreply_failed_send_later);
break;
case TOAST_HANDLER_MESSAGE_FAILED:
TexterNotification.showToast(SmsReceiverService.this,
R.string.quickreply_failed);
break;
}
}
}
};
/*
* Handle the result of a sms being sent
*/
private void handleSmsSent(Intent intent) {
Log.v(LOG_TAG, "HandleSMSSent called");
PackageManager pm = getPackageManager();
Intent sysIntent = null;
Intent tempIntent;
List<ResolveInfo> receiverList;
boolean forwardToSystemApp = true;
// Search for system messaging app that will receive our
// "message sent complete" type intent
tempIntent = intent.setClassName(
SmsMessageSender.MESSAGING_PACKAGE_NAME,
SmsMessageSender.MESSAGING_RECEIVER_CLASS_NAME);
tempIntent.setAction(SmsReceiverService.MESSAGE_SENT_ACTION);
receiverList = pm.queryBroadcastReceivers(tempIntent, 0);
if (receiverList.size() > 0) {
sysIntent = tempIntent;
}
Bundle b = intent.getExtras();
long rowid = 0;
rowid = b == null ? 0 : b.getLong("ROWID");
/*
* No system messaging app was found to forward this intent to,
* therefore we will need to do the final piece of this ourselves which
* is basically moving the message to the correct folder depending on
* the result.
*/
// TexterNotification.showToast(SmsReceiverService.this,
// "before moving folder");
if (sysIntent == null) {
forwardToSystemApp = false;
Uri uri = intent.getData();
Log.v(LOG_TAG, "id = " + rowid);
if (mResultCode == Activity.RESULT_OK) {
SmsMessageSender.moveMessageToFolder(this, uri,
SmsMessageSender.MESSAGE_TYPE_SENT);
} else if ((mResultCode == SmsManager.RESULT_ERROR_RADIO_OFF)
|| (mResultCode == SmsManager.RESULT_ERROR_NO_SERVICE)) {
SmsMessageSender.moveMessageToFolder(this, uri,
SmsMessageSender.MESSAGE_TYPE_QUEUED);
} else {
SmsMessageSender.moveMessageToFolder(this, uri,
SmsMessageSender.MESSAGE_TYPE_FAILED);
}
}
// Check the result and notify the user using a toast
if (mResultCode == Activity.RESULT_OK) {
mToastHandler.sendEmptyMessage(TOAST_HANDLER_MESSAGE_SENT);
TexterDB.getInstance().updateStatus(rowid,
Schedule.STATUS_SCHEDULED_SENT, 0);
} else if ((mResultCode == SmsManager.RESULT_ERROR_RADIO_OFF)
|| (mResultCode == SmsManager.RESULT_ERROR_NO_SERVICE)) {
TexterDB.getInstance().updateStatus(rowid,
Schedule.STATUS_NOT_SENT, -1);
} else {
mToastHandler.sendEmptyMessage(TOAST_HANDLER_MESSAGE_FAILED);
TexterDB.getInstance().updateStatus(rowid,
Schedule.STATUS_NOT_SENT, mResultCode);
}
if (forwardToSystemApp) {
try {
PendingIntent.getBroadcast(this, 0, sysIntent, 0).send(
mResultCode);
} catch (CanceledException e) {
e.printStackTrace();
}
}
}
/**
* Start the service to process the current event notifications, acquiring
* the wake lock before returning to ensure that the service will run.
*/
public static void beginStartingService(Context context, Intent intent) {
synchronized (mStartingServiceSync) {
if (mStartingService == null) {
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
mStartingService = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK,
"Texter.SmsReceiverService");
mStartingService.setReferenceCounted(false);
}
mStartingService.acquire();
context.startService(intent);
Log.v(LOG_TAG, "service started");
}
}
/**
* Called back by the service when it has finished processing notifications,
* releasing the wake lock if the service is now stopping.
*/
public static void finishStartingService(Service service, int startId) {
synchronized (mStartingServiceSync) {
if (mStartingService != null) {
if (service.stopSelfResult(startId)) {
mStartingService.release();
}
}
}
}
}
Look at the top of the trace... look at the code at:
com.texter.messenger.SmsReceiverService$ServiceHandler.handleMessage(SmsReceiverService.java:116)
Looking at the code... check that the variable intent is not null before you call handleSmsReceived
I had the same bug, still looking for the correct solution but temporarily I just comment out the onDetachFromWindow() method. In my case it was for testing purpose so commenting it out didn't affect me but not sure what's in your case but give it a try.
In general, this question/answer gives an introduction to reading these errors:
What is a stack trace, and how can I use it to debug my application errors?
Looking at the code, intent should not be null, after all intent.getAction() executes without error.
It seems more likely that you have a problem with ViewFlipper, getting some "Receiver not registered" exception in the nested stack trace:
java.lang.IllegalArgumentException: Receiver not registered: android.widget.ViewFlipper$1#40597cc0
Maybe this question can point you in the right direction:
ViewFlipper : Receiver not registered