欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android入門(mén)之IntentService的使用教程詳解

 更新時(shí)間:2022年12月05日 09:34:54   作者:TGITCIC  
IntentService的生命周期中有一個(gè)非常好的方法-onHandleIntent方法,它是一個(gè)abstract方法,開(kāi)發(fā)者在實(shí)現(xiàn)IntentService時(shí)可以覆蓋它來(lái)處理“長(zhǎng)事務(wù)”。本文就來(lái)聊聊IntentService的使用,需要的可以參考一下

開(kāi)篇

前一篇中我們講了bindService的使用。并且我們留下了一個(gè)念想,即在bindService取值時(shí)故意阻塞30秒,引起了一次ANR并引出了今天的章節(jié)-IntentService。

IntentService的生命周期中有一個(gè)非常好的方法-onHandleIntent方法,它是一個(gè)abstract方法,開(kāi)發(fā)者在實(shí)現(xiàn)IntentService時(shí)可以覆蓋它來(lái)處理“長(zhǎng)事務(wù)”。

IntentService

Android開(kāi)發(fā)者官網(wǎng)說(shuō)過(guò):

  • Service不是一個(gè)單獨(dú)的進(jìn)程,它和它的應(yīng)用程序在同一個(gè)進(jìn)程中
  • Service不是一個(gè)線程,這樣就意味著我們應(yīng)該避免在Service中進(jìn)行耗時(shí)操作

于是乎,Android給我們提供了解決上述問(wèn)題的替代品,就是下面要講的IntentService; IntentService是繼承與Service并處理異步請(qǐng)求的一個(gè)類,在IntentService中有 一個(gè)工作線程來(lái)處理耗時(shí)操作,請(qǐng)求的Intent記錄會(huì)加入隊(duì)列。

這么神奇?

我們來(lái)看演示,如何來(lái)驗(yàn)證這個(gè)IntentService里的onHandleIntent處理這種長(zhǎng)事務(wù)。

課程目標(biāo)

設(shè)Service里有一個(gè)字符型數(shù)組:

private String[] stdNames = new String[]{"小王", "小明", "小張"};

在Activity里輸入數(shù)組下標(biāo)后、等待30秒、然后把相對(duì)應(yīng)的數(shù)組下標(biāo)所對(duì)應(yīng)的StudentName顯示在Toast里,看看是不是會(huì)發(fā)生ANR。

該點(diǎn)擊動(dòng)作可以反復(fù)點(diǎn)擊,因?yàn)槊看吸c(diǎn)擊后都會(huì)執(zhí)行unbindService。

代碼核心設(shè)計(jì)

IntentService沒(méi)什么特殊的,它只是extends 自 IntentService,同時(shí)它擁有一個(gè)可以被覆蓋的:onHandleIntent方法。

  • 我們這次使用CallBack模式來(lái)實(shí)現(xiàn)Service里長(zhǎng)事務(wù)結(jié)束后回調(diào)activity里的handler實(shí)現(xiàn)數(shù)值傳遞;
  • 我們使用intent.putExtra來(lái)實(shí)現(xiàn)activity里的數(shù)值傳遞到service中去;

service注冊(cè)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
 
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.DemoRealIntentService"
        tools:targetApi="31">
        <service
            android:name=".LongWaitingService"
            android:exported="false"></service>
 
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
 
            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>
 
</manifest>

Service類-LongWaitingService

package org.mk.android.demorealintentservice;
 
import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.os.IBinder;
import android.util.Log;
 
 
public class LongWaitingService extends IntentService {
    private final String TAG = "LongWaitingService";
    private String[] stdNames = new String[]{"小王", "小明", "小張"};
    private Callback callback;
    private int stdNo;
 
    public class StudentBinder extends android.os.Binder {
        public LongWaitingService getService() {
            return LongWaitingService.this;
        }
    }
 
    public void setCallback(Callback callback) {
        this.callback = callback;
    }
 
    public static interface Callback {
        void onDataChange(String data);
    }
 
    public LongWaitingService() {
        super("LongWaitingService");
    }
 
    @Override
    public void onStart(Intent intent, int startId) {
        Log.i(TAG, ">>>>>>onStart");
    }
        @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            Log.i(TAG, ">>>>>>onHandleIntent");
            Log.i(TAG, ">>>>>>into a long waiting");
            new Thread() {
                public void run() {
                    try {
                        Thread.sleep(30000);
                        if (callback != null) {
                            String stdName = stdNames[stdNo];
                            callback.onDataChange(stdName);
                        }
                    } catch (Exception e) {
                    }
                }
            }.start();
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.i(TAG, ">>>>>>onBind方法被調(diào)用");
        this.stdNo = intent.getIntExtra("stdNo", -1);
        onHandleIntent(intent);
        return new StudentBinder();
    }
    //Service被關(guān)閉前回調(diào)
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, ">>>>>>onDestroyed方法被調(diào)用!");
    }
    @Override
 
    public boolean onUnbind(Intent intent) {
 
        Log.i(TAG,">>>>>>onUnbind");
 
        return false;
 
    }
}

主類-MainActivity.java

package org.mk.android.demorealintentservice;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
    private final String TAG = "LongWaitingService";
    private Button buttonGetValueFromBinder;
    private Button buttonClose;
    private Context ctx;
    private Intent intent;
    private LongWaitingService.StudentBinder stdBinder;
    private EditText etStdNo;
    Handler stdHandler = new StudentHandler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonGetValueFromBinder = (Button) findViewById(R.id.buttonGetValueFromBinder);
        etStdNo = (EditText) findViewById(R.id.etStdNo);
        ctx = MainActivity.this;
        intent = new Intent(ctx, LongWaitingService.class);
 
        buttonGetValueFromBinder.setOnClickListener(new OnClickListener());
    }
    private ServiceConnection conn = new ServiceConnection() {
 
        //Activity與Service斷開(kāi)連接時(shí)回調(diào)該方法
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, ">>>>>>Service DisConnected");
        }
 
        //Activity與Service連接成功時(shí)回調(diào)該方法
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, ">>>>>>Service Connected");
            stdBinder = (LongWaitingService.StudentBinder) service;
            LongWaitingService stdService = stdBinder.getService();
            stdService.setCallback(new LongWaitingService.Callback() {
                @Override
                public void onDataChange(String data) {
                    Message msg = new Message();
                    msg.obj = data;
                    stdHandler.sendMessage(msg);
                }
            });
        }
    };
 
    class OnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            Intent eIntent;
            switch (view.getId()) {
                case R.id.buttonGetValueFromBinder:
                    int stdNo = Integer.valueOf(etStdNo.getText().toString());
                    intent.putExtra("stdNo", stdNo);
                    bindService(intent, conn, Service.BIND_AUTO_CREATE);
                    break;
            }
        }
    }
    class StudentHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            Log.i(TAG,">>>>>>Service的count" + "的值為:" + msg.obj.toString());
            Toast.makeText(getApplicationContext(), "Service的count" + "的值為:" + msg.obj.toString(), Toast.LENGTH_LONG).show();
            unbindService(conn);
        }
    }
}

運(yùn)行效果

來(lái)看看運(yùn)行效果吧

看,再也沒(méi)有ANR了,結(jié)果成功通過(guò)CALL BACK回傳Activity。

因此我們一般都會(huì)這么使用IntentService來(lái)實(shí)現(xiàn)一些資源異步加載、第三方API回調(diào)。

到此這篇關(guān)于Android入門(mén)之IntentService的使用教程詳解的文章就介紹到這了,更多相關(guān)Android IntentService內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論