Skip to main content

Android app main activity Async Task

Hi Friend's recently i have created android application for election management(Voter search and other statics and reports as well as Messaging voter details).

I have developed this application for one of my client , Application requirement was storing almost 3 To 4 Lakh Record's in local phone memory without using internet and this was biggest challenge  for bcz I have not studied android and this was my first app in android .

I'm Posting some codes which may help you ............


 MainActivity.java
This is my main activity code  which create Database and do AsynTask like showing progress bar.........


package com.example.rajmal.dummy;


import java.io.IOException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.database.sqlite.SQLiteDatabase;


@SuppressLint({ "ParserError", "ParserError" })
public class mainactivity extends Activity implements OnClickListener  {
protected Button btnmarathi;
protected Button btnenglish;
protected Button btnSendMessage;
Context context;
private int mProgressStatus = 0;
private ProgressDialog mProgressDialog ;
private ProgressBarAsync mProgressbarAsync;
protected SQLiteDatabase db,db1,db2,db3,db4,db5,db6;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmarathi);
      //  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mProgressDialog = new ProgressDialog(this);
       
        /** Close the dialog window on pressing back button */
        mProgressDialog.setCancelable(false);

        /** Setting a horizontal style progress bar */
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        /** Setting a message for this progress dialog
        * Use the method setTitle(), for setting a title
        * for the dialog window
        *  */
        mProgressDialog.setMessage("Initializing Database .....");
        mProgressDialog.show();
       
        /** Creating an instance of ProgressBarAsync */
        mProgressbarAsync = new ProgressBarAsync();

        /** ProgressBar starts its execution */
        mProgressbarAsync.execute();
       
     
   
       btnmarathi = (Button) findViewById(R.id.btnmarathi);
       btnenglish = (Button) findViewById(R.id.btnEnglish);
       btnSendMessage=(Button)findViewById(R.id.btnsendmsg);

       
    }
  public void CreateDb()
  {
 db = (new DatabaseHelper(this)).getWritableDatabase();
      db1 = (new DatabaseHelper1(this)).getWritableDatabase();
     db2 = (new DatabaseHelper2(this)).getWritableDatabase();
      db3 = (new DatabaseHelper3(this)).getWritableDatabase();
      db4 = (new DatabaseHelper4(this)).getWritableDatabase();
      db5 = (new DatabaseHelper5(this)).getWritableDatabase();
      db6 = (new DatabaseHelper6(this)).getWritableDatabase();
  }
public void handleClick(View v){
       
        //Create an intent to start the new activity.
           // Our intention is to start secondActivity
    switch(v.getId())
    {
    case R.id.btnmarathi:
        Intent intent = new Intent();
        intent.setClass(this,Menuclick.class);
       // db2 = (new DatabaseHelper2(this)).getWritableDatabase();
        startActivity(intent);
    break;
       case R.id.btnEnglish:
    Intent intent1 = new Intent();
           intent1.setClass(this,Menuclick.class);
        //   db3 = (new DatabaseHelper3(this)).getWritableDatabase();
           startActivity(intent1);
           break;
       case R.id.btnsendmsg:
    Intent intent2 = new Intent();
           intent2.setClass(this,SendSMSActivity.class);
           startActivity(intent2);
           break;
   
    }
       }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.layout.mainmarathi, menu);
        return true;
    }

@Override
public void onClick(View v) {
//  Intent myintent = new Intent(mainactivity.this, EmployeeList.class);
  // startActivity(myintent);
}

   

private class ProgressBarAsync extends AsyncTask<Void, Integer, Void>{

    /** This callback method is invoked, before starting the background process */
    @Override
        protected void onPreExecute() {
   
        super.onPreExecute();
       // mProgressStatus = 0;
    }

    /** This callback method is invoked on calling execute() method
    * on an instance of this class */
    @Override
    protected Void doInBackground(Void...params) {
    CreateDb();
return null;
     
        //return null;
    }

    /** This callback method is invoked when publishProgress()
    * method is called */
 
   /* protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        mProgressDialog.setProgress(mProgressStatus);
    }*/

    /** This callback method is invoked when the background function
    * doInBackground() is executed completely */
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        mProgressDialog.dismiss();
    }
}
}


Comments

Popular posts from this blog

Upload Photo to server using Android and .Net

In this example with image we are passing some string also like name and description as well as we have Radio button also which is use too map different upload location . Step by step:- 1>                                                  First will be design which is our XML file name photo_upload.xml <? xml version = "1.0" encoding = "utf-8" ?> < ScrollView xmlns:android = "http://schemas.android.com/apk/res/android"     android:layout_width = "fill_parent"     android:layout_height = "fill_parent"      android:orientation = "vertical"     android:fillViewport = "true" >     < RelativeLayout      ...

PHP Code to Connect Mysql Database

Php mysql connectivity code............. Write below code in connect.php file <?Php ob_start(); $host="localhost:3307"; // Host name $username="root"; // Mysql username $password="sa"; // Mysql password $db_name="dbname"; // Database name $tbl_name="login"; // Table name ?> include your connect file in page include ("config.php"); mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); execute your query $quer2=mysql_query("SELECT * FROM village"); $noticia2 = mysql_fetch_array($quer2); and that's it ..... www.indbricks.com

Where is Java being Used?

The programming language Java was developed by Sun Microsystems in the year 1995. Earlier, it was only used to design and program small computing devices but later adopted as one of the platform independent programming language. The most important feature of Java is its byte code that can be interpreted on any platform including windows, Linux etc. One can also download it freely from the official website of Sun. As we have mentioned above that java-programming language was only developed for the small devices but now it can be found in a variety of devices like cell phones, e-commerce application, PCs and almost all network or computing devices. Java is available in different form: JSP – Like PHP and ASP, Java Server Pages based on a code with normal HTML tags, which helps in creating dynamic web pages. Java Applets – This is another type of Java program that used within a web page to add many new features to a web browser. These are small program used in the progr...