Android基礎(chǔ)12

service:服務(wù)。

  1. 開始服務(wù)
  2. 停止服務(wù)
  3. 通信,綁定活動(dòng)
  4. 通信,解綁活動(dòng)

新建service

package com.example.bbw.servicesdemo;

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

public class MyService extends Service {

    private DownloadBinder binder = new DownloadBinder();

    public MyService() {
    }

    @Override
    public void onCreate() {
        Log.d("MyService","created");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("MyService","start");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Log.d("MyService","stop");
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

     class DownloadBinder extends Binder{

        public void  startLoad(){
            Log.d("MyService","start Download");
        }

        public int getProgress(){
            Log.d("myservice","getProgress");
            return 0;
        }
    }
}

布局文件

<Button
        android:id="@+id/startService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="startService"/>

    <Button
        android:id="@+id/stopService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="stopService"/>

    <Button
        android:id="@+id/bindService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="bindService"/>

    <Button
        android:id="@+id/unBindService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="unBindService"/>

開始服務(wù)

startServices.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent startIntent = new Intent(getApplicationContext(),MyService.class);
                startService(startIntent);
            }
        });

結(jié)束服務(wù)

stopServices.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent stopIntent = new Intent(getApplicationContext(),MyService.class);
                stopService(stopIntent);
            }
        });
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,923評(píng)論 18 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,287評(píng)論 25 708
  • 1.什么是Activity?問的不太多,說點(diǎn)有深度的 四大組件之一,一般的,一個(gè)用戶交互界面對(duì)應(yīng)一個(gè)activit...
    JoonyLee閱讀 5,760評(píng)論 2 51
  • 第三次作業(yè)用時(shí)90分鐘
    珍妮寶貝_3d80閱讀 191評(píng)論 1 1