So sánh giữa asynctask and asynctaskloader android năm 2024

Search

Language

  • English(selected)
  • Español
  • Português
  • Deutsch
  • Français
  • Русский
  • Italiano
  • Română
  • Bahasa Indonesia

Upload

Welcome to Scribd!

  • Upload
  • Read for free
  • FAQ and support
  • What is Scribd?
  • Documents(selected)
  • Explore Documents

    Categories

    • Academic Papers
    • Business Templates
    • Court Filings
    • All documents
    • Sports & Recreation
      • Bodybuilding & Weight Training
      • Boxing
      • Martial Arts
    • Religion & Spirituality
      • Christianity
      • Judaism
      • New Age & Spirituality
      • Buddhism
      • Islam
    • Art
      • Music
      • Performing Arts
    • Wellness
      • Body, Mind, & Spirit
      • Weight Loss
    • Self-Improvement
    • Technology & Engineering
    • Politics
      • Political Science All categories

Uploaded by

tiến nguyễn công

0% found this document useful (0 votes)

6 views

841 pages

Original Title

Chapter 9. Share Preference-đã Gộp

Copyright

© © All Rights Reserved

Available Formats

PDF, TXT or read online from Scribd

Share this document

Did you find this document useful?

Is this content inappropriate?

0% found this document useful (0 votes)

6 views841 pages

Chapter 9. Share Preference-Đã G P

Uploaded by

tiến nguyễn công

Jump to Page

You are on page 1of 841

Search inside document

Phát triển ứng dụng cho thiết bị di động

Shared Preference

So sánh giữa asynctask and asynctaskloader android năm 2024

Note

Các slide trong bài giảng này dựa trên các bài thực hành của Google Android Labs

Original slides: https://drive.google.com/drive/folders/1eu-LXxiHocSktGYpG04PfE9Xmr_pBY5P

2

So sánh giữa asynctask and asynctaskloader android năm 2024

So sánh giữa asynctask and asynctaskloader android năm 2024

9.1 Shared Preferences

3

So sánh giữa asynctask and asynctaskloader android năm 2024

Reward Your Curiosity

Everything you want to read.

Anytime. Anywhere. Any device.

No Commitment. Cancel anytime.

So sánh giữa asynctask and asynctaskloader android năm 2024

Một dịch vụ (Service) là một thành phần chạy ngầm trên hệ điều hành để thực hiện các hoạt động dài hạn mà không cần phải tương tác với người sử dụng và nó hoạt động ngay cả khi ứng dụng bị phá hủy. Một dịch vụ cơ bản có thể có hai loại.

Một dịch vụ được gọi là started (được khởi động) khi một thành phần ứng dụng, chẳng hạn như Activity khởi động nó bằng cách gọi startService(). Một khi được gọi, dịch vụ này có thể chạy ở chế độ nền vô thời hạn, thậm chí cả khi thành phần đã khởi động nó bị phá hủy.

Dịch vụ này còn được gọi là dịch vụ không bị giàng buộc (Un Bounded Service).

Một dịch vụ được giàng buộc (bound) khi một thành phần ứng dụng giàng buộc nó bằng cách gọi bindService(). Một dịch vụ ràng buộc cung cấp một giao diện client-server cho phép các thành phần tương tác với dịch vụ, gửi các yêu cầu, nhận kết quả, và thậm chí làm như vậy xuyên qua nhiều tiến trình với Interprocess communication (IPC) (Truyền thông nhiều tiến trình).

Trong khoa học máy tính, inter-process communication (IPC) là hoạt động chia sẻ dữ liệu qua nhiều tiến trình chuyên dụng thông thường sử dụng giao thức truyền thông. Cụ thể ứng dụng sử dụng IPC được phân ra như clients và servers, khi các clients yêu cầu dữ liệu, và server đáp ứng yêu cầu của client.

Một dịch vụ có phương thức gọi lại chu kỳ vòng đời của nó (life cycle callback methods) mà bạn có thực hiện (implement) để theo dõi những thay đổi trong trạng thái của dịch vụ và bạn có thể thực hiện công việc ở giai đoạn thích hợp. Sơ đồ dưới đây về bên trái cho thấy vòng đời khi dịch vụ được tạo ra với startService(), sơ đồ bên phải cho thấy vòng đời của dịch vụ được tạo ra bởi bindService().

So sánh giữa asynctask and asynctaskloader android năm 2024

Để tạo ra một dịch vụ, bạn tạo một lớp Java mở rộng lớp Service hoặc một trong các lớp con của nó. Lớp Service định nghĩa các phương thức callback khác nhau và quan trọng nhất được đưa ra dưới đây. Bạn không cần phải thực hiện (implements) tất cả các phương thức callbacks. Tuy nhiên, điều quan trọng là bạn hiểu mỗi phương thức thực hiện những điều gì, đảm bảo ứng dụng của bạn cư xử theo cách người dùng mong đợi.

Ngoài 2 loại dịch vụ trên, có một dịch vụ khác gọi là IntentService. Intent Service được sử dụng để thực hiện các nhiệm vụ một lần duy nhất, nghĩa là khi nhiệm vụ hoàn thành dịch vụ tự hủy.

So sánh giữa asynctask and asynctaskloader android năm 2024

So sánh các loại dịch vụ:

Unbound Service (Không giàng buộc)

Bound Service (Giàng buộc)

Intent Service

Unbounded Service được sử dụng để thực hiện nhiệm vụ lâu dài và lặp đi lặp lại.

Bounded Service được sử dụng để thực hiện nhiệm vụ ở nền (background) và giàng buộc với thành phần giao diện.

Intent Service được sử dụng để thực hiện các nhiệm vụ một lần duy nhất, nghĩa là khi nhiệm vụ hoàn thành dịch vụ tự hủy.

Unbound Service được khởi động bởi gọi startService().

Bounded Service được khởi động bởi gọi bindService().

Intent Service được khởi động bởi gọi startService().

Unbound Service bị dừng lại hoặc bị hủy bởi gọi một cách tường minh phương thức stopService().

Bounded Service bị gỡ giàng buộc hoặc bị hủy bởi gọi unbindService().

IntentService gọi một cách không tường minh phương thức stopself() để hủy

Unbound Service độc lập với thành phần đã khởi động nó.

Bound Service phụ thuộc vào thành phần giao diện đã khởi động nó.

Intent Service độc lập với thành phần đã khởi động nó.

Các phương thức callback và mô tả:

Callback

Description

onStartCommand()

Hệ thống gọi phương thức này khi một thành phần khác, chẳng hạn như một Activity, yêu cầu khởi động dịch vụ, bằng cách gọi startService(). Nếu bạn thực thi phương pháp này, trách nhiệm của bạn là ngừng dịch vụ khi nó hoàn thành công việc, bằng cách gọi phương thức stopSelf() hoặc stopService().

onBind()

Hệ thống gọi phương thức này khi thành phần khác muốn liên kết với các dịch vụ bằng cách gọi bindService(). Nếu bạn thi hành phương pháp này, bạn phải cung cấp một giao diện (Giao diện ứng dụng) mà khách hàng sử dụng để giao tiếp với các dịch vụ, bằng cách trả lại một đối tượng IBinder. Bạn phải luôn luôn thi hành phương thức này, nhưng nếu bạn không muốn cho phép ràng buộc, bạn có thể trả về null.

onUnbind()

Hệ thống gọi phương thức này khi tất cả các clients đã bị ngắt kết nối từ một giao diện cụ thể được công bố bởi các dịch vụ.

onRebind()

Hệ thống gọi phương thức này khi khách hàng mới đã kết nối với dịch vụ, sau khi trước đó đã được thông báo rằng tất cả đã bị ngắt kết nối trong onUnbind(Intent).

onCreate()

Hệ thống gọi phương thức này khi dịch vụ được tạo ra sử dụng đầu tiên onStartCommand() hoặc onBind(). Gọi một lần tại thời điểm thiết lập.

onDestroy()

Hệ thống gọi phương thức này khi dịch vụ không còn được sử dụng và đang bị hủy (destroy). Dịch vụ của bạn nên thi hành điều này để dọn dẹp các dữ liệu rác...

2. Dịch vụ không bị giàng buộc (Un bounded Service)

Unbound Service (hoặc còn gọi là Started Service): Trong trường hợp này, một thành phần ứng dụng khởi động dịch vụ bằng cách gọi startService(), và dịch vụ sẽ tiếp tục chạy trong nền (background), ngay cả khi các thành phần khởi tạo nó bị phá hủy. Ví dụ, khi được bắt đầu, một dịch vụ sẽ tiếp tục chơi nhạc trong nền vô thời hạn.

So sánh giữa asynctask and asynctaskloader android năm 2024

Phương thức onStartCommand() trả về kiểu integer, và là một trong các giá trị sau:

  • START_STICKY
  • START_NOT_STICKY
  • TART_REDELIVER_INTENT
    START_STICKY & START_NOT_STICKY

Cả hai giá trị này chỉ thích hợp khi điện thoại hết bộ nhớ và giết các dịch vụ trước khi nó kết thúc thực hiện.

START_STICKY nói với các hệ điều hành để tạo lại các dịch vụ sau khi đã có đủ bộ nhớ và gọi onStartCommand() một lần nữa với một Intent null.

START_NOT_STICKY nói với các hệ điều hành để không bận tâm tái tạo các dịch vụ một lần nữa.

Ngoài ra còn có một START_REDELIVER_INTENT giá trị thứ ba mà nói với các hệ điều hành để tạo lại các dịch vụ và truyền một Intent tương tự cho onStartCommand().

Ví dụ dịch vụ chơi nhạc (Chạy ngầm)

Tạo mới một "Empty Activity" project với tên PlaySongService

So sánh giữa asynctask and asynctaskloader android năm 2024

  • Name: PlaySongService
  • Package name: org.o7planning.playsongservice

So sánh giữa asynctask and asynctaskloader android năm 2024

Project đã được tạo ra.

So sánh giữa asynctask and asynctaskloader android năm 2024

Chuẩn bị file nhạc mp3:

Nhấn phải chuột vào thư mục res chọn:

  • New > Folder > Raw Resources Folder

So sánh giữa asynctask and asynctaskloader android năm 2024

Copy và Paste một file nhạc mp3 vào thư mục 'raw' bạn vừa tạo ra.

So sánh giữa asynctask and asynctaskloader android năm 2024

So sánh giữa asynctask and asynctaskloader android năm 2024

Thiết kế giao diện của ứng dụng:

So sánh giữa asynctask and asynctaskloader android năm 2024

activity_main.xml



    

Tạo lớp Service

Nhấn phải chuột vào một java package, chọn:

  • New > Service > Service

So sánh giữa asynctask and asynctaskloader android năm 2024

Nhập vào tên lớp:

  • PlaySongService

So sánh giữa asynctask and asynctaskloader android năm 2024

So sánh giữa asynctask and asynctaskloader android năm 2024

Bạn có thể nhìn thấy PlaySongService đã được khai báo với AndroidManifest.xml:

** AndroidManifest.xml **



    
        
            ....
    

PlaySongService.java

package org.o7planning.playsongservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.media.MediaPlayer;
public class PlaySongService extends Service {
    private MediaPlayer mediaPlayer;
    public PlaySongService() {
    }
    // Return the communication channel to the service.
    @Override
    public IBinder onBind(Intent intent){
        // This service is unbounded
        // So this method is never called.
        return null;
    }
    @Override
    public void onCreate(){
        super.onCreate();
        // Create MediaPlayer object, to play your song.
        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.mysong);
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        // Play song.
        mediaPlayer.start();
        return START_STICKY;
    }
    // Destroy
    @Override
    public void onDestroy() {
        // Release the resources
        mediaPlayer.release();
        super.onDestroy();
    }
}

MainActivity.java

package org.o7planning.playsongservice;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
    private Button buttonPlay;
    private Button buttonStop;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.buttonPlay = (Button) this.findViewById(R.id.button_play);
        this.buttonStop = (Button) this.findViewById(R.id.button_stop);
        this.buttonPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playSong();
            }
        });
        this.buttonStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopSong();
            }
        });
    }
    // This method is called when users click on the Play button.
    public void playSong()  {
        // Create Intent object for PlaySongService.
        Intent myIntent = new Intent(MainActivity.this, PlaySongService.class);
        // Call startService with Intent parameter.
        this.startService(myIntent);
    }
    // This method is called when users click on the Stop button.
    public void stopSong( )  {
        // Create Intent object
        Intent myIntent = new Intent(MainActivity.this, PlaySongService.class);
        this.stopService(myIntent);
    }
}

OK bây giờ bạn có thể chạy ứng dụng của mình và thưởng thức bài hát.

So sánh giữa asynctask and asynctaskloader android năm 2024

3. Dịch vụ bị giàng buộc (Bouned Service)

Ở đây tôi mô phỏng một dịch vụ cung cấp thông tin thời tiết cho ngày hiện tại, với đầu vào là vị trí địa lý (Hanoi, Chicago, ...), kết quả trả về là mưa, nắng,...

Tạo một project có tên WeatherService.

So sánh giữa asynctask and asynctaskloader android năm 2024

  • Name: WeatherService
  • Package name: org.o7planning.weatherservice

So sánh giữa asynctask and asynctaskloader android năm 2024

Thiết kế giao diện cho ứng dụng:

So sánh giữa asynctask and asynctaskloader android năm 2024

So sánh giữa asynctask and asynctaskloader android năm 2024

activity_main.xml



    
    
    
    

Tạo Service:

Nhấn phải chuột vào một java package chọn:

  • New > Service > Service

So sánh giữa asynctask and asynctaskloader android năm 2024

Nhập vào:

  • Class name: WeatherService

So sánh giữa asynctask and asynctaskloader android năm 2024

Lớp WeatherService đã được tạo ra, đây là class mở rộng từ class android.app.Service.

So sánh giữa asynctask and asynctaskloader android năm 2024

Bạn có thể nhìn thấy WeatherService đã được khai báo với AndroidManifest.xml:

** AndroidManifest.xml **



    
        
        ...
    

WeatherService.java

package org.o7planning.weatherservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Binder;
import android.util.Log;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class WeatherService extends Service {
    private static String LOG_TAG = "WeatherService";
    // Store the weather data.
    private static final Map weatherData = new HashMap();
    private final IBinder binder = new LocalWeatherBinder();
    public class LocalWeatherBinder extends Binder {
        public WeatherService getService()  {
            return WeatherService.this;
        }
    }
    public WeatherService() {
    }
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(LOG_TAG,"onBind");
        return this.binder;
    }
    @Override
    public void onRebind(Intent intent) {
        Log.i(LOG_TAG, "onRebind");
        super.onRebind(intent);
    }
    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(LOG_TAG, "onUnbind");
        return true;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(LOG_TAG, "onDestroy");
    }
    // Returns the weather information corresponding to the location of the current date.
    public String getWeatherToday(String location) {
        Date now= new Date();
        DateFormat df= new SimpleDateFormat("dd-MM-yyyy");
        String dayString = df.format(now);
        String keyLocAndDay = location + "$"+ dayString;
        String weather=  weatherData.get(keyLocAndDay);
        //
        if(weather != null)  {
            return weather;
        }
        //
        String[] weathers = new String[]{"Rainy", "Hot", "Cool", "Warm" ,"Snowy"};
        // Random value from 0 to 4
        int i= new Random().nextInt(5);
        weather =weathers[i];
        weatherData.put(keyLocAndDay, weather);
        //
        return weather;
    }
}

MainActivity.java

package org.o7planning.weatherservice;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    private boolean binded = false;
    private WeatherService weatherService;
    private TextView textViewWeather;
    private EditText editTextLocation;
    private Button buttonWeather;
    ServiceConnection weatherServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            WeatherService.LocalWeatherBinder binder = (WeatherService.LocalWeatherBinder) service;
            weatherService = binder.getService();
            binded = true;
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            binded = false;
        }
    };
    // When the Activity creating its interface.
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.textViewWeather = (TextView) this.findViewById(R.id.textView_weather);
        this.editTextLocation = (EditText) this.findViewById(R.id.editText_location);
        this.buttonWeather = (Button) this.findViewById(R.id.button_weather);
        this.buttonWeather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 showWeather();
            }
        });
    }
    // When Activity starting.
    @Override
    protected void onStart() {
        super.onStart();
        // Create Intent object for WeatherService.
        Intent intent = new Intent(this, WeatherService.class);
        // Call bindService(..) method to bind service with UI.
        this.bindService(intent, weatherServiceConnection, Context.BIND_AUTO_CREATE);
    }
    // Activity stop
    @Override
    protected void onStop() {
        super.onStop();
        if (binded) {
            // Unbind Service
            this.unbindService(weatherServiceConnection);
            binded = false;
        }
    }
    // When user click on 'Show weather' button.
    public void showWeather()  {
        String location = this.editTextLocation.getText().toString();
        String weather= this.weatherService.getWeatherToday(location);
        this.textViewWeather.setText(weather);
    }
}

OK, giờ bạn có thể chạy ứng dụng.

So sánh giữa asynctask and asynctaskloader android năm 2024

4. Dịch vụ IntentService

Ví dụ IntentService:

Hình ảnh dưới đây minh họa cách giao tiếp giữa Client (Activity) và IntentService, Client khởi động dịch vụ, nó gửi yêu cầu của nó thông qua một đối tượng Intent, dịch vụ chạy và làm các công việc của nó, đồng thời nó có thể gửi thông tin liên quan tới tình trạng công việc của nó, chẳng hạn làm được bao nhiêu phần trăm. Tại client có thể sử dụng ProgressBar để hiển thị phần trăm công việc đã làm được.

So sánh giữa asynctask and asynctaskloader android năm 2024

Các IntentService được thiết kế để tự động stop một cách tự nhiên khi công việc hoàn thành, và chỉ sử dụng một lần, vì vậy bạn nên sử dụng nó trong các tình huống như vậy. Phương thức .stopService(intentService) sẽ không hoạt động với IntentService. Hơn nữa, rất khó để bạn sử dụng UI của ứng dụng để tương tác với IntentService.

So sánh giữa asynctask and asynctaskloader android năm 2024

Tạo mới một project SimpleIntentService.

So sánh giữa asynctask and asynctaskloader android năm 2024

  • Name: SimpleIntentService
  • Package name: org.o7planning.simpleintentservice

So sánh giữa asynctask and asynctaskloader android năm 2024

Thiết kế giao diện:

So sánh giữa asynctask and asynctaskloader android năm 2024

So sánh giữa asynctask and asynctaskloader android năm 2024

activity_main.xml



    
    
    

Tạo một IntentService bằng cách nháy phải chuột vào một package, chọn:

  • New > Service > Service (IntentService)

So sánh giữa asynctask and asynctaskloader android năm 2024

So sánh giữa asynctask and asynctaskloader android năm 2024

So sánh giữa asynctask and asynctaskloader android năm 2024

Bạn có thể nhìn thấy SimpleIntentService đã được khai báo với AndroidManifest.xml:

** AndroidManifest.xml **



    
        
        ...
    

Lớp SimpleIntentService đã được tạo ra, nó cũng đã được đăng ký với AndroidManifest.xml, code được tạo ra là một gợi ý cho bạn viết một IntentService, bạn có thể xóa hết các code được tạo ra.