鱼C论坛

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

[学习笔记] android 10.5.2

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

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

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

x
上一节使用前台服务书上的哪种方式已经不适用了,只能启动服务跟通知分开写,这里就不多做研究了。主要说下IntentService的使用,在原来的项目中修改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.     <Button
  57.         android:id="@+id/start_intent_service"
  58.         android:layout_width="wrap_content"
  59.         android:layout_height="wrap_content"
  60.         android:layout_marginBottom="8dp"
  61.         android:layout_marginEnd="8dp"
  62.         android:layout_marginStart="8dp"
  63.         android:layout_marginTop="8dp"
  64.         android:text="start intentservice"
  65.         app:layout_constraintBottom_toBottomOf="parent"
  66.         app:layout_constraintEnd_toEndOf="parent"
  67.         app:layout_constraintStart_toStartOf="parent"
  68.         app:layout_constraintTop_toTopOf="parent"
  69.         app:layout_constraintVertical_bias="0.442" />

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

新建MyIntentService.java
  1. package com.example.xinwei.servicetest;

  2. import android.app.IntentService;
  3. import android.content.Intent;
  4. import android.support.annotation.Nullable;
  5. import android.util.Log;

  6. /**
  7. * Created by xinwei on 2017/11/11.
  8. */

  9. public class MyIntentService extends IntentService {
  10.     private static final String TAG = "MyIntentService";
  11.     public MyIntentService() {
  12.         super("MyIntentService");
  13.     }

  14.     @Override
  15.     protected void onHandleIntent(@Nullable Intent intent) {
  16.         Log.d(TAG, "onHandleIntent: "+Thread.currentThread().getId());
  17.     }

  18.     @Override
  19.     public void onDestroy() {
  20.         super.onDestroy();
  21.         Log.d(TAG, "onDestroy: ");
  22.     }
  23. }
复制代码

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

  2. import android.app.Notification;
  3. import android.app.PendingIntent;
  4. import android.content.ComponentName;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.graphics.BitmapFactory;
  8. import android.os.IBinder;
  9. import android.support.v4.app.NotificationCompat;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.Button;

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

  16.     private static final String TAG = "MainActivity";
  17.     private MyService.DownloadBinder downloadBinder;
  18.     private ServiceConnection connection=new ServiceConnection() {
  19.         @Override
  20.         public void onServiceConnected(ComponentName componentName, IBinder service) {
  21.             downloadBinder=(MyService.DownloadBinder)service;
  22.             downloadBinder.startDownload();
  23.             downloadBinder.getProgress();
  24.         }

  25.         @Override
  26.         public void onServiceDisconnected(ComponentName componentName) {

  27.         }
  28.     };
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_main);
  33.         Button startService = (Button)findViewById(R.id.start_service);
  34.         Button stopService = (Button)findViewById(R.id.stop_service);
  35.         startService.setOnClickListener(this);
  36.         stopService.setOnClickListener(this);
  37.         Button bindService = (Button)findViewById(R.id.bind_service);
  38.         Button unbindService = (Button)findViewById(R.id.unbind_service);
  39.         Button startIntentService=(Button)findViewById(R.id.start_intent_service);
  40.         bindService.setOnClickListener(this);
  41.         unbindService.setOnClickListener(this);
  42.         startIntentService.setOnClickListener(this);
  43.     }



  44.     @Override
  45.     public void onClick(View view) {
  46.         switch (view.getId()){
  47.             case R.id.start_service:
  48.                 Intent startIntent = new Intent(this,MyService.class);
  49.                 startService(startIntent);
  50.                 break;
  51.             case R.id.stop_service:
  52.                 Intent stopIntent = new Intent(this,MyService.class);
  53.                 stopService(stopIntent);
  54.                 break;
  55.             case R.id.bind_service:
  56.                 Intent bindIntent=new Intent(this,MyService.class);
  57.                 bindService(bindIntent,connection,BIND_AUTO_CREATE);
  58.                 break;
  59.             case R.id.unbind_service:
  60.                 unbindService(connection);
  61.                 break;
  62.             case R.id.start_intent_service:
  63.                 Log.d(TAG, "onClick: "+Thread.currentThread().getId());
  64.                 Intent intentService=new Intent(this,MyIntentService.class);
  65.                 startService(intentService);
  66.                 break;
  67.             default:
  68.                 break;
  69.         }
  70.     }
  71. }
复制代码

AndroidManifest.xml里application标签下添加
  1. <service android:name=".MyIntentService"/>
复制代码

启动程序后IntentService里运行的线程跟主线程会不同。

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 12:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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