鱼C论坛

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

[学习笔记] android 10.3.2

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

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

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

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="8dp"
  14.         android:layout_marginTop="8dp"
  15.         android:text="start service"
  16.         app:layout_constraintBottom_toBottomOf="parent"
  17.         app:layout_constraintStart_toStartOf="parent"
  18.         app:layout_constraintTop_toTopOf="parent"
  19.         app:layout_constraintVertical_bias="0.498" />

  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_toBottomOf="parent"
  30.         app:layout_constraintEnd_toEndOf="parent"
  31.         app:layout_constraintHorizontal_bias="0.929"
  32.         app:layout_constraintStart_toEndOf="@+id/start_service"
  33.         app:layout_constraintTop_toTopOf="parent"
  34.         app:layout_constraintVertical_bias="0.498" />

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

新建个MyService.java的服务,系统会自动在AndroidManifest.xml里添加注册。然后修改此文件
  1. package com.example.xinwei.servicetest;

  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.util.Log;

  6. public class MyService extends Service {
  7.     private static final String TAG = "MyService";
  8.     public MyService() {
  9.     }

  10.     @Override
  11.     public IBinder onBind(Intent intent) {
  12.         // TODO: Return the communication channel to the service.
  13.         throw new UnsupportedOperationException("Not yet implemented");
  14.     }

  15.     @Override
  16.     public void onCreate() {
  17.         super.onCreate();
  18.         Log.d(TAG, "onCreate: ");
  19.     }

  20.     @Override
  21.     public int onStartCommand(Intent intent, int flags, int startId) {
  22.         Log.d(TAG, "onStartCommand: ");
  23.         return super.onStartCommand(intent, flags, startId);
  24.     }

  25.     @Override
  26.     public void onDestroy() {
  27.         super.onDestroy();
  28.         Log.d(TAG, "onDestroy: ");
  29.     }
  30. }
复制代码

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

  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.Button;

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

  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_main);
  12.         Button startService = (Button)findViewById(R.id.start_service);
  13.         Button stopService = (Button)findViewById(R.id.stop_service);
  14.         startService.setOnClickListener(this);
  15.         stopService.setOnClickListener(this);
  16.     }

  17.     @Override
  18.     public void onClick(View view) {
  19.         switch (view.getId()){
  20.             case R.id.start_service:
  21.                 Intent startIntent = new Intent(this,MyService.class);
  22.                 startService(startIntent);
  23.                 break;
  24.             case R.id.stop_service:
  25.                 Intent stopIntent = new Intent(this,MyService.class);
  26.                 stopService(stopIntent);
  27.                 break;
  28.             default:
  29.                 break;
  30.         }
  31.     }
  32. }
复制代码
当按两个按钮时logcat会出现我们写的log,如图: asasasdsas.png

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-24 20:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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