Activity data transfer to service

Source: Internet
Author: User

The activity data interface is responsible for starting the service pack. Service to obtain data. Surgery.

Detailed demo as below:

Package Com.example.android_service_trance;import Android.annotation.suppresslint;import android.app.Activity; Import Android.content.componentname;import Android.content.context;import Android.content.intent;import Android.content.serviceconnection;import Android.os.bundle;import Android.os.ibinder;import Android.os.Parcel; Import Android.os.remoteexception;import android.util.log;import Android.view.menu;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.textview;import Com.example.android_service_trance. Myservice.localbinder, @SuppressLint ("Recycle") public class Mainactivity extends Activity {private Button bindservice= Null;private button callservice=null;private button communicationservice=null;private TextView tv=null;private Boolean flag=false;//does not feel bound to the object in the private MyService myservice=null;private localbinder binder;//service @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); SetcoNtentview (R.layout.activity_main); bindservice= (Button) This.findviewbyid (R.id.button1); callservice= (Button) This.findviewbyid (R.id.button2); communicationservice= (Button) This.findviewbyid (R.id.button3); tv= (TextView) This.findviewbyid (R.id.textview1); Bindservice.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//Get Intent Intent intent=new Intent (MAINACTIVITY.THIS,MYSERVICE.CLASS);//Bind service Bindservice (Intent, connection, context.bind_auto_create);}); Callservice.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {if (flag) {int result= Myservice.getrandom (); Tv.settext ("<<<<<<<<" +result);}}); Communicationservice.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {// Objects passing values to service Parcel data=parcel.obtain ();d ata.writeint (10086);d ata.writestring ("Li Hua"); Parcel Reply=parcel.obtain (); try {binder.transact (ibinder.last_call_transaction, data, reply, 0);} catch ( RemoteException e) {//TODO Auto-generaTed Catch Blocke.printstacktrace ();} Read data from the service LOG.I ("main<<<<<<<<", reply.readstring ()); LOG.I ("main<<<<<<<<<", Reply.readint () + "");}}); Unbind @overrideprotected void OnStop () {super.onstop (); if (flag) {//unbound unbindservice (connection); flag=false;}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} Bridge connecting activity and service private serviceconnection connection=new serviceconnection () {@Overridepublic void Onserviceconnected (componentname arg0, IBinder ibinder) {//Connect//Get Localbinder binder= (localbinder) ibinder;// Get MyService instance Myservice=binder.getservice (); flag=true;} @Overridepublic void onservicedisconnected (ComponentName arg0) {//do not connect flag=false;}};}

Service side:

/** *version: *author:yangquanqing *data: */package com.example.android_service_trance;import java.util.Random; Import Android.app.service;import android.content.intent;import Android.os.binder;import Android.os.IBinder;import Android.os.parcel;import android.os.remoteexception;import android.util.log;/** * @author YangQuanqing YQQ * */public Class MyService extends Service {//define a random number for test private random random=new random ();p rivate localbinder lb=new Localbinder ( );//Gets the instance of the current class public class Localbinder extends Binder{public myservice getService () {return myservice.this;} @Overrideprotected boolean ontransact (int code, PARCEL data, Parcel Reply,int flags) throws RemoteException {//activity won Take Data log.i ("service<<<<<<<<", data.readstring ()); LOG.I ("service<<<<<<<<<", Data.readint () + ""); Reply.writestring ("nickname"); Reply.writeInt ( 1990); return Super.ontransact (Code, data, reply, flags);}} @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper. OnCreate ();} @Overridepublic void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy ();} @Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method Stubreturn lb;} Get a random number public int getrandom () {return random.nextint (22);}}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Activity data transfer to 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.