启动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. 理解Docker(5):Docker 网络

    本系列文章将介绍 Docker的相关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...

  2. Say goodbye to my photos&videos

    刚刚得知一个悲惨的消息:虽然2012已经过去了,但是世界末日并未过去.嗯,我不是来严肃的,我是来搞笑的.毕竟,我已经如此伤心了.中午结束考试,下午看了一半的电影然后躺室友的床上睡了一觉,醒来看到阿姨发 ...

  3. Java实现office文档与pdf文档的在线预览功能

    最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...

  4. 约瑟夫问题(java实现)

    方法一.自定义的链表实现 package com.code.yuesefu; public class YueSeFuList { public static void main(String[] a ...

  5. java util 下的concurrent包

    ------------------------------------------java util 下的concurrent包--------并发包--------------------.jav ...

  6. [LeetCode] Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  7. [LeetCode] Summary Ranges 总结区间

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  8. FineUI(专业版)v3.1发布(ASP.NET控件库)!

    FineUI(专业版)v3.1 正式发布,60多项更新,官网示例多达 690 个,新增 30 个页面加载动画! 自 2008 年 4 月发布第一个版本,我们持续更新了 126 个版本,拥有 16000 ...

  9. 华为5G空口新技术(2015年)

    2015-03-24 长江后浪推前浪,4G建设方兴未艾,业界关于5G的讨论已如火如荼.对于每一代移动通信,空口技术都相当于王冠上的明珠. 在月初的世界移动通信大会上,华为发布了面向5G的新空口,并展出 ...

  10. 使用UITableView展示数据

    TableView主要用于展示数据,类似于Android中的ListView. 我们可以通过两个方式使用TableView.第一种是直接使用TableView类.第二种是通过UITableViewCo ...