Android-Start service and pass data

Source: Internet
Author: User

This article primarily records activity delivery data to the service.


1.


2, through the above, you can see the activity page value changes, the corresponding background service output value also changed.
3, the core code is as follows, look at 38 lines in the code, using intent as a carrier, loading the data on the activity page.

?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 <code class="hljs" java="">package com.example.connectservice;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.EditText;public class MainActivity extends Activity implements OnClickListener {    private EditText edit;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById(R.id.startService).setOnClickListener(this);        findViewById(R.id.stopService).setOnClickListener(this);        edit = (EditText) findViewById(R.id.editText1);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.startService:            Intent i = new Intent(this, MyService.class);            i.putExtra(data, edit.getText().toString());            startService(i);            break;        case R.id.stopService:            stopService(new Intent(this, MyService.class));            break;        }    }}</code>

4, the service code is as follows, see the Code of 21 lines, we in the Onstartcommand method through the intent parameter to get the value of activity passed over.

?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 <code class="hljs" java="">package com.example.connectservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.widget.EditText;public class MyService extends Service {    String data;    boolean running = false;    @Override    public IBinder onBind(Intent intent) {        // TODO Auto-generated method stub        return null;    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        data = intent.getStringExtra(data);        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onCreate() {        super.onCreate();        running = true;        new Thread() {            public void run() {                while (running) {                    System.out.println(Service中获取到的数据: + data);                    try {                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }            };        }.start();    }    @Override    public void onDestroy() {        super.onDestroy();        running = false;    }}</code>

?
12345678910111213141516 <code class="hljs" xml=""><manifest android:versioncode="1" android:versionname="1.0" package="com.example.connectservice" xmlns:android="http://schemas.android.com/apk/res/android"><uses-sdk android:minsdkversion="8" android:targetsdkversion="21">                        <intent-filter>                                <category android:name="android.intent.category.LAUNCHER">            </category></action></intent-filter>        </activity>        <service android:enabled="true" android:exported="true" android:name=".MyService">        </service>    </application></uses-sdk></manifest></code>

Android-Start service and pass data

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.