Android Getting Started-service

Source: Internet
Author: User

SDK 1.7

Package Com.example.hellowrold;import Java.util.random;import com.example.hellowrold.r.id;import Android.os.bundle;import Android.app.activity;import Android.content.intent;import android.graphics.Typeface; Import Android.view.menu;import android.view.view;import Android.widget.Button; Public classEX0314 extends Activity {PrivateButton Startbutton; PrivateButton Stopbubutton; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (r.layout.activity_ex0314); Startbutton=(Button) Findviewbyid (ID.        Ex0314startbutton); Startbutton.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubIntent sintent=NewIntent (EX0314. This, MService1.class);                Sintent.setflags (Intent.flag_activity_new_task);            StartService (sintent);                        }        }); Stopbubutton=(Button) Findviewbyid (ID.        Ex0314stopbutton); Stopbubutton.setonclicklistener (NewButton.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubIntent sintent=NewIntent (EX0314. This, MService1.class);            StopService (sintent);            }        }); } @Override Publicboolean Oncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (r.menu.ex0314, menu); return true; }}
Package Com.example.hellowrold;import Android. R.integer;import Android.app.service;import Android.content.intent;import Android.os.handler;import Android.os.ibinder;import Android.util.Log; Public classMService1 extends service{PrivateHandler objhandler=NewHandler ();Private intIntcounter=0;PrivateRunnable mtasks=NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method Stubintcounter++; LOG.I ("Hippo","Calculator"+integer.tostring (intcounter)); Objhandler.postdelayed (Mtasks, +); }}; @Override Public voidonCreate () {//TODO auto-generated Method Stubsuper.oncreate (); Objhandler.postdelayed (Mtasks, +); } @Override Public voidOnDestroy () {//TODO auto-generated Method StubSuper.ondestroy ();    Objhandler.removecallbacks (Mtasks); } @Override Publicibinder onbind (Intent Intent) {//TODO auto-generated Method Stub        return NULL; }}
<Activity Android:name="com.example.hellowrold.EX0314"Android:label="@string/app_name"> <intent-filter> <action android:name="Android.intent.action.MAIN"/> <category android:name="Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android:name=". MService1"android:exported="false"> </service>

Android Getting Started-service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.