Android establishes a local service that can communicate with the Activity.

Source: Internet
Author: User

MyService. java [java] package cn. itcast. service; import android. app. service; import android. content. intent; import android. OS. binder; import android. OS. IBinder; public class MyService extends Service {private Binder binder = new MyBinder (); public String getName (int id) {return "Chuanzhi podcast ";} @ Override public IBinder onBind (Intent intent) {return binder;} private final class MyBinder extends Binder im Plements IService {public String getName (int id) {return MyService. this. getName (23) ;}} MainActivity. java [java] package cn. itcast. service; import android. app. activity; import android. content. componentName; import android. content. intent; import android. content. serviceConnection; import android. OS. bundle; import android. OS. IBinder; import android. view. view; import android. widget. button; import Droid. widget. textView; public class MainActivity extends Activity {private MyServiceConnection conn; private IService myservice; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); conn = new MyServiceConnection (); Intent intent = new Intent (this, MyService. class); bindService (intent, conn, BIND_AUTO_CREATE); Button button = (Button) this. findViewById (R. id. button); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {TextView resultView = (TextView) findViewById (R. id. result); resultView. setText (myservice. getName (56) ;}});} private final class MyServiceConnection implements ServiceConnection {@ Override public void onServiceConnected (ComponentName name, IBinder service ){ Myservice = (IService) service ;}@ Override public void onServiceDisconnected (ComponentName name) {myservice = null ;}@ Override protected void onDestroy () {unbindService (conn); super. onDestroy () ;}} IService. java [java] package cn. itcast. service; public interface IService {public String getName (int id);} [html] <? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "cn. itcast. service "android: versionCode =" 1 "android: versionName =" 1.0 "> <application android: icon =" @ drawable/icon "android: label = "@ string/app_name"> <activity android: name = ". mainActivity "android: label =" @ string/app_name "> <intent-filter> www.2cto.com <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> <service android: name = ". myService "/> </application> <uses-sdk android: minSdkVersion =" 8 "/> </manifest>

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.