Notification example on Android's site -
so i'm quite new android , i'm working through of example code off android's site. 1 i'm looking @ called notifying user: http://developer.android.com/training/notify-user/index.html.
the example pretty simple, user enters in time in seconds , message, , timer set in background. once time runs out, user notified.
so here copy of activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <textview android:id="@+id/seconds_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="32dp" android:textappearance="?android:attr/textappearancemedium" android:text="@string/ping_text" /> <edittext android:id="@+id/edit_seconds" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/seconds_view" android:layout_centerhorizontal="true" android:layout_margintop="15dp" android:ems="10" android:text="@string/seconds_default" android:inputtype="numbersigned"> <requestfocus /> </edittext> <textview android:id="@+id/reminder_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edit_seconds" android:layout_centerhorizontal="true" android:layout_margintop="40dp" android:textappearance="?android:attr/textappearancemedium" android:text="@string/reminder_label" /> <edittext android:id="@+id/edit_reminder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/edit_seconds" android:layout_below="@+id/reminder_view" android:layout_margintop="15dp" android:ems="10" android:text="@string/reminder_text" android:inputtype="textmultiline" /> <button android:id="@+id/ping_button" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edit_reminder" android:layout_centerhorizontal="true" android:layout_margintop="25dp" android:textappearance="?android:attr/textappearancemedium" android:onclick="onpingclick" android:text="@string/ping" /> </relativelayout>
you can see there 2 editviews user enter in seconds (edit_seconds) , message (edit_reminder). there button called ping, when press sends data serviceintent.
below code mainactivity.java:
/* * copyright (c) 2012 android open source project * * licensed under apache license, version 2.0 (the "license"); may not use file except * in compliance license. may obtain copy of license @ * * http://www.apache.org/licenses/license-2.0 * * unless required applicable law or agreed in writing, software distributed under license * distributed on "as is" basis, without warranties or conditions of kind, either express * or implied. see license specific language governing permissions , limitations under * license. */ package com.example.android.pingme; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.edittext; import android.widget.toast; public class mainactivity extends activity { private intent mserviceintent; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // creates explicit intent start service constructs , // issues notification. mserviceintent = new intent(getapplicationcontext(), pingservice.class); } /* * gets values user entered , adds them intent * used launch intentservice runs timer , issues * notification. */ public void onpingclick(view v) { int seconds; // gets reminder text user entered. edittext msgtext = (edittext) findviewbyid(r.id.edit_reminder); string message = msgtext.gettext().tostring(); mserviceintent.putextra(commonconstants.extra_message, message); mserviceintent.setaction(commonconstants.action_ping); toast.maketext(this, r.string.timer_start, toast.length_short).show(); // number of seconds timer should run. edittext edittext = (edittext)findviewbyid(r.id.edit_seconds); string input = edittext.gettext().tostring(); if(input == null || input.trim().equals("")){ // if user didn't enter value, sets default. seconds = r.string.seconds_default; } else { seconds = integer.parseint(input); } int milliseconds = (seconds * 1000); mserviceintent.putextra(commonconstants.extra_timer, milliseconds); // launches intentservice "pingservice" set timer. startservice(mserviceintent);
you can see above in onpingclick(view v) method both edittexts referenced variables "msgtext" , "edittext". ping button never referenced nor listener attached button starts services when button pressed. however, app works on both emulator , phone without button being referenced.
how android know clicking button means method onpingclick(view v) must actioned?
because in xml layout button specified : android:onclick="onpingclick"
sets onclicklistener outside java code.
note method mus satisfy signature:
public void somename(view v)
it way set click listener view.
Comments
Post a Comment