alltolove 发表于 2017-11-10 08:50:00

android 10.3.3

修改上次项目activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xinwei.servicetest.MainActivity">

    <Button
      android:id="@+id/start_service"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="8dp"
      android:layout_marginStart="16dp"
      android:layout_marginTop="8dp"
      android:text="start service"
      app:layout_constraintBottom_toTopOf="@+id/bind_service"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.502" />

    <Button
      android:id="@+id/stop_service"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="8dp"
      android:layout_marginEnd="8dp"
      android:layout_marginStart="8dp"
      android:layout_marginTop="8dp"
      android:text="stop service"
      app:layout_constraintBottom_toTopOf="@+id/unbind_service"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.783"
      app:layout_constraintStart_toEndOf="@+id/start_service"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.502" />

    <Button
      android:id="@+id/bind_service"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="168dp"
      android:layout_marginEnd="8dp"
      android:layout_marginStart="8dp"
      android:text="bind service"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toStartOf="@+id/unbind_service"
      app:layout_constraintHorizontal_bias="0.2"
      app:layout_constraintStart_toStartOf="parent" />

    <Button
      android:id="@+id/unbind_service"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="168dp"
      android:layout_marginEnd="28dp"
      android:text="unbind service"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

mainactivity.javapackage com.example.xinwei.servicetest;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private MyService.DownloadBinder downloadBinder;
    private ServiceConnection connection=new ServiceConnection() {
      @Override
      public void onServiceConnected(ComponentName componentName, IBinder service) {
            downloadBinder=(MyService.DownloadBinder)service;
            downloadBinder.startDownload();
            downloadBinder.getProgress();
      }

      @Override
      public void onServiceDisconnected(ComponentName componentName) {

      }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button startService = (Button)findViewById(R.id.start_service);
      Button stopService = (Button)findViewById(R.id.stop_service);
      startService.setOnClickListener(this);
      stopService.setOnClickListener(this);
      Button bindService = (Button)findViewById(R.id.bind_service);
      Button unbindService = (Button)findViewById(R.id.unbind_service);
      bindService.setOnClickListener(this);
      unbindService.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
      switch (view.getId()){
            case R.id.start_service:
                Intent startIntent = new Intent(this,MyService.class);
                startService(startIntent);
                break;
            case R.id.stop_service:
                Intent stopIntent = new Intent(this,MyService.class);
                stopService(stopIntent);
                break;
            case R.id.bind_service:
                Intent bindIntent=new Intent(this,MyService.class);
                bindService(bindIntent,connection,BIND_AUTO_CREATE);
                break;
            case R.id.unbind_service:
                unbindService(connection);
                break;
            default:
                break;
      }
    }
}

页: [1]
查看完整版本: android 10.3.3