码迷,mamicode.com
首页 > 移动开发 > 详细

Android -- 启动Service并传递数据

时间:2016-01-02 20:23:39      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

本文主要记录Activity传递数据到Service。


1、效果图
技术分享
技术分享
2、通过以上效果图,可以看出activity页面的数值改变,相应后台service输出的数值也跟着改变。
3、核心代码如下,看代码中的38行,使用Intent作为载体,装载activity页面上的数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<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、Service代码如下,详见代码的21行,我们在onStartCommand方法中通过intent参数获取activity传过来的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<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>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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 -- 启动Service并传递数据

标签:

原文地址:http://www.cnblogs.com/cityking/p/a017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!