首页 > 代码库 > 用Intent给Service传数据

用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     @Override11     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     @Override26     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="http://www.mamicode.com/This is the data"; 8  9     @Override10     public void onCreate() {11         super.onCreate();12         running=true;13         new Thread(){14             @Override15             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     @Override32     public int onStartCommand(Intent intent, int flags, int startId) {33         data=http://www.mamicode.com/intent.getStringExtra("data");34         return super.onStartCommand(intent, flags, startId);35     }36 37     @Override38     public void onDestroy() {39         super.onDestroy();40         running=false;41     }42 43     @Override44     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>

用Intent给Service传数据