IntentServic

这个例子是在我上一篇文章 的基础上修改的
Service启动和停止
https://www.jianshu.com/p/760a7a61bcd0

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.hzx.myservice.MainActivity">

    <Button
        android:id="@+id/start_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Service"/>

    <Button
        android:id="@+id/stop_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop Service"/>

    <Button
        android:id="@+id/bind_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bind Service"/>

    <Button
        android:id="@+id/unbind_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Unbind Service"/>

    <Button
        android:id="@+id/start_intent_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start IntetnService"/>
</LinearLayout>


新建一个类集成IntentServic

package com.example.hzx.myservice;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by hzx on 2018/11/5.
 */

public class MyIntentService extends IntentService {

    private static final String TAG = "123";

    public MyIntentService(){
        super("MyIntentService");
        Log.d(TAG,"==>MyIntentService");
    }
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.d(TAG,"==>Thread id is "+Thread.currentThread().getId());

    }
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"==>onDestroy");
    }

}

Activity

package com.example.hzx.myservice;

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 final String TAG = "123";
    private MyService.DownloadBinder downloadBinder;

    private ServiceConnection connetction = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            downloadBinder = (MyService.DownloadBinder)iBinder;
            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(MainActivity.this);
        stopService.setOnClickListener(MainActivity.this);

        Button bingService = (Button)findViewById(R.id.bind_service);
        Button unbindService = (Button)findViewById(R.id.unbind_service);

        bingService.setOnClickListener(MainActivity.this);
        unbindService.setOnClickListener(MainActivity.this);


        Button startIntentService = (Button)findViewById(R.id.start_intent_service);
        startIntentService.setOnClickListener(MainActivity.this);

    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.start_service:
                Log.d(TAG,"activity start_service");
                Intent startIntent = new Intent(this,MyService.class);
                startService(startIntent);
                break;
            case R.id.stop_service:
                Log.d(TAG,"activity stop_sercie");
                Intent stopIntent = new Intent(this,MyService.class);
                stopService(stopIntent);
                break;
            case R.id.bind_service:
                Log.d(TAG,"activity bind_service");
                Intent bindIntent = new Intent(this,MyService.class);
                bindService(bindIntent,connetction,BIND_AUTO_CREATE);
                break;
            case R.id.unbind_service:
                Log.d(TAG,"activity unbind_service");
                unbindService(connetction);
                break;
            case R.id.start_intent_service:
                Log.d(TAG,"activity Thread id is "+Thread.currentThread().getId());
                Intent intentService = new Intent(this,MyIntentService.class);
                startService(intentService);
                break;
                default:
                    break;
        }
    }
}


注意:
1、 需要在AndroidManifest.xml文件注册
<service android:name=".MyService" />
<service android:name=".MyIntentService"/>

效果


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,441评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,960评论 2 59
  • 面试必背 会舍弃、总结概括——根据我这些年面试和看面试题搜集过来的知识点汇总而来 建议根据我的写的面试应对思路中的...
    luoyangzk阅读 6,814评论 6 173
  • 阳光正好,微风不燥! 今天的天气好的有点让我不敢相信了简直,从早上的第一份早餐开始,看着她们跳舞,...
    童心童画可乐老师1553463阅读 211评论 0 0
  • 在我倾听Bettina Warburg的演讲之后,我对去中心化经济(dApps)的概念着迷。传统的Web应用程序是...
    编程狂魔阅读 460评论 0 2