Android菜鸟练习第三十五课 Service基本使用

第一部分 Service部分  关联文章http://blog.csdn.net/pi9nc/article/details/18764415?locationNum=3&fps=1

public class MyService extends Service {
    public String TAG = "MY_SERVICE";
    public MyBinder mBinder = new MyBinder();

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "CREATE");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "DESTROY");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "START_COMMAND");
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG, "BIND");
        return null;
    }


    class MyBinder extends Binder {
        public void startDownLoad() {
            //执行下载的具体任务
            Log.e(TAG, "执行下载任务");
        }
    }
}
 
第二部分 Activity部分
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public Button mBtnStart, mBtnStop, mBtnBind, mBtnUnBind;
    //任何一个Service在整个应用程序范围内都是通用的,即MyService不仅可以和MainActivity建立关联
    //还可以和任何一个Activity建立关联,而且在建立关联时它们都可以获取到相同的MyBinder实例。
    public MyService.MyBinder myBinder;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (MyService.MyBinder) service;
            myBinder.startDownLoad();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtnStart = (Button) findViewById(R.id.start);
        mBtnStart.setOnClickListener(this);
        mBtnStop = (Button) findViewById(R.id.stop);
        mBtnStop.setOnClickListener(this);
        mBtnBind = (Button) findViewById(R.id.bind);
        mBtnBind.setOnClickListener(this);
        mBtnUnBind = (Button) findViewById(R.id.unbind);
        mBtnUnBind.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                Intent intent = new Intent(this, MyService.class);
                startService(intent);
                break;
            case R.id.stop:
                Intent intent1 = new Intent(this, MyService.class);
                stopService(intent1);
                break;
            case R.id.bind:
                Intent intent2 = new Intent(this,MyService.class);
                //BIND_AUTO_CREATE表示在Activity和Service建立关联后自动创建Service
                //这会使得MyService中的onCreate()方法得到执行,但onStartCommand()方法不会执行。
                bindService(intent2,connection,BIND_AUTO_CREATE);
                break;
            case R.id.unbind:
                unbindService(connection);
                break;
        }
    }
}
第三部分 布局部分
<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.liying.servicedemo.MainActivity">

    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:text="开启服务" />

    <Button
        android:id="@+id/stop"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:text="关闭服务" />

    <Button
        android:id="@+id/bind"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:text="绑定服务" />

    <Button
        android:id="@+id/unbind"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:text="解绑服务" />

</LinearLayout>
第四部分 清单文件部分

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.liying.servicedemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".MyService" />
    </application>

</manifest>




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值