Saturday, June 4, 2016

Store Launching Time (Timestamp) in SQLite DataBase

"Jai Saraswati Maa"

Dear All Today i am loading a superb example of storing launch time of your app.

Special Thanks Monu Kumar Jangir

We are using Android Studio 1.5.1 with API 23
Add permission in manifest file like
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

A. Output will be like this

B. XML file activity_main.xml is as
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.example.student.timestamp.MainActivity">

    <TextView        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceLarge"

        android:id="@+id/textView"

        android:layout_centerVertical="true"

        android:layout_centerHorizontal="true" />
</RelativeLayout>

C. Java Files are as 

1. MainActivity.java is as 

package com.example.student.timestamp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    String str1;
    int second,minute, hour;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView)findViewById(R.id.textView);
        Calendar c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR);
        second = c.get(Calendar.SECOND);
        minute = c.get(Calendar.MINUTE);

        textView.setText("Your App Launched at \n   " +
                                  hour + ":" + minute + ":" + second);

        str1=textView.getText().toString();
        MyHelper myhlp=new MyHelper(this);
        myhlp.putInfo(myhlp,str1);
        Toast.makeText(this, "Record Saved", Toast.LENGTH_SHORT).show();


    }
}

2. MyHelper.java is as 

package com.example.student.timestamp;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyHelper extends SQLiteOpenHelper {

    private  static final String  CREATE_QUERY = " create table " +
               UserInfo.TAB_NAME + " ( " + UserInfo.TIME_STAMP + " timestamp );";

    public MyHelper(Context c){
        super(c,UserInfo.DB_NAME,null,UserInfo.DB_VER);
    }

    @Override    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_QUERY);

    }

    @Override

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(" drop table  if exists " + UserInfo.TAB_NAME);
    }
    public void putInfo(MyHelper mob,String t_stamp)
    {
        SQLiteDatabase sb = mob.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(UserInfo.TIME_STAMP,t_stamp);
        sb.insert(UserInfo.TAB_NAME, null, cv);

    }
}
 
3. UserInfo.java is as 

package com.example.student.timestamp;

import android.support.v7.app.AppCompatActivity;

public class  UserInfo extends AppCompatActivity {

    public static final int DB_VER = 4;
    public static final String TIME_STAMP = "t_stamp";
    public static final String DB_NAME = "my_db2";
    public static final String TAB_NAME = "user_tab";




    }


No comments:

Post a Comment