Wednesday, 7 September 2016

when to use try catch over throws key word in java

hello all
you should be careful while using throws keyword in method declaration. if you do so then the exception that may thrown from your method may go unnoticed. so it is good to use try catch for a piece of code and handle the exception.

class Example
{
   public void method()throws Exception
      {
   //the exception may come here , but we cant notice as we have declared method as throws        //exception.
     //so use try catch here.
      }
}

//or you can have another choice, use try catch where you call method

Example e = new Example();
try{
e.method
}
catch(Exception e)
{
 //handle here
}

Wednesday, 20 July 2016

how to enable location service in android marshmallow

You need to add a dependency to your app build.gradle:

compile 'com.google.android.gms:play-services:8.1.0'
then you can use following example by calling this method:
private void displayLocationSettingsRequest(Context context) {
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API).build();
    googleApiClient.connect();

    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(10000);
    locationRequest.setFastestInterval(10000 / 2);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);

    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    Log.i(TAG, "All location settings are satisfied.");
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");

                    try {
                        // Show the dialog by calling startResolutionForResult(), and check the result
                        // in onActivityResult().
                        status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        Log.i(TAG, "PendingIntent unable to execute request.");
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
                    break;
            }
        }
    });
}

reference link : https://github.com/googlesamples/android-play-location/blob/master/LocationSettings/app/src/main/java/com/google/android/gms/location/sample/locationsettings/MainActivity.java

Sunday, 29 May 2016

My View On Hinduism

Hindus must stop from trying to prove that Hinduism is a religion. It is not a religion it is the way of life or it is a culture.This culture may worship 33million gods, in some way they are our ancestors, we worship them not as god, we worship their ideal life, their contribution to humankind, their wisdom, their teachings, their struggles and sacrifices.we worship nature and animals because we know they are part of our civilization and we depend on each other. we worship tools because, they made our life easier. if we keep on trying to prove that Hinduism is a religion we may end up in
giving rise to extremists.

When you know that you do not belong to any particular religion you will feel liberated and your intelligence will never stop working.

Just think this culture has survived from thousands of years without single prophet, without single holy book and without conversion.
This is not a miracle this is the way of life.

This is only my opinion and i draw this opinion from teachings of swami Vivekananda and Sadhguru.

Tuesday, 26 April 2016

how to start mongodb server in windows

Steps to start mongodb server in windows
1. Open the command prompt
2. Open the directory where you have installed mongodb
3. Suppose you have installed mongodb in 'D' drive. open the directory upto bin folder
4. D:\>cd mongo\bin will take to bin directory.
5. Now run the mongo db server using command D:\mongo\bin>mongod --dbpath d:\mongo\data

 done, server will start. if not please check the command again and path where you installed  mongodb.

thankyou


Wednesday, 6 April 2016

AAPT err: ERROR: Unable to open PNG file Android

hello
sometimes you will get error like unable to open png files in android studio. it is because your project path is lengthy. it should kept as short as possible. this error will resolve after you path reduced to short.
thanku
mithun

Saturday, 2 April 2016

How to get SHA1 fingerprint for android

step 1: open command prompt
step 2:open the directory where you have installed jdk
C:\Program Files\Java\jdk1.8.0_60\bin
step 3: type command "keytool -list -v -keystore c:\users\UserName\.android\debug.keystore -alias androiddebugkey -storepass android -keypass android"

the word in bold font UserName should be replaced by user name of computer or os.

sterp 4: press enter. there you go you will get SHA1 fingerprint for android

Friday, 25 March 2016

setOnClickListener not working android

setOnClickListener was not working when came back to previous activity.
solution what i found is i override the onBackPressed method. i dont relied on default backpress option.

code in first activity
"
closeCameraPreview.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View view) {
        closeCameraPreview.setVisibility(View.INVISIBLE);
        mVideoView.setVisibility(View.INVISIBLE);
        Intent intent = new Intent(view.getContext(), MediaInfoActivity.class);
        String monument = PropertyFile.TargetOne;
        intent.putExtra("monument", monument);
        String monument_images = PropertyFile.TargetOne+"_images";
        intent.putExtra("monument_images",monument_images);
        startActivity(intent);
    }
});

"


code in second activity to come back to first activity.
@Overridepublic void onBackPressed() {
    Intent intent = new Intent(MediaInfoActivity.this, SimpleClientTrackingActivity.class);
    startActivity(intent);
}



i dont know this is the only solution. please comment if you know any alternate solution.
thanks in advance.