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

Android Studio开发基础之启动Service,并通过从Activity向Service传递数据

时间:2015-05-23 18:28:44      阅读:760      评论:0      收藏:0      [点我收藏+]

标签:

       本实例演示启动Service,并通过从Activity向Service传递数据,新建一个Service,并敲如下代码:

package com.example.lhb.startservice;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.view.ViewDebug;
import android.widget.Toast;

public class MyService extends Service {
    private boolean Running=false;
    private String data="默认信息!!!";
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) { 
        data=intent.getStringExtra("data");//这里的intent是参数里的,不是自定义的
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Running=true;
        new Thread(){
            @Override
            public void run() {
                super.run();
                while (Running){
                    System.out.println(data);
                    try {
                        sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Running=false;
    }
}
       主程序代码:
package com.example.lhb.startservice;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
    private EditText inputText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btnStartService).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputText= (EditText) findViewById(R.id.inputText);
                if(inputText.getText().length()==0){
                    Toast.makeText(MainActivity.this,"请输入传递的值!",Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent;
                intent=new Intent(MainActivity.this,MyService.class);
                intent.putExtra("data",inputText.getText().toString());
                startService(intent);
            }
        });

        findViewById(R.id.btnStopService).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                intent=new Intent(MainActivity.this,MyService.class);
                stopService(intent);
            }
        });
    }
}


Android Studio开发基础之启动Service,并通过从Activity向Service传递数据

标签:

原文地址:http://blog.csdn.net/lucky51222/article/details/45935809

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