第一步.MainActivity.java

package com.chaowen;import android.app.Activity;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {   private Button startButton,stopButton,bindButton,unbindButton;       @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                startButton = (Button)findViewById(R.id.startButton);        stopButton = (Button)findViewById(R.id.stopButton);        bindButton = (Button)findViewById(R.id.bindButton);        unbindButton = (Button)findViewById(R.id.unbindButton);                //添加监听器        startButton.setOnClickListener(startListener);        stopButton.setOnClickListener(stopListener);        bindButton.setOnClickListener(bindListener);        unbindButton.setOnClickListener(unbindListener);            }                //启动Service监听器    private OnClickListener startListener = new OnClickListener() {@Overridepublic void onClick(View v) {//创建IntentIntent intent = new Intent();//设置Action属性intent.setAction("com.chaowen.action.MY_SERVICE");//启动该ServicestartService(intent);}};//停止Service监听器private OnClickListener stopListener = new OnClickListener() {@Overridepublic void onClick(View v) {   Intent intent = new Intent();   intent.setAction("com.chaowen.action.MY_SERVICE");   stopService(intent);}};//连接对象private ServiceConnection connection =new ServiceConnection() {//断开时调用@Overridepublic void onServiceDisconnected(ComponentName name) {//输出日志Log.i("Service", "断开连接");//通过Toast显示信息Toast.makeText(MainActivity.this, "断开连接", Toast.LENGTH_LONG).show();}//连接时调用@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//输出日志Log.i("Service", "连接成功");//通过Toast显示信息Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_LONG).show();}};//绑定Service监听器private OnClickListener bindListener = new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setAction("com.chaowen.action.MY_SERVICE");bindService(intent, connection, Service.BIND_AUTO_CREATE);}};//解除绑定Service监听器private OnClickListener unbindListener = new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setAction("com.chaowen.action.MY_SERVICE");unbindService(connection);}};}

第二步.MyService.java

package com.chaowen;

import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;import android.widget.Toast;public class MyService extends Service {@Overridepublic IBinder onBind(Intent intent) {    Log.i("SERVICE", "onBind");    Toast.makeText(MyService.this, "onBind...", Toast.LENGTH_LONG).show();return null;}   @Overridepublic void onCreate() { Log.i("SERVICE", "onCreate"); Toast.makeText(MyService.this, "onCreate...", Toast.LENGTH_LONG).show();}@Overridepublic void onStart(Intent intent, int startId) { Log.i("SERVICE", "onStart"); Toast.makeText(MyService.this, "onStart...", Toast.LENGTH_LONG).show();}@Overridepublic void onDestroy() { Log.i("SERVICE", "onDestroy"); Toast.makeText(MyService.this, "onDestroy...", Toast.LENGTH_LONG).show();}}

第三步.main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><Button  android:text="启动Service"  android:id="@+id/startButton"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  />      <Button  android:text="停止Service"  android:id="@+id/stopButton"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  />    <Button  android:text="绑定Service"  android:id="@+id/bindButton"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  />    <Button  android:text="解除绑定"  android:id="@+id/unbindButton"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  />  </LinearLayout>

第四步.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.chaowen"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="4" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".MainActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>                        <service android:name="MyService">           <intent-filter>             <action android:name="com.chaowen.action.MY_SERVICE" />             </intent-filter>                </service>    </application></manifest>




更多相关文章

  1. SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
  2. Android(安卓)ListView实现方法三(自定义适配器)
  3. Android(安卓)蓝牙客户端实现
  4. Adroid Studio基于TCP协议的通信
  5. Android(安卓)应用的耗电量优化
  6. android网络与通信(三种网络接口简述 )
  7. 二、获取wifi列表并连接wifi
  8. 一个不错的loading效果
  9. Android(安卓)基于NetworkCallback的网络状态监听框架

随机推荐

  1. 创建 PSR-4 的 Php 包
  2. 看看PHP 多进程处理任务
  3. 一定要改掉 这5个PHP编程中的不良习惯!
  4. PHP安全问题汇总
  5. 详解PHP中被忽略的性能优化利器:生成器
  6. 教你使用PHP实现查找你想要的附近人
  7. 你可能要纠正这5个PHP编码小陋习!
  8. PHP处理时间和时区需注意以下三点!
  9. php+redis实现全页缓存系统
  10. 分享php秒杀功能实现的思路