Friday, 26 August 2016

How to ask permission marshmallow and above in android ?

Hi guys it is very simple ask permission in above marshmallow .

I think the below code is useful to you.

private void insertDummyContactWrapper() {
        List<String> permissionsNeeded = new ArrayList<String>();
        final List<String> permissionsList = new ArrayList<String>();
        if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
            permissionsNeeded.add("GPS");
        if (!addPermission(permissionsList, Manifest.permission.READ_CONTACTS))
            permissionsNeeded.add("Read Contacts");
        if (!addPermission(permissionsList, Manifest.permission.WRITE_CONTACTS))
            permissionsNeeded.add("Write Contacts");
        if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
            permissionsNeeded.add("Write External Storage");
        if (!addPermission(permissionsList, Manifest.permission.ACCESS_COARSE_LOCATION))
            permissionsNeeded.add("Location");
        if (!addPermission(permissionsList, Manifest.permission.RECORD_AUDIO))
            permissionsNeeded.add("Record Audio");
        if (!addPermission(permissionsList, Manifest.permission.READ_PHONE_STATE))
            permissionsNeeded.add("Read Phone State");
        if (!addPermission(permissionsList, Manifest.permission.CALL_PHONE))
            permissionsNeeded.add("Call Phone");
        if (!addPermission(permissionsList, Manifest.permission.RECEIVE_SMS))
            permissionsNeeded.add("Recieve SMS");
        if (!addPermission(permissionsList, Manifest.permission.READ_SMS))
            permissionsNeeded.add("Read SMS");
        if (permissionsList.size() > 0) {
            if (permissionsNeeded.size() > 0) {
                // Need Rationale
                String message = "You need to grant access to the following set of android permissions to experience all the features of our in.";
                for (int i = 1; i < permissionsNeeded.size(); i++)
                    message = message + ", " + permissionsNeeded.get(i);
                showMessageOKCancel(message,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                                        REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
                            }
                        });
                return;
            }
            requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                    REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
            return;
        }
    }

By
goldenashok
Senior Software Engineer

Friday, 29 April 2016

How to find Network information 3G,4G or WIFI ?

Very simple to find  Network information 3G,4G or WIFI.

The below function used to find network information


public String getNetworkClass(Context con) {
    connectivityManager = (ConnectivityManager)con.getSystemService(con.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    if (info == null || !info.isConnected())
        return "Not Connected to Any Network"; //not connected  
    if (info.getType() == ConnectivityManager.TYPE_WIFI)
        return "WIFI";
    if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
        int networkType = info.getSubtype();
        switch (networkType) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace by 11               
 return "2G";
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14          
            case TelephonyManager.NETWORK_TYPE_EHRPD:  //api<11 : replace by 12           
            case TelephonyManager.NETWORK_TYPE_HSPAP:  //api<13 : replace by 15               
 return "3G";
            case TelephonyManager.NETWORK_TYPE_LTE:    //api<11 : replace by 13              
 return "4G";
            default:
                return "Unknown";
        }
    }
    return "Unknown";
}

how to find app version name in android?

It is very simple we can find app version name in android programmetically using the below function.

public String Android_Version(Context context){ PackageManager packageManager = context.getPackageManager(); try { PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0); return packageInfo.versionName; } catch (PackageManager.NameNotFoundException ex) {} catch(Exception e){} return "";
} 

Saturday, 16 April 2016

How to Compress Drawable image using thirtparty website?.

Compress image size is very important things. because drawable image increase the image size so we have to reduce the image size.


the below website reduce the image size

https://tinypng.com/

Wednesday, 9 March 2016

Image Download using asynchronous task

hi guys image download using asynchronous very easy. The below coding very useful to all


AsyncTaskRunnerObject runner = new AsyncTaskRunnerObject(this);
runner.execute(Partnerdownloadurl,userid);//update


Partnerdownloadurl = Download url (passing parameter )
userid = userid is the login member id



class AsyncTaskRunnerObject extends AsyncTask<String,String,String> {
    private String resp,Memberid,AsyType,OBJID;
    private Context myCtx;
    public AsyncTaskRunnerObject(Context ctx){
        this.myCtx = ctx;
    }
    @Override    protected void onPreExecute() {
        Log.e("Pre Execute","Pre Execute");
    }
    @Override    protected void onPostExecute(String s) {
        Log.e("Post Execute",""+s);
        StorePreference.SinglewritePreference(myCtx, "LOCAL_FEMALE_PROFILE_IMAGE", s);
    }
    @Override    protected String doInBackground(String... params) {
        HttpURLConnection urlConnection = null;
        try {
            URL uri = new URL(params[0]);
            Memberid = params[1];
            urlConnection = (HttpURLConnection) uri.openConnection();
            urlConnection.setRequestMethod("GET");
            int statusCode = urlConnection.getResponseCode();
            if (statusCode != HttpStatus.SC_OK) {
                return null;
            }
            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream != null) {
                File storageDir;
                String mediatype = null;
                String audiofmt = null;
                mediatype = "Profile";
                audiofmt = ".jpg";
                if (Environment.getExternalStorageState() == null || Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {
                    storageDir = new File(Environment.getDataDirectory(), mediatype);
                } else {
                    storageDir = new File(Environment.getExternalStorageDirectory() + Common.STORAGE_PATH, mediatype);
                }
                if (!storageDir.exists() && !storageDir.mkdirs()) {
                    // For HTC Device                    storageDir = new File(Common.STORAGE_PATH, mediatype);
                    if (!storageDir.exists() && !storageDir.mkdirs()) {
                    }
                }
                File image = File.createTempFile(
                        Memberid,  /* prefix */                        audiofmt,         /* suffix */                        storageDir      /* directory */                );
                FileOutputStream f = new FileOutputStream(image);
                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = inputStream.read(buffer)) > 0) {
                    f.write(buffer, 0, len1);
                }
                f.close();
                resp=image.getAbsolutePath();                
               
                return resp;
            }
        } catch (Exception e) {
            Log.d("URLCONNECTIONERROR", e.toString());
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return resp;
    }
}




 

Thursday, 25 February 2016

Deleting files from a folder - android

Very simple delete the file from folder for particular format

File dir = new File(Environment.getExternalStorageDirectory() + "/Pictures/.Twogether/Videos");
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                if (new File(dir, children[i]).getAbsolutePath().endsWith("3gp2")) {
                    new File(dir, children[i]).delete();
                }
            }
        }

Thursday, 19 November 2015

How to create custom font for Text View

Hi guys i am going to explain custom font for TextView

First We have create assets directory inside the main directory and then create fonts directory inside the assets main->assets->font->  then put the font file inside the font directory

Then create the below file inside any one package. The below file created in com.crm.common package

package com.crm.Common;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

/** * Created by Ashok on 11/19/2015. */public class CustomReqularTextView extends TextView {
    public static Typeface FONT_NAME;


    public CustomReqularTextView(Context context) {
        super(context);
        if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular_0.ttf");
        this.setTypeface(FONT_NAME);
    }
    public CustomReqularTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular_0.ttf");
        this.setTypeface(FONT_NAME);
    }
    public CustomReqularTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular_0.ttf");
        this.setTypeface(FONT_NAME);
    }
}


Then We have to use like

<com.crm.Common.CustomBoldTextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/compid"    android:text="@string/compname"    android:layout_gravity="center_horizontal"/>