zoukankan      html  css  js  c++  java
  • 用Intent给Service传数据

    对数通过intent传递,代码如下:

    MainActivity

     1 package com.example.zhangmeng.servicedemo;
     2 import android.content.Intent;
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.view.View;
     6 import android.widget.EditText;
     7 
     8 public class MainActivity extends AppCompatActivity implements View.OnClickListener{
     9     private EditText editText;
    10     @Override
    11     public void onClick(View v) {
    12         switch (v.getId()){
    13             case R.id.button_StartService:
    14                 Intent intent=new Intent(MainActivity.this,MyService.class);
    15                 intent.putExtra("data",editText.getText().toString());
    16                 startService(intent);
    17                 break;
    18             case R.id.button_StopService:
    19                 stopService(new Intent(MainActivity.this,MyService.class));
    20 
    21         }
    22 
    23     }
    24 
    25     @Override
    26     protected void onCreate(Bundle savedInstanceState) {
    27         super.onCreate(savedInstanceState);
    28         setContentView(R.layout.activity_main);
    29         findViewById(R.id.button_StartService).setOnClickListener(this);
    30         findViewById(R.id.button_StopService).setOnClickListener(this);
    31         editText=(EditText) findViewById(R.id.et);
    32 
    33 
    34 
    35 
    36     }
    37 }

    MyService.class

     1 package com.example.zhangmeng.servicedemo;
     2 import android.app.Service;
     3 import android.content.Intent;
     4 import android.os.IBinder;
     5 public class MyService extends Service {
     6     private boolean running=false;
     7     private String data="This is the data";
     8 
     9     @Override
    10     public void onCreate() {
    11         super.onCreate();
    12         running=true;
    13         new Thread(){
    14             @Override
    15             public void run() {
    16                 super.run();
    17                 while (running){
    18                     System.out.println(data);
    19 
    20                     try {
    21                         sleep(1000);
    22                     } catch (InterruptedException e) {
    23                         e.printStackTrace();
    24                     }
    25 
    26                 }
    27             }
    28         }.start();
    29     }
    30 
    31     @Override
    32     public int onStartCommand(Intent intent, int flags, int startId) {
    33         data=intent.getStringExtra("data");
    34         return super.onStartCommand(intent, flags, startId);
    35     }
    36 
    37     @Override
    38     public void onDestroy() {
    39         super.onDestroy();
    40         running=false;
    41     }
    42 
    43     @Override
    44     public IBinder onBind(Intent intent) {
    45         return null;
    46     }
    47 
    48 }

    MainActivity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.zhangmeng.servicedemo.MainActivity">

    <EditText
    android:id="@+id/et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="默认信息"/>
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="启动服务"
    android:id="@+id/button_StartService" />

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

    </LinearLayout>
  • 相关阅读:
    redis(二)高级用法
    redis(一) 安装以及基本数据类型操作
    RabbitMQ(五) -- topics
    JS实时数据运算
    Access数据库中Sum函数返回空值(Null)时如何设置为0
    asp检测数字类型函数
    MVC:从客户端中检测到有潜在危险的 Request.Form 值 的解决方法
    WIN8系统安装软件时提示"扩展属性不一致"的解决方法
    免费的网络扫描器-Advanced IP Scanner
    中国电信大亚DP607光猫破解,设置路由,wifi!关闭远程管理,改连接限制,SN码查询!
  • 原文地址:https://www.cnblogs.com/WebGiant/p/5879541.html
Copyright © 2011-2022 走看看