鱼C论坛

 找回密码
 立即注册
查看: 1715|回复: 0

[学习笔记] android 10.3.3

[复制链接]
发表于 2017-11-10 08:50:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

  8.     <Button
  9.         android:id="@+id/start_service"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:layout_marginBottom="8dp"
  13.         android:layout_marginStart="16dp"
  14.         android:layout_marginTop="8dp"
  15.         android:text="start service"
  16.         app:layout_constraintBottom_toTopOf="@+id/bind_service"
  17.         app:layout_constraintStart_toStartOf="parent"
  18.         app:layout_constraintTop_toTopOf="parent"
  19.         app:layout_constraintVertical_bias="0.502" />

  20.     <Button
  21.         android:id="@+id/stop_service"
  22.         android:layout_width="wrap_content"
  23.         android:layout_height="wrap_content"
  24.         android:layout_marginBottom="8dp"
  25.         android:layout_marginEnd="8dp"
  26.         android:layout_marginStart="8dp"
  27.         android:layout_marginTop="8dp"
  28.         android:text="stop service"
  29.         app:layout_constraintBottom_toTopOf="@+id/unbind_service"
  30.         app:layout_constraintEnd_toEndOf="parent"
  31.         app:layout_constraintHorizontal_bias="0.783"
  32.         app:layout_constraintStart_toEndOf="@+id/start_service"
  33.         app:layout_constraintTop_toTopOf="parent"
  34.         app:layout_constraintVertical_bias="0.502" />

  35.     <Button
  36.         android:id="@+id/bind_service"
  37.         android:layout_width="wrap_content"
  38.         android:layout_height="wrap_content"
  39.         android:layout_marginBottom="168dp"
  40.         android:layout_marginEnd="8dp"
  41.         android:layout_marginStart="8dp"
  42.         android:text="bind service"
  43.         app:layout_constraintBottom_toBottomOf="parent"
  44.         app:layout_constraintEnd_toStartOf="@+id/unbind_service"
  45.         app:layout_constraintHorizontal_bias="0.2"
  46.         app:layout_constraintStart_toStartOf="parent" />

  47.     <Button
  48.         android:id="@+id/unbind_service"
  49.         android:layout_width="wrap_content"
  50.         android:layout_height="wrap_content"
  51.         android:layout_marginBottom="168dp"
  52.         android:layout_marginEnd="28dp"
  53.         android:text="unbind service"
  54.         app:layout_constraintBottom_toBottomOf="parent"
  55.         app:layout_constraintEnd_toEndOf="parent" />

  56. </android.support.constraint.ConstraintLayout>
复制代码

mainactivity.java
  1. package com.example.xinwei.servicetest;

  2. import android.content.ComponentName;
  3. import android.content.Intent;
  4. import android.content.ServiceConnection;
  5. import android.os.IBinder;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;

  10. public class MainActivity extends AppCompatActivity implements View.OnClickListener{

  11.     private MyService.DownloadBinder downloadBinder;
  12.     private ServiceConnection connection=new ServiceConnection() {
  13.         @Override
  14.         public void onServiceConnected(ComponentName componentName, IBinder service) {
  15.             downloadBinder=(MyService.DownloadBinder)service;
  16.             downloadBinder.startDownload();
  17.             downloadBinder.getProgress();
  18.         }

  19.         @Override
  20.         public void onServiceDisconnected(ComponentName componentName) {

  21.         }
  22.     };
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.         Button startService = (Button)findViewById(R.id.start_service);
  28.         Button stopService = (Button)findViewById(R.id.stop_service);
  29.         startService.setOnClickListener(this);
  30.         stopService.setOnClickListener(this);
  31.         Button bindService = (Button)findViewById(R.id.bind_service);
  32.         Button unbindService = (Button)findViewById(R.id.unbind_service);
  33.         bindService.setOnClickListener(this);
  34.         unbindService.setOnClickListener(this);
  35.     }

  36.     @Override
  37.     public void onClick(View view) {
  38.         switch (view.getId()){
  39.             case R.id.start_service:
  40.                 Intent startIntent = new Intent(this,MyService.class);
  41.                 startService(startIntent);
  42.                 break;
  43.             case R.id.stop_service:
  44.                 Intent stopIntent = new Intent(this,MyService.class);
  45.                 stopService(stopIntent);
  46.                 break;
  47.             case R.id.bind_service:
  48.                 Intent bindIntent=new Intent(this,MyService.class);
  49.                 bindService(bindIntent,connection,BIND_AUTO_CREATE);
  50.                 break;
  51.             case R.id.unbind_service:
  52.                 unbindService(connection);
  53.                 break;
  54.             default:
  55.                 break;
  56.         }
  57.     }
  58. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 20:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表