Faq

button (Button) findViewByld(R.id.cloudy I);

but tori. setOnClickListener (new Button.OnCl ickListener () \

public void onClick(View v) I

setWeather rBB"^^", "XHffiU", "ffl^ftlfi", R .drawable . cloudy) ; )

button = (Button) findViewByld(R.id.rain 1);

button.setOnClickListener(new Button.OnCiickListener() {

public void onClick(View v) {

set-Weather ("AmSSfe", R.drawable.rain) ;

button - (Button) £indViewByld(R.id.defaultSound); but ton.setOnClickListener(new Button.OnClickListener() ( public void onClick(View v) { setDefault(Not ification.DEFAULT_SOUND);

button - (Button) findViewByld(R.id.defaultVibrate); button.setOnClickListener(new Button.OnCiickListener() ( public void onClick(View v) (

setDefault(Notification.DEFAULT VIBRATE); }

button = (Button) findVLewbyld(R.id.defauItA"i1); button.setOnClickListener(new Button.OnClickListener() < public void onClick(View v) {

setDefault(Notification.DEFAULT_ALL); )

button (Button) findViewById(R.id.clear);

button.setOnClickListener(new Button.OnClickListener0 \

public void onClick(View v) I

mNoti ficarionManager.cancel(NOT!FTCATIONS_TD); 1

private void set.Weather (String tickerText, String title, String content, int drawable) I

Notification notification new Not ification(drawable, tickerText, System.currentTimeMillis());

Pending1ntent contentIntent Pendinglntent.getActivity(this, 0, new Intent (this, ActivityMain.class), 0) ;

notificat ion.setLatestEventInfo(this, title, content, content Intent);

mNotificationManager.notify(NOTIFICATIONS_ID, notification)/ }

private void setDefault(int defaults) {

Pendinglntent contentIntent = Pendinglntent.getActivity(this, 0, new Intent(this, ActivityMain.class), 0);

String title = String content -

final Notification notification - new Not ification(R.drawable.sun, content. System.currentTimeMillis<) ) ;

notification. set LatestEventlnf o (this, title, content., content Intent) ; notification.defaults defaults;

mNotificationManager.notify(NOTIFICATIONS ID, notification); 1 )

• W\ Notification B&ti\ Notification Manager jfc^ffl, 35 &WM t Noti ficationManager fKj&'WI. W®ifffl^iii^ Activity f^tfj Notification:mNotificationManager = (Noti ficati on Manager) getSystemService(NOTI FI CATION SERVICE);

• System.currentTimeMiIIis() vb'%1 ft?£l]&tjiftj Sf(FHfilo

• setWeather()i^Silk ActivityMainNotification -„ ffiS^Mii, f ^Notification , Notification

• Notification notification new Notificationdrawable, ticker Text,System.currentTimcMillis ()); . IS-ttfM^Jittio

• 58Notification M^AWfri], jZBPM/F. System.current rimeMillis()o

• ftJuW 4s Notification NotificationManager M/jvtl'.^, if S&fiiftft» H

Notification ttft -fa ID. ^ ID fijffifflff»*ft, fflftl. /r.iitW f

'VlMD X/ NOTIFICATIONS ID„ M^ Notification WJl-fr^aWiPi^s mNotificationManager.notify (NOTIFICATIONS ID, notification);

• —^ Notification liijif W "AS T«M& >F "I'> W. Jf «t ¿1 itt J±/j tt setLatest Eventlnfo(Context context, CharSequence contentTitle, CharSequence contentText, Pendinglntent contentIntent)*ai!M"/7iBWrt^: 'IMMm 7-67 fiff/fc, "i/gjf Notification ^J^WWfct. ttl

Notification ; fift -//¡HSltS^ilf Notification itfM, ftMttaU&rfc,

• setDefault()i$3&;te ActivityMainNotification SJ&Mit -'MtKfcjitffto t Notification. fP.Ei&W-.P&m f Eft JB»J6ifr Notificaion aitt««!^

fiW^. in notification.defaults = defaults« ^jfeffJ-J* S11. JIJfiJTtU FJLftl^WSM^o

• Notification.DEFAULT VIBRATE, ^^'^HUfft^^h Notification

• Notification.DEFAULT SOUND, Mnjij ffii*^ Notification ^/kM'j^WIHM, f ^Liift-

• Notification.DEFAULT ALL, 3 fniffti^ Notification tiyj^WKlMHifc• N/ll'WiHt-M^jffgftfo

^i(9FJ«JIJW KW-lfli'Y'-.li- Toast" ScfflJB, wmvm ActivityToast 'I', ftfrfi- • FfW

package com.eoeAndroid.toast_and_notification;

import Androi d.app.Act i vi ty; impor t Android.app.Not i f ication; import Android. app. Not i.-icationManager; import Android.app.Pendinglntent; import Android.content.Context; import Android.content.Intent; import Android, os. Bundle; import Android.view.LayoutInflater; impor t Android.view.View;

import Android, view. View. OnClickListenereimport Android.widget.Button; import Android.widget.TextView; import Android.widget.Toast;

public class ActivityToast extends Activity {

OnClickListener listenerl = null; OnClickListener listener2 ~ null; Button button 1; Button button2;

private static int NOT!FICATI0NS_ID = R.layout .activity_toast; ClOverr i do public void onCreate(Bundle savedlnstanceState) I

super.onCreate(savedlnstanceState);

listenerl new OnClickListener() {

public void onClick(View v) (

setTitle ("JSBifiilS!^ Toast") ;

showToast(Toast.LENGTH SHORT);

listener^ = new OnClickListener{) { public void onClick(View v) { setTitle ("-K&ffi^TF Toast") ; showToast(Toast.LENGTH LONG); showNotif i cat i on(); ) );

setContentView(R.layout.activity toast); buttonl (Button) findViewById(R.id.buttonl); button!.setOnClickListener(1istenerl); butLon2 - (Button) findViewByld(R.id.button2); buttori2 . set:OnClickListener (listener2) ; )

protected void showToast(int type) { View view - inf lateView (R. layout. toast.) ;

TextView tv = (TextView) view.findViewByld(R.id.content);

tv.set.Text ("})0Alf± Android ff^ttE eoeAndroid.com, ^^^JifiiiSI^") ;

Toast toast = new Toast(this); toast. set.View (view) ; toast.setDuration(type);

private View inflateView(int resource) {

Layout Inf later vi. = (Layout Inf later) get.SystemService (Context. LAYOUT_INFLATER SERVICE);

return vi.inflate(resource, null); }

protected void showNotification() {

Notif icationManager notificationManager = (NOTIFICATION SERVICE);

(NotificationManager) getSystemService

CharSequence title - "iMfcffl Android " ;

CharSequence contents "eoeAndroid.com";

PendingIntent content Intent = Pendinglntent.getActivity(this, 0, new Intent (this. ActivityMain.class), 0);

Notification notification = new Notification(R.drawable.default icon.

title« System.currentTirneMillis () ) ;

notification.setLatestEventlnfo{this, title, contents, ccntentlntent);

// 100ms ifeaft 250ms, f$lt 100ms Jsfeifl 500ms notification.vibrate = new longf) ( 100, 250, 100, 500 };

not if icationManager .notify (NOT I FICATIONS_ID, not.i f ication) ; )

Toast MiSttLftSiKifl^W -flJ h'A > hill Notification -ft, Notification®®

$ Noti(IcationManager flrfifffl. ifu Toast Jiff Android + Widget ff-fcfft« hli^WSlAW showToast + "J'Wfi" Toast Ift, Attain >'o

• ftftiffiS&M it ^ Toast, iffiiifW Toast toast -new Toast(this)

• H Toast ^ View ffiX:. fin toast set View(view)«

• Toast fin * B it Toast fi^MMMt** W «ii ® S toast.setDuration(Toast.LENGTH LONG) )(<:'& ifflSflii.1' Toast S^flWfflftT ¿¿Mii. ojWifflii toast.setDuration(Toast.LENGTH_SHORT)3K^0io

• i/ii^Hn/ri, «ftMTWih Toast S/jsil1.)R-, iftil toast.showO^^M^ffl/r^Liat* Toasto

Toast a^Stftii. f VIf-*ft 1 KWl"J«1fi^T&t Toast SUH r Notification Jfl F^&^&SgJtl/^ Notification WttWSfJUiiB^.ii^ittM J', i£ffi JS/l: Activity Toast 'I'M showNotification ^itk^JB^^j'HBW,

• notification.vibrate = new long|| { 100,250, 100,500 }, iWM Notification iJi'R )(-I 100ms fc^J 250ms. ^Jn'fV' it 100ms fn I'fft^ 500ms*

Was this article helpful?

0 0

Post a comment