Jetpack是google官方的安卓開發(fā)工具集马胧,目的是為了標(biāo)準(zhǔn)化和加快開發(fā)效率汉买,并且之后會持續(xù)更新
安卓開發(fā)中,google推薦使用MVVM架構(gòu)佩脊,Jetpack集成了構(gòu)建MVVM架構(gòu)的幾種工具蛙粘,相比于以前的MVVM,組合使用這些工具會更加高效威彰、簡潔出牧、安全⌒危可以說你的安卓項(xiàng)目沒有升級使用這套架構(gòu)舔痕,那么你的架構(gòu)就已經(jīng)過時(shí)了
JetPack與AndroidX
- AndroidX命名空間中包含Jetpack庫
- AndroidX代替Android Support Library
- AAC(Android Architect Component)中的組件并入AndroidX
- 其他一些需要頻繁更新和迭代的特性也并入AndroidX
一、LifeCycle
LifeCycle會自動(dòng)綁定組件的生命周期豹缀,省去了我們以前在onResume伯复,onPause等方法中的操作
1.LifeCycle解耦頁面與組件
我們有一個(gè)Activity,布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(0):(0)"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
這是一個(gè)簡單的計(jì)時(shí)器邢笙,我們想要在Activity處于前臺時(shí)計(jì)時(shí)啸如,退到后臺暫停計(jì)時(shí),那么Activity中寫法如下:
package com.aruba.jetpackapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.SystemClock;
import android.widget.Chronometer;
public class MainActivity extends AppCompatActivity {
private Chronometer chronometer;
private long countTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chronometer = findViewById(R.id.chronometer);
}
@Override
protected void onResume() {
super.onResume();
//恢復(fù)計(jì)時(shí)氮惯,基于休息的時(shí)間作一個(gè)偏移
chronometer.setBase(SystemClock.elapsedRealtime() - countTime);
chronometer.start();
}
@Override
protected void onPause() {
super.onPause();
//記錄下計(jì)時(shí)時(shí)間
countTime = SystemClock.elapsedRealtime() - chronometer.getBase();
chronometer.stop();
}
}
效果:
接下來是使用LifeCycle方式:
1.實(shí)現(xiàn)LifecycleObserver接口
2.在方法上添加注解叮雳,指定在什么時(shí)候執(zhí)行
package com.aruba.lifecycle;
import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.Chronometer;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
/**
* Created by aruba on 2021/9/9.
*/
class MyChronometer extends Chronometer implements LifecycleObserver {//實(shí)現(xiàn)LifecycleObserver接口
private long countTime;
public MyChronometer(Context context, AttributeSet attrs) {
super(context, attrs);
}
//對應(yīng)生命周期的注解
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private void startCount() {
//恢復(fù)計(jì)時(shí),基于休息的時(shí)間作一個(gè)偏移
setBase(SystemClock.elapsedRealtime() - countTime);
start();
}
//對應(yīng)生命周期的注解
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
private void stopCount() {
//記錄下計(jì)時(shí)時(shí)間
countTime = SystemClock.elapsedRealtime() - getBase();
stop();
}
}
把布局文件改為MyChronometer 后筐骇,在Activity中添加一行監(jiān)聽代碼
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
MyChronometer chronometer = findViewById(R.id.chronometer);
getLifecycle().addObserver(chronometer);
}
}
效果:
2.使用LifecycleService解耦Service與組件
首先需要添加下依賴
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
定義一個(gè)類债鸡,實(shí)現(xiàn)LifecycleObserve接口江滨,并實(shí)現(xiàn)gps數(shù)據(jù)獲取
package com.aruba.lifecycle;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
/**
* Created by aruba on 2021/9/9.
*/
public class MyLocationObserver implements LifecycleObserver {
private Context context;
private LocationManager locationManager;
private MyLocation listener;
public MyLocationObserver(Context context) {
this.context = context;
}
/**
* 開啟gps
*/
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private void startGetLocation() {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
listener = new MyLocation();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300, 1, listener);
}
/**
* 關(guān)閉gps
*/
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private void stopGetLocation() {
locationManager.removeUpdates(listener);
}
static class MyLocation implements LocationListener {
private static final String TAG = MyLocation.class.getSimpleName();
@Override
public void onLocationChanged(@NonNull Location location) {
Log.i(TAG, location.toString());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(@NonNull String provider) {
}
@Override
public void onProviderDisabled(@NonNull String provider) {
}
}
}
權(quán)限在manifests.xml里也要添加
使用一個(gè)service來獲取gps數(shù)據(jù)铛纬,繼承于LifecycleService,并在相關(guān)方法上寫上注解
package com.aruba.lifecycle;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import androidx.lifecycle.LifecycleService;
public class GpsService extends LifecycleService {
public GpsService() {
MyLocationObserver myLocationObserver = new MyLocationObserver(this);
getLifecycle().addObserver(myLocationObserver);
}
}
模擬器可以使用adb命令修改下gps位置
adb -s Pixel2:5554 emu geo fix 121.4961236714487 31.24010934431376
adb -s Pixel2:5554 emu geo fix 122.4961236714487 31.24010934431376
Activity中開啟測試下效果:
2021-09-09 17:05:03.316 3046-3046/com.aruba.lifecycle I/MyLocation: Location[gps 37.421998,-122.084000 acc=20 et=+1m13s108ms alt=0.0 {Bundle[EMPTY_PARCEL]}]
3.ProcessLifecycleOwner監(jiān)聽?wèi)?yīng)用程序生命周期
新建一個(gè)類實(shí)現(xiàn)LifecycleObserve唬滑,在方法上加上注解告唆,指定想要監(jiān)聽的生命周期
package com.aruba.lifecycle;
import android.util.Log;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
/**
* Created by aruba on 2021/9/9.
*/
class AppLifeObserve implements LifecycleObserver {
private static final String TAG = AppLifeObserve.class.getSimpleName();
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private void onCreate() {
Log.i(TAG, "onCreate");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
private void onStart() {
Log.i(TAG, "onCreate");
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private void onResume() {
Log.i(TAG, "onResume");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
private void onPause() {
Log.i(TAG, "onPause");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private void onStop() {
Log.i(TAG, "onStop");
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private void onDestroy() {
Log.i(TAG, "onDestroy");
}
}
在Application中棺弊,使用ProcessLifecycleOwner注冊觀察
package com.aruba.lifecycle;
import android.app.Application;
import androidx.lifecycle.ProcessLifecycleOwner;
/**
* Created by aruba on 2021/9/9.
*/
class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifeObserve());
}
}
其中onCreate只會調(diào)用一次,onDestroy不會調(diào)用
Lifecycle可以使我們不必在原來組件的生命周期中進(jìn)行管理擒悬,降低了模塊的耦合度模她,一定程度上避免了沒有及時(shí)銷毀資源的情況,降低了內(nèi)存泄漏的發(fā)生
二懂牧、ViewModel
Jetpack中侈净,官方提供了ViewModel組件,我們應(yīng)該繼承它實(shí)現(xiàn)我們的ViewModel層業(yè)務(wù)
1.瞬態(tài)數(shù)據(jù)保存
例如以前我們手機(jī)屏幕旋轉(zhuǎn)時(shí)僧凤,如果沒有配置畜侦,那么Activity會重新創(chuàng)建,數(shù)據(jù)就會丟失
使用ViewModel躯保,我們可以什么都不做就解決這個(gè)問題
現(xiàn)在來創(chuàng)建一個(gè)Activity旋膳,點(diǎn)擊按鈕讓一個(gè)數(shù)字不斷加一,布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.287" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="add"
android:onClick="addCount"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.133" />
</androidx.constraintlayout.widget.ConstraintLayout>
創(chuàng)建一個(gè)類繼承ViewModel
package com.aruba.viewmodel;
import androidx.lifecycle.ViewModel;
/**
* Created by aruba on 2021/9/10.
*/
class NumberViewModel extends ViewModel {
public int number;
}
在Activity中使用ViewModelProvider通過反射獲取ViewModel途事,并實(shí)現(xiàn)點(diǎn)擊方法
package com.aruba.viewmodel;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private NumberViewModel numberViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(NumberViewModel.class);
textView = findViewById(R.id.textView);
textView.setText(String.valueOf(numberViewModel.number));
}
public void addCount(View view) {
numberViewModel.number++;
textView.setText(String.valueOf(numberViewModel.number));
}
}
效果:
如果要在ViewModel中使用Context验懊,不要手動(dòng)傳入,而是繼承至AndroidViewModel
package com.aruba.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.ViewModel;
/**
* Created by aruba on 2021/9/10.
*/
public class NumberViewModel extends AndroidViewModel {
public int number;
public NumberViewModel(@NonNull Application application) {
super(application);
}
}
2.除了瞬態(tài)數(shù)據(jù)自動(dòng)保存外尸变,ViewModel還具有異步調(diào)用不會造成內(nèi)存泄漏的優(yōu)點(diǎn)义图,需要結(jié)合LiveData使用,ViewModel的生命周期是獨(dú)立于Activity的
三召烂、LiveData
LiveData對象提供了可觀測方法歌溉,當(dāng)數(shù)據(jù)發(fā)送改變時(shí),觀測方能夠觀測到骑晶,并且線程安全痛垛,集成了LifeCycle的綁定生命周期特性
1.來實(shí)現(xiàn)一個(gè)定時(shí)器,線程中更新定時(shí)時(shí)間桶蛔,使用LiveData使得ui上進(jìn)行更新
布局文件很簡單匙头,一個(gè)TextView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/countTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
定義ViewModel,并使用LiveData
package com.aruba.livedata;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
/**
* Created by aruba on 2021/9/10.
*/
public class CountViewModel extends ViewModel {
private MutableLiveData<Integer> count;
public MutableLiveData<Integer> getCount() {
if (count == null) {
count = new MutableLiveData<>();
count.setValue(0);
}
return count;
}
}
在Activity中使用ViewModel仔雷,并觀測LiveData的值蹂析,ui線程中使用setValue方法設(shè)置LiveData的值,非ui線程使用postValue方法
package com.aruba.livedata;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private TextView countTextView;
private CountViewModel countViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(CountViewModel.class);
countTextView = findViewById(R.id.countTextView);
countViewModel.getCount().observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
countTextView.setText(String.valueOf(integer));
}
});
startTimer();
}
private void startTimer() {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
//setValue:ui線程中使用
//postValue:非ui線程中使用
countViewModel.getCount().postValue(countViewModel.getCount().getValue() + 1);
}
}, 0, 1000);
}
}
效果:
四碟婆、ViewModel+LiveData电抚,實(shí)現(xiàn)Fragment間通信
先看效果:
定義兩個(gè)fragment,布局是一樣的
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".FirstFragment">
<SeekBar
android:id="@+id/seekbar"
android:layout_width="0dp"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
創(chuàng)建ViewModel竖共,定義要聯(lián)動(dòng)的進(jìn)度值
package com.aruba.livedata2;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
/**
* Created by aruba on 2021/9/10.
*/
public class MyViewModel extends ViewModel {
private MutableLiveData<Integer> progress;
public MutableLiveData<Integer> getProgress() {
if (progress == null) {
progress = new MutableLiveData<>();
progress.setValue(0);
}
return progress;
}
}
實(shí)現(xiàn)兩個(gè)fragment中對ViewModel進(jìn)度值的觀察蝙叛,注意這邊獲取ViewModel時(shí)Owner要用Activity的上下文,因?yàn)閷?shí)現(xiàn)聯(lián)動(dòng)需要使用同一個(gè)ViewModel公给,不同Owner會生成不同的實(shí)例
package com.aruba.livedata2;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
public class FirstFragment extends Fragment {
private SeekBar seekbar;
private MyViewModel myViewModel;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_first, container, false);
seekbar = root.findViewById(R.id.seekbar);
myViewModel = new ViewModelProvider(getActivity(), new ViewModelProvider.AndroidViewModelFactory(getActivity().getApplication())).get(MyViewModel.class);
myViewModel.getProgress().observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
seekbar.setProgress(integer);
}
});
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
myViewModel.getProgress().setValue(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return root;
}
}
第二個(gè)fragment代碼是一樣的借帘。然后在Activity中加載兩個(gè)fragment
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<fragment
android:id="@+id/fragmentContainerView"
android:name="com.aruba.livedata2.FirstFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="@+id/fragmentContainerView2"
android:name="com.aruba.livedata2.SecondFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2" />
</androidx.constraintlayout.widget.ConstraintLayout>