| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
上一节使用前台服务书上的哪种方式已经不适用了,只能启动服务跟通知分开写,这里就不多做研究了。主要说下IntentService的使用,在原来的项目中修改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" />
 
  
-     <Button
 
 -         android:id="@+id/start_intent_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="start intentservice"
 
 -         app:layout_constraintBottom_toBottomOf="parent"
 
 -         app:layout_constraintEnd_toEndOf="parent"
 
 -         app:layout_constraintStart_toStartOf="parent"
 
 -         app:layout_constraintTop_toTopOf="parent"
 
 -         app:layout_constraintVertical_bias="0.442" />
 
  
- </android.support.constraint.ConstraintLayout>
 
 
  复制代码 
新建MyIntentService.java- package com.example.xinwei.servicetest;
 
  
- import android.app.IntentService;
 
 - import android.content.Intent;
 
 - import android.support.annotation.Nullable;
 
 - import android.util.Log;
 
  
- /**
 
 -  * Created by xinwei on 2017/11/11.
 
 -  */
 
  
- public class MyIntentService extends IntentService {
 
 -     private static final String TAG = "MyIntentService";
 
 -     public MyIntentService() {
 
 -         super("MyIntentService");
 
 -     }
 
  
-     @Override
 
 -     protected void onHandleIntent(@Nullable Intent intent) {
 
 -         Log.d(TAG, "onHandleIntent: "+Thread.currentThread().getId());
 
 -     }
 
  
-     @Override
 
 -     public void onDestroy() {
 
 -         super.onDestroy();
 
 -         Log.d(TAG, "onDestroy: ");
 
 -     }
 
 - }
 
 
  复制代码 
然后修改mainactivity.java- package com.example.xinwei.servicetest;
 
  
- import android.app.Notification;
 
 - import android.app.PendingIntent;
 
 - import android.content.ComponentName;
 
 - import android.content.Intent;
 
 - import android.content.ServiceConnection;
 
 - import android.graphics.BitmapFactory;
 
 - import android.os.IBinder;
 
 - import android.support.v4.app.NotificationCompat;
 
 - import android.support.v7.app.AppCompatActivity;
 
 - import android.os.Bundle;
 
 - import android.util.Log;
 
 - import android.view.View;
 
 - import android.widget.Button;
 
  
- public class MainActivity extends AppCompatActivity implements View.OnClickListener{
 
  
-     private static final String TAG = "MainActivity";
 
 -     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);
 
 -         Button startIntentService=(Button)findViewById(R.id.start_intent_service);
 
 -         bindService.setOnClickListener(this);
 
 -         unbindService.setOnClickListener(this);
 
 -         startIntentService.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;
 
 -             case R.id.start_intent_service:
 
 -                 Log.d(TAG, "onClick: "+Thread.currentThread().getId());
 
 -                 Intent intentService=new Intent(this,MyIntentService.class);
 
 -                 startService(intentService);
 
 -                 break;
 
 -             default:
 
 -                 break;
 
 -         }
 
 -     }
 
 - }
 
 
  复制代码 
AndroidManifest.xml里application标签下添加- <service android:name=".MyIntentService"/>
 
  复制代码 
启动程序后IntentService里运行的线程跟主线程会不同。 |   
 
 
 
 |