Open top menu






In this tutorial explain how to Create / Delete Multiple text file Inside Root Folder  and every text file Size is 5Mb .

private File createEVENTFileWriter() {

    File filelog = null;

    try {

        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/" + log_foldereventname);
        myDir.mkdirs();
        String h = DateFormat.format("MM-dd-yyyyy ss",System.currentTimeMillis()).toString();
        filelog = new File(myDir, log_filenameevent);
        filelog.createNewFile();
        long fileSizeInBytes = filelog.length();
        long fileSizeInKB = fileSizeInBytes / 1024;

        if (fileSizeInKB > 5000) {

            File from = new File(myDir, log_filenameevent);
            File to = new File(myDir, h + log_filenameevent);
            if (from.exists()) {
                from.renameTo(to);

            }
        }

    } catch (Exception err) {
        fileWriteCreationFailed = true;
        err.printStackTrace();
    }
    return filelog;

}
 

public void DeletOlderFolder() {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir =
new File(root + "/" + log_foldereventname);
    myDir.mkdirs();
    File list[] = myDir.listFiles();
   
for (int i = 0; i < list.length; i++) {
       
long diff = new Date().getTime() - list[i].getAbsoluteFile().lastModified();
       
if (diff >  3 * 24 * 60 * 60 * 1000) {//3 * 24 * 60 * 60 * 1000(3days)
           
list[i].getAbsoluteFile().delete();
        }
    }
Read more




Explain- The Log file delete alternate in  3 days.


import android.content.Context;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;


public class LogFile {

    Context context;

     public LogFile ( ) {

        DeletOlderFolder();
    }

    public void DeletOlderFolder(){

        File file=new File("sdcard/LoungeLogFile.txt");

        if(file != null) {

            long diff = new Date().getTime() - file.lastModified();

            if (diff > 3 * 24 * 60 * 60 * 1000) {

                file.delete();

            }

        }

    }

}
 
 

public class MainActivity extends Activity  {
 
@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
 
 
new LogFile (); // Delete Log file in 3 days
 
 
}
}


Read more


Explain-  write Log file programmatically in android.


 
import android.content.Context;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;


public class LogClass {
    Context context;

    public LogClass(Context context, String text) {
       this.context=context;
        LogFile(text);
        DeletOlderFolder();
    }

    public  void LogFile( String text){

            File logFile = new File("sdcard/LoungeLogFile.txt");
            if (!logFile.exists()) {
                try {
                    logFile.createNewFile();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            try {
                BufferedWriter buf = new BufferedWriter(

                                    new FileWriter(logFile, true));
                buf.append(text);
                buf.newLine();
                buf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

    }
 }
Read more



How to solve SSL.SSLHandshakeException in Android?

Basically ,SSL usage Scenario, a server configurd  with a certificate containing a public key as well as a Matching private key.As part of handshake between an SLL client and server.

TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
     
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
         
return null;
      }
     
public void checkClientTrusted(X509Certificate[] certs, String authType) {
      }
     
public void checkServerTrusted(X509Certificate[] certs, String authType) {
      }
  } };
 
final SSLContext sc = SSLContext.getInstance("SSL");
  sc.init(
null, trustAllCerts, new java.security.SecureRandom());
  HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
 
// Create all-trusting host name verifier
 
HostnameVerifier allHostsValid = new HostnameVerifier() {
     
public boolean verify(String hostname, SSLSession session) {
         
return true;
      }
  };
 
// Install the all-trusting host verifier
 
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
  URL url =
new URL(aurl[0]);
//  HttpsURLConnection connection = (HttpsURLConnection) (url.openConnection());
 
URLConnection connection = url.openConnection();
  connection.setRequestProperty(
"connection", "close");
  connection.connect();
  


Try this, 
Read more


Example:- Create Multiple EditText And Get editText String programmatically.


In this tutorial explain how to implement EditText in loop and Set Empty Validation, get EditText String programmatical.



public class ServiceTypeDetailActivity extends Activity implements View.OnClickListener{

private List editTexts = new ArrayList();
ArrayList enterItemArray = new ArrayList<>();

public static ArrayList ServiceEditParameters= ArrayList();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_servicetypedetail);

LinearLayout Create_ServiceDeatils_layout = 
(LinearLayout) findViewById(R.id.Create_ServiceDeatils_layout);


lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
ViewGroup.LayoutParams.WRAP_CONTENT);
lparams.setMargins(5, 7, 5, 7);

for (int ed_txt = 0; ed_txt < ServiceTypeActivity.ServiceParameters.size(); ed_txt++) {
    ServiceParametersDetail item = ServiceTypeActivity.ServiceParameters.get(ed_txt);
    TextInputLayout inputLayout = new TextInputLayout(this);
    EditText ed = new EditText(this);
    ed.setId(ed_txt);
    ed.setHint(item.getPName());
    ed.setLayoutParams(lparams);
    ed.setPadding(7, 0, 2, 0);
    if (item.getPDataType().equalsIgnoreCase("String")) {
        ed.setInputType(InputType.TYPE_CLASS_TEXT);
    } else {
        ed.setInputType(InputType.TYPE_CLASS_NUMBER);
    }
    inputLayout.addView(ed);
    editTexts.add(ed);
    Create_ServiceDeatils_layout.addView(inputLayout);
}
}
@Overridepublic void onClick(View v) {
    switch (v.getId()) {
       
        case R.id.btn_calculate:
            try {
                enterItemArray.clear();
                String[] items = new String[editTexts.size()];
                for (int get = 0; get < editTexts.size(); get++) {
                    if (editTexts.get(get).getText().toString().equals("")) {
                        ServiceParametersDetail item = ServiceTypeActivity
.ServiceParameters.get(get);
                        AlertDialog("PLease Enter Item .");
                        enterItemArray.clear();
                        break;
                    }
                    enterItemArray.add(editTexts.get(get).getText().toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
}






Read more