Service与Intent(一)

Fancis学习笔记二

          -------------------Service与Intent(一)                   

Where there is a will, there is a way!

1.Log输出学习;

2.Service与Intent学习;

3.Service生命周期;

4.adb安装APK;


一、Service学习根据sundy视频总结及网上资料查找

service不是一个独立的进程,默认情况下运行在应用程序的主线程中。

启动服务后,按Home键,服务还会执行。

启动服务后,退出进程,服务不会执行。

启动服务,不用多线程,界面会阻塞。

Service的分类:

按照启动方式分类

(一)Started

上个图:


1、startService()来启动。

2、一旦启动,就运行在后台,即便启动它的对象都销毁了。

3、通常只启动,不返回值。

4、通常网络上传或下载,操作完成后,自动停止。

5、onStartCommand()

(二)Bound

1、bindService()来启动

2、提供客户端服务器接口来启动

3、发送请求、得到返回值,甚至通过IPC来通讯

4、使用者绑定,只要一个绑定者,服务运行,所有绑定者都退出,服务退出。

5、onBind()

Service生命周期:

onCreate()

onDestory()

onStartCommand()

onBind()

Service只有在系统资源不足的时候才有可能被系统销毁。值得注意的是:Service如果正在bind状态,很少被销毁,如果服务运行在前台,几乎不会被销毁。

创建和使用Service步骤:

1、继承Service类实现自己的服务

2、在Mainifest.xml中注册服务

3、启动服务startService()

4、停止服务 stopService()、stopSelf()

绑定Service的步骤

1、继承Service类或者IntentService类实现自己的服务。

2、在Mainifest.xml中注册服务

3、绑定服务bindService()

4、取消绑定服务unbinService()

第二类型service参考本链接:http://blog.csdn.net/blueskydyliu/article/details/6741223

二、Android-StartService和BoundService的区别

1. 两者与Activity的关系 
StartService和启动它的Activity不同生共死,进程关闭了,该服务类型不会关闭
BoundService和启动它的Activity同生共死,进程关闭了,该服务类型会关闭
2 .多次start 或者 bind观察service的实例化的数量
两种方式,只要没有被实例化,都会被create,
但是多次的实例化时:start每次都会onstartcommand。
而bind,则每次都不调用onBind
3 .音乐播放器的例子
使用startService的原因:
1 )不与进程同生共死
2 )每次都启动onStartCommand
不使用BoundService的原因:
1 )与进程同生共死
2 )只能绑定一次
4 两者最本质区别
BoundService:其实有一个客户端和服务器端的概念,,借助binder来通信。

源码一:

第一个类:MainActivity

package myapplication.com.servicedemo;

import android.content.Intent;
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 {

    private Intent _intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button bt = (Button) findViewById(R.id.startButton);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _intent = new Intent(MainActivity.this, TestService.class);
                MainActivity.this.startService(_intent);

            }
        });

        Button bt2 = (Button) findViewById(R.id.stopbutton);
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (_intent != null) {
                    MainActivity.this.stopService(_intent);
                    Log.i("stopService", "print service");
                }
            }
        });
    }
}

第二个类:TestService

package myapplication.com.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by Administrator on 2016/5/14.
 */
public class TestService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    Log.i("servicetest", "print number:" + i);

                }


            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("on Destroy", "Destroy");

    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="myapplication.com.servicedemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/startButton"
        android:text="startService"
        android:layout_marginTop="167dp"
        android:layout_below="@+id/textView"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/stopbutton"
        android:text="stopService"
        android:layout_below="@+id/startButton"
        android:layout_alignRight="@+id/startButton"
        android:layout_alignEnd="@+id/startButton"
        android:layout_marginTop="58dp" />
</RelativeLayout>
AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="myapplication.com.servicedemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".TestService" />

    </application>

</manifest>

三、Service生命周期

上图:


四、adb安装APK,adb install APK本地目录

更多内容会在后续更新,小白正在努力学习中,感谢同各位分享!



 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值