Notification in Android App (in tamil)

456 views

Published on

Dr. Ramkumar Lakshminarayanan article published in Tamil Computer Magazine. The content is about creating Android Application notification.

Published in: Mobile
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
456
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
35
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Notification in Android App (in tamil)

  1. 1. ðô Å›G¬ôèO™ ðò÷£˜èÀ‚° îèõ™ è¬÷ ªîKMŠð Notification º¬ø ðò¡ð´Aø¶. «ï£†®çH«èê¬ù Í¡Á º¬øèO™ ݇†ó£Œ®™ à¼õ£‚è º®»‹. 1. «ì£v† «ï£†®çH«èê¡ (Toast Notifi cation) 2. v«ì†ìv 𣘠«ï£†®çH«èê¡ (Status Bar Notification) 3. ìòô£‚ «ï£†® çH«èê¡ (Dialog Notification) «ì£v† «ï£†®çH«èê¡ (Toast Notification) «ì£v† «ï£†® çH«èê¡ ðò¡ð£† ®™ àœ÷ F¬óJ™ «î£¡Á‹. ªð¼‹ð£ ½‹ å¼ ªêò™ ï¬ì ªðŸÁœ÷¶ â¡ ðî¬ù ðòù£÷˜ èÀ‚° ªîKMŠð «ì£v† ðò¡ð´A ø¶. ꣡ø£è Üô£˜‹ «ïóˆF¬ù ñ£ŸPò ¾ì¡ «ïó‹ ñ£P»œ÷¶ â¡ðî¬ùˆ ªîK MŠð‹, îèõ™ «ì†ì£«ðC™ «êI‚ èŠð´œ÷¶ â¡ð î¬ùˆ ªîKMŠðîŸ °‹ ðò¡ð´Aø¶. v«ì†ìv 𣘠«ï£†®çH«èê¡ (Status Bar Notifica- tion) v«ì†ìv 𣘠«ï£†®H«èê¡ÜŠO «èê¡ «ñ™ð°FJ™ «î£¡Á‹. ꣡ø£è, âvâ‹âv õ‰¶œ÷¶ â¡ð¬î ªîKMˆ¶ ܉î îèõ¬ô «î˜‰ªî´ˆî£™ âvâ‹âv J¬ù ð®‚èô£‹. ¹Fò ÜŠO«èê¡ GÁ¾Al˜ èœ, GÁMò¾ì¡ v«ì†ìv«ï£†® H«èê¡ Íô‹ GÁ¾ î™ º®‰¶ M†ì¶ â¡ðî¬ù ªîKòŠ ð´ˆîô£‹. ܉î v«ì†ìv 𣘠«ï£†®H«èê¬ù AO‚ ªêŒ¶ 𣘈 ðòù£÷¼‚° â¡ù ï¬ì ªðŸÁœ÷¶ â¡ø îèõ™ «î£¡Áñ£Á à¼õ£‚°õ v«ì†ìv 𣘠«ï£†® H«èê¡ ðò¡ð´ Aø¶. îèõ™ ªîKM‚°‹ º¬ø ìòô£‚ «ï£†®çH«èê¡ (Dialog Notification) ªêò™ð†´ ªè£‡®¼‚°‹ Ý‚®M† ®‚° (Activity) «ñ«ô îèõ¬ôˆ ªîKMˆ¶, H¡ù˜ ܉î F¬óJ™ «î˜‰ªî´‚°‹ àœ k†®Ÿ° ãŸð ªêò™è¬÷ ñ£ŸP ܬñ‚è ìòô£è «ï£†®çH«èê¡ ðò¡ð´Aø¶. ꣡ ø£è å¼ F¬óJL¼‰¶ ñŸªø£¼ F¬ó‚° ªê™õ ðòù£÷¼‚° æ.«è. ñŸÁ‹ «è¡ê™ â¡ø «î˜M¬ù ÜOˆ¶, æ.«è. ªè£´ˆî£™ Ü´ˆî F¬ó‚° ªê™ô «õ‡´‹, «è¡ê™ ªè£´ˆî£™ Ü«î F¬óJ™ Þ¼‚è «õ‡´‹ «ð£¡ø ªêò™ð£†®¬ù à¼õ£‚è ìòô£‚ «ï£†®çH«èê¡ ðò¡ð´Aø¶. «ì£v† «ï£†®çH«èê¬ù ݇†ó£Œ´ ÜŠO‚«èêQ™ ðò¡ð´ˆ¶õ «ì£v† ÝŠªü‚† ðò¡ð´Aø¶. îèõ™è¬÷ F¬ó J™ 裇ð makeText() ªñˆî®Ÿ° Í¡Á Mîñ£ù îèõ™è¬÷ ªè£´‚è «õ‡´‹. ܬõ â‰î F¬ó, â¡ù îèõ™ ñŸÁ‹ âš õ÷¾ «ïó‹ â¡ðùõŸ¬ø ªè£´‚è «õ‡ ´‹. «ì£v†®¬ù ÜŠO‚«èêQ™ 裇 HŠð show() ªñˆî† ðò¡ð´Aø¶. ꣡ ø£è, (Gó™-1) ðì‹-1 ðì‹-2 ðì‹-3 Gó™-1 Gó™-2 Gó™-3
  2. 2. Toast F¬óJ¡ â‰îŠ ð°FJ™ «î£¡ø «õ‡´‹ â¡ðî¬ù °PŠð setGravity() ªñˆî† ðò¡ð´Aø¶. ꣡ø£è, toast.setGravity(Gravity.Top,o,o,); Ü™ô¶ toast.setGravity(Gravity.LEFT,O,O); Ü´ˆ¶ «ï£†®çH«èê¬ù, v«ì†ìú ð£K™ à¼õ£‚°‹ º¬øJ¬ù 𣘊«ð£‹. ¹Fò Šó£ªü‚† å¡P¬ù à¼õ£‚°«õ£‹. Project Name â¡ðî¬ù Notification Manager âù ¬õˆ¶‚ ªè£œ«õ£‹. Ý‚®M†® ªðòK¬ù Create Notification âùŠ ªðòK´ «õ£‹. ÜF™ mail.xml -Ÿè£ù «è£´ (code) Gó™ 2-™ ªè£´‚èŠð†´œ÷¶. Ü´ˆîî£è result.xml â¡ø ç¬ðL¬ù Gó™ 3-™ àœ÷¶ «ð£¡Á à¼õ£‚è «õ‡´‹. result.xml ‚è£ù Activity Notification Receiver â¡ð‹. ÜF™ Ý¡†ó£Œ´ -2 Þ󣋰ñ£˜ ªô†²Iï£ó£òí¡ 06 import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; public class CreateNotification extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void createNotification(View view) { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.icon, "A new notification", System.currentTimeMillis()); // Hide the notification after its selected notification.flags |= Notification.FLAG_AUTO_CANCEL; Intent intent = new Intent(this, NotificationReceiver.class); PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0); notification.setLatestEventInfo(this, "This is the title", "This is the text", activity); notification.number += 1; notificationManager.notify(0, notification); <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/ apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button1" android:text="@string/btn_name"></Button> </LinearLayout> Gó™-4 Gó™-5 Gó™-6
  3. 3. package com.ram.dialogex; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.*; public class DialogExActivity extends Activity { /** Called when the activity is first created. */ Button b1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); b1 = (Button)findViewById(R.id.button1); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialog alertDialog = new AlertDialog.Builder(DialogExActivity.this).creat e(); alertDialog.setTitle("Reset..."); alertDialog.setMessage("R u sure?"); alertDialog.setIcon(R.drawable.icon); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //here you can add functions } }); alertDialog.show(); } }) ; } } ðì‹-4 ðì‹-5 Gó™-7 Gó™-8 Gó™ A™ àœ÷¶ «ð£¡ø Š«ó£Aó£ I¬ù à¼õ£‚è «õ‡´‹. Þ‰î ç¬ðL™ result.xml notificationmanager Activity -»ì¡ SetContentView(R.layout.result) â¡ø õKJ¡ Íô‹ ެ특 ãŸð´ˆF»œ«÷£‹. Ü´ˆ¶ CreateNotifoca t i o n- Ÿ è £ ù Gó¬ô à¼õ£ ‚è «õ‡´‹. «ï£†®çH«è êQ¬ù Þ‰î ꣡P™ CreateNotification â¡ø ªð£ˆî£¬ù‚ AO‚ ªêŒî¾ì¡ ªêò™ ï¬ìªðÁõ main.xml ç¬ðL™ «ï£†®çH«èê¡ «ñ«ù ü¬ó ðò¡ð´ˆî «õ‡´‹. CreateNotification -Ÿè£ù Gó™ ä‰F™ ªè£´‚èŠð†´œ÷¶. ÜŠO‚«èê¬ù ªêò™ð´ˆ¶õ º¡ sAndroidManifest.xml ç¬ðL™ W›‚è‡ì õK è¬÷ Þ¬í‚è «õ‡´‹. ÜŠO«èê¬ù„ ªêò™ð´ˆ¶õ sRun As -> Andriod Application â¡Á ªè£´‚è «õ‡ ´‹. F¬óJ™ «î£¡Á‹ Create Notification â¡ø ªð£ˆî£¬ù‚ AO‚ ªêŒî£™ «ï£†® çH«èê¡ v«ì†ìv 𣘠«î£¡Á‹. Ü´ˆîî£è ìòô£‚ «ï£†®çH«èêQŸ è£ù ꣡Á à¼õ£‚°‹ º¬ø¬òŠ 𣘊 «ð£‹. Dialog Notification Dialog A÷£R¬ù Ü®Šð¬ìò£è‚ ªè£‡´ à¼õ£‚èŠð´Aø¶. ìòô£‚ A÷£C¬ù Ü®Šð¬ìò£è ªè£‡´ Alert Dialog, Progress Dialog, DatePickerDialog, TimePickerDialog «ð£¡ø¬õ àœ÷ù. Ý‚® M†®J¬ùÜ®Šð¬ìò£èªè£‡«ììòô£‚
  4. 4. package com.ram.datepickerexample; import android.app.Activity; import java.util.Calendar; import android.app.DatePickerDialog; import android.widget.DatePicker; import android.app.Dialog; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class DatePickerExampleActivity extends Activity { private TextView mDateDisplay; private int mYear; private int mMonth; private int mDay; static final int DATE_DIALOG_ID = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mDateDisplay = (TextView) findViewById(R.id.dateDisplay); Button pickDate = (Button) findViewById(R.id.pickDate); pickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(DATE_DIALOG_ID); } }); final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); updateDisplay(); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); } return null; } protected void onPrepareDialog(int id, Dialog dialog) { switch (id) { case DATE_DIALOG_ID: ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay); break; } } private void updateDisplay() { mDateDisplay.setText( new StringBuilder() // Month is 0 based so add 1 .append(mMonth + 1).append("- ") .append(mDay).append("-") .append(mYear).append(" ")); } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; updateDisplay(); } }; } ðì‹-6 Gó™-9 à¼õ£‚èŠð´‹. Ý‚®M†®JL¼‰¶ ìòô£‚ à¼õ£‚°õ OnCreateDialog(int) â¡ø ªñˆî† ðò¡ð´Aø¶. Üô£˜† ìòô£‚ ªêòL¬ù ÜP‰¶ ªè£œ õ ¹Fò Šó£ªü‚† Ýè Dialog Ex â¡ ðî¬ù à¼õ£‚°«õ£‹. ÞF™ Button å¡P¬ù à¼õ£‚A Üî¬ù AO‚ ªêŒî£™ Üô£˜† ìòô£‚ «î£¡Á‹. ÞîŸè£ù «ôܾ† ç¬ð™ main.xml Ý°‹. (Gó™ 6) main.xml L™ à¼õ£‚A àœ÷ ªð£ˆî£Q¡ ªðò˜ Button 1 â¡ð‹. ªð£ˆ ù DialogExActivity -J™ ެ특 ãŸð´ˆî ºîL™ ªð£ˆî£¡ ÝŠªü‚®¬ù à¼õ£‚è «õ‡´‹. Button b1 ; H¡¹ b1 = (Button) findViewById(R.id.button1); â¡Á ðò¡ð´ˆî «õ‡´‹. ªð£ˆî£¬ù‚ AO‚ ªêŒî£™ ªêò™ ï¬ìªðÁõ b1. SetOnClickListener(); â¡ðî¬ù ðò¡ð´ˆî «õ‡´‹. Üô£˜†ìòô£‚A¬ùà¼õ£‚° õ Alert.Dialog.Builder ðò¡ ð´Aø¶.
  5. 5. - ªî£ì¼‹ ðì‹-7 AlertDialog alertdialog=new AlertDialog. Builder(DialogExActivity.this).create Ý°‹. Üô£˜† ìòô£‚ ªêò™ð£†®Ÿè£ù Gó™ 7-™ ªè£´‚èŠð†´œ÷¶. ÜîŸè£ù F¬ó ðì‹ 5-™ àœ÷¶ «ð£¡Á «î£¡Á‹. Ü´ˆîî£è Progress Dialog ðò¡ð´ˆ¶‹ º¬øJ¬ù 𣘊«ð£‹. ꣡ø£è, ProgressDialog dialog = progressDialog.show( DialogExActivity.this,"", "Please Wait for few Seconds...",true); Progress Dialog -J¬ù GÁˆ¶õîŸè£ù ðò¡ð£´. ProgressDialogName.dismiss();  ðò¡ð´ˆF àœ÷ ꣡P™ dialog. dismiss(),; â¡Á ðò¡ð´ˆî «õ‡´‹. Progress Dialog ÜŠO‚«èêQ™F¬ó 6-™ àœ÷¶ «ð£¡Á «î£¡Á‹. Date Picker Dialog -J¬ù à¼õ£‚è main. xml ç¬ðL¬ù «ôܾ† Ýè ܬñˆ¶ Text View ñŸÁ‹ Button Þ¼‚°ñ£Á à¼õ£‚è «õ‡´‹. ð†ìQ¬ù‚ AO‚ ªêŒî£™ Date Picker Dialog «î£¡Áñ£Á Date Picker Example Activity -J¬ù à¼õ£‚è «õ‡´‹. DatePickerExampleActivity.java â¡ø Class ç¬ðL™ Cô Member è¬÷ Þ¬í‚è «õ‡´‹. ܬõ, sprivate Textview mDateDisplay: private Button mDate: private int mYear; private int mMonth; private int mDay; Static final int DATE-DAALOG-ID=O; H¡ù˜ On create () ªñˆî®¬ù à¼õ£‚è «õ‡´‹. ºîL™ GóL¬ù main.xml -Ÿ° ܬñˆ Fì «õ‡´‹. H¡ù˜ ªì‚v† MÎ ñŸÁ‹ ð†ìQ¬ù FindviewById(int) Íô‹ «ôܾ† ®™ Þ¼‰¶ ªðø «õ‡´‹. viewon clicklistee ner Íô‹ ð†ìQ¬ù AO‚ ªêŒ»‹«ð£¶ showDialog(int) ªêò™ð´ˆ¶‹. H¡ù˜ è£ô‡ìKL¼‰¶ ݇´, ñ£î‹ ñŸÁ‹ «îF ÝAò îèõ™è¬÷ ªðŸÁ H¡ù˜ updateDisplay() ªñˆî† ܬö‚èŠð†´ ªì‚v† MÎM™  «î˜‰ªî´‚°‹ «îF «î£¡Á‹. «ì†ì£H‚è˜ ìòô£‚AŸè£ù (DataPicker Dialog) Gó™ 9-™ ªè£´‚èŠð† ´œ÷¶. DataPicker Dialog «î£¡Á‹ F¬ó ðì‹ 7- ™ 裇H‚èŠð†´œ÷¶.

×