启动Service并传递数据的小实例(通过外界与服务进行通信):

1、activity_main.xml:

  <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="默认信息"
    android:id="@+id/etData"/>

  <Button
    android:text="启动服务"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnStartService" />

  <Button
    android:text="停止服务"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnStopService" />

2、MainActivity.java:

  public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText;

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

      editText = (EditText) findViewById(R.id.etData);
      findViewById(R.id.btnStartService).setOnClickListener(this);
      findViewById(R.id.btnStopService).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
      Intent i = new Intent(this,MyService.class);
      switch(v.getId()){
        case R.id.btnStartService:
          i.putExtra("data",editText.getText().toString());   //获取默认信息
          startService(i);
        break;
        case R.id.btnStopService:
          stopService(i);
        break;
      }
    }
  }

3、MyService.java:

  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(1000);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
            }
         }
       }.start();
    }

    @Override
    public void onDestroy() {
      super.onDestroy();
      running = false;
    }
  }

Android中Service通信(一)——启动Service并传递数据的更多相关文章

  1. Android中startService的使用及Service生命周期

    Android中有两种主要方式使用Service,通过调用Context的startService方法或调用Context的bindService方法.本文仅仅探讨纯startService的使用.不 ...

  2. Android 开发 8.0版本启动Service的方法

    前言  google在更新Android8.0后对Service的权限越发收紧.导致目前想要启动服务必需实现服务的前台化(否则在服务启动5秒后,系统将自动报错).下面我们就来看看如何在8.0上启动服务 ...

  3. android中的通信机制总结

      第一种:使用handler来进行通信   handler 大家可以把它想象成主线程(UI线程)的一个子线程,它可以给主线程(UI线程)发送数据从而更新主线程(UI线程)的UI与逻辑,handler ...

  4. Android中实现Activity的启动拦截之----实现360卫士的安装应用界面

    第一.摘要 今天不是周末,但是我已经放假了,所以就开始我们的技术探索之旅,今天我们来讲一下Android中最期待的技术,就是拦截Activity的启动,其实我在去年的时候,就像实现这个技术了,但是因为 ...

  5. Android中线程通信的方式

    Android 跨线程通信 android 中是不允许在主线程中进行 网络访问等事情的因为UI如果停止响应5秒左右的话整个应用就会崩溃,到Android4.0 以后 Google强制规定,与网络相关的 ...

  6. mono for android中使用dapper或petapoco对sqlite进行数据操作

    在mono for android中使用dapper或petapoco,很简单,新建android 类库项目,直接把原来的文件复制过来,对Connection连接报错部分进行注释和修改就可以运行了.( ...

  7. Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]

    http://blog.csdn.net/cjjky/article/details/6441104 在Android中的不同Activity之间传递对象,我们可以考虑采用Bundle.putSeri ...

  8. 【Android基础】利用Intent在Activity之间传递数据

    前言: 上一篇文章给大家聊了Intent的用法,如何用Intent启动Activity和隐式Intent,这一篇文章给大家聊聊如何利用Intent在Activity之间进行沟通.   从一个Activ ...

  9. Android使用JNI实现Java与C之间传递数据(转)

    介绍Java如何将数据传递给C和C回调Java的方法.  java传递数据给C,在C代码中进行处理数据,处理完数据后返回给java.C的回调是Java传递数据给C,C需要用到Java中的某个方法,就需 ...

随机推荐

  1. 关于Jmeter分布式测试在公司内的使用

    首先非常感谢虫师的文章受益匪浅 http://www.cnblogs.com/fnng/category/345478.html 今天,花了半天时间进行分布式的测试,真是纠结啊!! RT 1.在公司用 ...

  2. 刚接触Linux,菜鸟必备的小知识点(一)

    身为一个将要大四的学生,而且还是学计算机的没有接触过linux简直是羞愧难当.这个假期做了一个软件测试员,必须要熟悉linux的操作,所以对于我这个菜鸟我也就说几点比较重要的小知识点吧. 第一.cd指 ...

  3. Java 类的实例变量初始化的过程 静态块、非静态块、构造函数的加载顺序

    先看一道Java面试题: public class Baset { private String baseName = "base"; // 构造方法 public Baset() ...

  4. Hibernate 中出现表名(XXX) is not mapped 问题

    今天晚上自己试着用Hibernate去搭建一个Web工程,然后去实现一个简单的登录.通过Hibernate?做查询操作的时候总是报出这样的错:users is?not?mapped. 于是乎去检查了下 ...

  5. 装X代码

    装X代码 http://hackcode.ishoulu.com/scp/ http://hackcode.ishoulu.com/umbrella/ http://hackcode.ishoulu. ...

  6. rank()函数的使用

    排序: ---rank()over(order by 列名 排序)的结果是不连续的,如果有4个人,其中有3个是并列第1名,那么最后的排序结果结果如:1 1 1 4select scoreid, stu ...

  7. phylogeny analysis

    Multiple Alignment: MUSCLE ProbCons T-Coffee ClustalW Alignment curation: Gblocks Remove positions w ...

  8. Python 随机数用法

    1. random.seed(int) 给随机数对象一个种子值,用于产生随机序列. 对于同一个种子值的输入,之后产生的随机数序列也一样. 通常是把时间秒数等变化值作为种子值,达到每次运行产生的随机系列 ...

  9. git简易操作

    git config --global user.name 'scales' git config --global user.email "1477835701@qq.com" ...

  10. angular服务二

    angular服务 $http 实现客户端与服务器端异步请求 get方式 test.html <!DOCTYPE html> <html lang="en"> ...