Wednesday, 6 August 2014

Android Database എങ്ങനെയുണ്ടാക്കാം

അന്ദ്രോയിദ് database പല രീതിയില്‍ ഉണ്ടാക്കാം
openOrCreateDatabase വെച്ചും extends SQLiteOpenHelper ക്ലാസ്സു വെച്ചും നിര്‍മിക്കാം
 ഞാനിവിടെ പെട്ടന്ന്അല്ലെങ്കില്‍ എളുപ്പത്തില്‍ എങ്ങനെ ഒരു database ഉണ്ടാക്കാമെന്ന് പറയാം

ഞാനിവിടെ ഉപയോഗിക്കുന്ന ത് openOrCreateDatabase കൊണ്ടുള്ള രീതിയാണ്‌

അന്ദ്രോയിദ് database ഉണ്ടാക്കുവാന്‍ ഒരറ്റ കമന്റ്‌ മതി
പക്ഷെ സാധാരണ അന്ദ്രോയിദ് database ഉണ്ടാക്കുവാന്‍ ഉപയോഗിക്കുന്ന രീതി ഇതല്ലെങ്കിലും ഇത് ഉപയോഗിച്ച് ഞാനുണ്ടാക്കിയ ആപ്പ് പ്രശനമില്ലാതെ തന്നെ വര്‍ക്ക്‌ ചെയ്യുന്നുണ്ട്
ഒന്നാമതായി database name കൊടുത്തു database ഉണ്ടാക്കാം
അതിനു സഹായിക്കുന്ന ഒരു ക്ലാസ്സാണ് SQLiteDatabase

step 1.create Database

SQLiteDatabase db;
db=openOrCreateDatabase("Home_DB",MODE_PRIVATE,null);

അടുത്തത് ഒരു databasil ഒരുപാട് tables ഉണ്ടാകും ഇതേ databasil ഒരു table ഉണ്ടാക്കുവാന്‍ ചെയ്യാന്‍ വേണ്ട കോഡ് ആണ്

Step 2. create a table

db.execSQL("create table if not exists personal_tb(_id INTEGER PRIMARY KEY AUTOINCREMENT,age INTEGER,weight INTEGER,height INTEGER,sex VARCHAR(50))");


Step 3.Finish

ഇനി എങ്ങനെ ഒരു databasil data insert ചെയ്യാമെന്ന് നോക്കാം

Step 4.inserting data to table

db=openOrCreateDatabase("Home_DB",MODE_PRIVATE,null);

db.execSQL("create table if not exists personal_tb(_id INTEGER PRIMARY KEY AUTOINCREMENT,age INTEGER,weight INTEGER,height INTEGER,sex VARCHAR(50))");

db.execSQL("insert into personal_tb(age,weight,height,sex) values ('"+age+"','"+Weight+"','"+Height+"','"+Sex+"')");

ഇനി എങ്ങനെ database display ചെയ്യാമെന്ന് നോക്കാം

Step 5 Display Databse

db=openOrCreateDatabase("MedicineDB",MODE_PRIVATE,null);

db.execSQL("create table if not exists personal_tb(age INTEGER,weight INTEGER,height INTEGER,sex VARCHAR(50))");
 String age=t1_Age;
 String age= t1_Height;
String age= t1_Weigh;
String age=t1_sex;
 Cursor cursor = db.rawQuery("SELECT * from personal_tb", null);
 
while(cursor.moveToNext()){
t1_Age=cursor.getString(0);
 t1_Height=cursor.getString(1);
 t1_Weight=cursor.getString(2);
 t1_sex=cursor.getString(3);
        }

Step 6 Delete a 10th row from database
int i=10;
db.execSQL("DELETE FROM personal_tb WHERE _id ='"+i+"'");

facebook whatsapp notification എങ്ങനെയുണ്ടാക്കാമെന്ന് നോക്കാം( in android)


firstly നമുക്ക് ഒരു activity ഉണ്ടാക്കണം

ActivityNotification.java
-------------------------------
notification ഉണ്ടാക്കാന്‍ വേണ്ട രണ്ടു മഹാ സംഭവങ്ങളാണ് :p
1.Notification
2.NotificationManager

ഈ രണ്ടു ക്ലാസ്സുകളും വെച്ച് രണ്ടു object ആദ്യം ഉണ്ടാക്കണം

private Notification notificationobj;
private NotificationManager notificationmngr_obj;

ആദ്യം notification service ആക്റ്റീവ് ആക്കണം അതിനു വേണ്ടി യുള്ള കോഡ് ആണ്
notificationmngr_obj=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

ഇനി notification കൊടുക്കുമ്പോള്‍ വേണ്ട കാര്യങ്ങള്‍ പറഞ്ഞു കൊടുക്കാം
അതിനു notification initialize ചെയ്യണം
അതിനു വേണ്ടി
notificationobj=new Notification(R.drawable.ic_launcher, "hello", 0);

ഇതില്‍ കൊടുത്ത ഭാഗങ്ങള്‍ ഓരോന്നായി പഠിക്കാം

Notification(icon, tickerText, when)
--------------------------------------
notification നില്‍ നമുക്ക് ഒരു icon സെറ്റ് ചെയ്യാം
notification നില്‍ tickerText അതായത് ഒരു heading കൊടുക്കാം
notification നില്‍  when അതായത് എപ്പോള്‍ notification വേണമെന്ന് പറഞ്ഞു കൊടുക്കാം ഞാന്‍ ഇവിടെ 0(പൂജ്യം)ആണ് കൊടുക്കുന്നത് അതായത് 0second ആകുമ്പോള്‍ notification കിട്ടും

ഇത് കൂടാതെ notification നമ്പറും കൂടെ കൊടുക്കാം

notificationobj.number=10;

Intent intent=new Intent(context,Activity2.class);
PendingIntent pIntent=PendingIntent.getActivity(context, 0, intent, 0);
ഈ രണ്ടു ക്ലാസുകള്‍ Intent ഉം PendingIntent ഉം
ഉപയോഗിക്കാം Intent എന്താണെന്ന്  മുന്നേ പറഞ്ഞിട്ടുണ്ട് ആവശ്യമുള്ളവര്‍ ഇവിടെ ക്ലിക്ക് ചെയ്യുക
PendingIntent എന്താണെന്നു അടുത്ത പോസ്റ്റില്‍ പറയാം

ഇനി വേണ്ടത് ഈ കൊടുത്തിരിക്കുന്ന ഇന്‍ഫര്‍മേഷന്‍ തമ്മില്‍ connect ചെയ്യാം

notification heading
String hello="hello";
String msg="mamstricks.com";

notificationobj.setLatestEventInfo(context, hello, msg, pIntent);

ഇനി NotificationManager വെച്ച് notification വിളിക്കാം

notificationmngr_obj.notify(0, notificationobj);

ഇനി notify കൊടുത്ത 0 എന്താണെന്ന് പറയാം അതാണ്‌ notification id ഇത് വെച്ച് വേണമെങ്കില്‍ notification cancel ചെയ്യാം അല്ലെങ്കില്‍ destroy ചെയ്യാം
notify(0, notificationobj);
 ഇനി notificationഇല്‍ ക്ലിക്ക് ചെയ്താല്‍ അത് cancel ചെയ്യുവാനും തിരിച്ചു ആപില്‍ എത്തുവാനും ഈ കോഡ് ഉപയോഗിക്കാം

 notificationobj.flags |= Notification.FLAG_AUTO_CANCEL;

full code
******************************************

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class ActivityNotificationextends Activity {

private Notification notificationobj;
private NotificationManager notificationmngr_obj;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
context=this;
}


public void viewNotify(View v){
String hello="hello";
String msg="messsage";
Intent intent=new Intent(context,ActivityNotification.class);
PendingIntent pIntent=PendingIntent.getActivity(context, 0, intent, 0);
notificationmngr_obj=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


notificationobj=new Notification(R.drawable.ic_launcher, "hellook", 0);

notificationobj.number=10;
notificationobj.setLatestEventInfo(context, hello, msg, pIntent);


 
   notificationobj.flags |= Notification.FLAG_AUTO_CANCEL;

  notificationmngr_obj.notify(0, notificationobj);
 
}
}
***********************************************************************
മറ്റു രീതികല്‍  കൂടെ നോക്കാം
ഇവിടെ കൊടുത്ത കോഡ്
എങ്ങനെ പല രീതിയില്‍ ഉപയോഗിക്കാമെന്ന് നോക്കാം
1.
/**************/
notificationobj=new Notification(R.drawable.ic_launcher, "hellook", 0);

notificationobj.number=10;
notificationobj.setLatestEventInfo(context, hello, msg, pIntent);
/******************/
2.
/**************/
Notification notificationobj= new Notification.Builder(this)
       .setContentTitle("my title " + "mamstricks")
       .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
       .setContentIntent(pIntent)
       .addAction(R.drawable.ic_launcher, "ok", pIntent)
       .addAction(R.drawable.ic_launcher, "more info", pIntent)
       .addAction(R.drawable.ic_launcher, "And more info", pIntent).build();
   noti.number=10;
/**************/
3.
/**************/
notificationobj=new Notification();

notificationobj.icon=R.drawable.ic_launcher;
notificationobj.tickerText="hellook";
notificationobj.number=10;
notificationobj.setLatestEventInfo(context, hello, msg, pIntent);
/**************/


facebook whatsapp notification എങ്ങനെയുണ്ടാക്കാമെന്ന് നോക്കാം( in android)


firstly നമുക്ക് ഒരു activity ഉണ്ടാക്കണം

ActivityNotification.java
-------------------------------
notification ഉണ്ടാക്കാന്‍ വേണ്ട രണ്ടു മഹാ സംഭവങ്ങളാണ് :p
1.Notification
2.NotificationManager

ഈ രണ്ടു ക്ലാസ്സുകളും വെച്ച് രണ്ടു object ആദ്യം ഉണ്ടാക്കണം

private Notification notificationobj;
private NotificationManager notificationmngr_obj;

ആദ്യം notification service ആക്റ്റീവ് ആക്കണം അതിനു വേണ്ടി യുള്ള കോഡ് ആണ്
notificationmngr_obj=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

ഇനി notification കൊടുക്കുമ്പോള്‍ വേണ്ട കാര്യങ്ങള്‍ പറഞ്ഞു കൊടുക്കാം
അതിനു notification initialize ചെയ്യണം
അതിനു വേണ്ടി
notificationobj=new Notification(R.drawable.ic_launcher, "hello", 0);

ഇതില്‍ കൊടുത്ത ഭാഗങ്ങള്‍ ഓരോന്നായി പഠിക്കാം

Notification(icon, tickerText, when)
--------------------------------------
notification നില്‍ നമുക്ക് ഒരു icon സെറ്റ് ചെയ്യാം
notification നില്‍ tickerText അതായത് ഒരു heading കൊടുക്കാം
notification നില്‍  when അതായത് എപ്പോള്‍ notification വേണമെന്ന് പറഞ്ഞു കൊടുക്കാം ഞാന്‍ ഇവിടെ 0(പൂജ്യം)ആണ് കൊടുക്കുന്നത് അതായത് 0second ആകുമ്പോള്‍ notification കിട്ടും

ഇത് കൂടാതെ notification നമ്പറും കൂടെ കൊടുക്കാം

notificationobj.number=10;

Intent intent=new Intent(context,Activity2.class);
PendingIntent pIntent=PendingIntent.getActivity(context, 0, intent, 0);
ഈ രണ്ടു ക്ലാസുകള്‍ Intent ഉം PendingIntent ഉം
ഉപയോഗിക്കാം Intent എന്താണെന്ന്  മുന്നേ പറഞ്ഞിട്ടുണ്ട് ആവശ്യമുള്ളവര്‍ ഇവിടെ ക്ലിക്ക് ചെയ്യുക
PendingIntent എന്താണെന്നു അടുത്ത പോസ്റ്റില്‍ പറയാം

ഇനി വേണ്ടത് ഈ കൊടുത്തിരിക്കുന്ന ഇന്‍ഫര്‍മേഷന്‍ തമ്മില്‍ connect ചെയ്യാം

notification heading
String hello="hello";
String msg="mamstricks.com";

notificationobj.setLatestEventInfo(context, hello, msg, pIntent);

ഇനി NotificationManager വെച്ച് notification വിളിക്കാം

notificationmngr_obj.notify(0, notificationobj);

ഇനി notify കൊടുത്ത 0 എന്താണെന്ന് പറയാം അതാണ്‌ notification id ഇത് വെച്ച് വേണമെങ്കില്‍ notification cancel ചെയ്യാം അല്ലെങ്കില്‍ destroy ചെയ്യാം
notify(0, notificationobj);
 ഇനി notificationഇല്‍ ക്ലിക്ക് ചെയ്താല്‍ അത് cancel ചെയ്യുവാനും തിരിച്ചു ആപില്‍ എത്തുവാനും ഈ കോഡ് ഉപയോഗിക്കാം

 notificationobj.flags |= Notification.FLAG_AUTO_CANCEL;

full code
******************************************

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class Activity2 extends Activity {

private Notification notificationobj;
private NotificationManager notificationmngr_obj;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
context=this;
}


public void viewNotify(View v){
String hello="hello";
String msg="messsage";
Intent intent=new Intent(context,Activity2.class);
PendingIntent pIntent=PendingIntent.getActivity(context, 0, intent, 0);
notificationmngr_obj=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


notificationobj=new Notification(R.drawable.ic_launcher, "hellook", 0);

notificationobj.number=10;
notificationobj.setLatestEventInfo(context, hello, msg, pIntent);


 
   notificationobj.flags |= Notification.FLAG_AUTO_CANCEL;

  notificationmngr_obj.notify(0, notificationobj);
 
}
}
***********************************************************************
മറ്റു രീതികല്‍  കൂടെ നോക്കാം
ഇവിടെ കൊടുത്ത കോഡ്
എങ്ങനെ പല രീതിയില്‍ ഉപയോഗിക്കാമെന്ന് നോക്കാം
1.
/**************/
notificationobj=new Notification(R.drawable.ic_launcher, "hellook", 0);

notificationobj.number=10;
notificationobj.setLatestEventInfo(context, hello, msg, pIntent);
/******************/
2.
/**************/
Notification notificationobj= new Notification.Builder(this)
       .setContentTitle("my title " + "mamstricks")
       .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
       .setContentIntent(pIntent)
       .addAction(R.drawable.ic_launcher, "ok", pIntent)
       .addAction(R.drawable.ic_launcher, "more info", pIntent)
       .addAction(R.drawable.ic_launcher, "And more info", pIntent).build();
   noti.number=10;
/**************/
3.
/**************/
notificationobj=new Notification();

notificationobj.icon=R.drawable.ic_launcher;
notificationobj.tickerText="hellook";
notificationobj.number=10;
notificationobj.setLatestEventInfo(context, hello, msg, pIntent);
/**************/


എങ്ങനെ ആണ്ട്രോയിട് sharepreference ഉപയോഗിച്ച് data സ്റ്റോര്‍ ചെയ്യാം


ഞാന്‍ രണ്ടു function ഉസ് ചെയ്തു കാണിക്കാം
ഒന്നാമത്തെ functionil data സ്റ്റോര്‍ ചെയ്തും രണ്ടാമത്തെ functionil data retrieve ചെയ്യാം

int k=1;notificationCount=1;
SharedPreferences pref;

First function on button click
**********************************
public void getDataa(View v){
k++;

notificationCount++;
pref = getApplicationContext().getSharedPreferences("MyPrefmams", MODE_PRIVATE); 
Editor editor = pref.edit();
editor.putInt("notificationCountkey", notificationCount);
editor.commit(); 
   Log.d("getdata_c_k"," "+notificationCount+" "+k);
}


 Second function with button click
*************************************************
public void getDat(View v){
pref=getSharedPreferences("MyPrefmams",this.MODE_PRIVATE);
notificationCount =pref.getInt("notificationCountkey", 0);
   Log.d("getdata_c_k"," "+notificationCount+" "+k);
}