我需要知道如何使用从主要活动启动的服务来更新LiveData对象的数据
我正在使用android architecture components,所以我有对象类,dao接口,存储库,房间数据库和视图模型。
一切正常,并且在RoomDatabase中创建数据库时成功加载了数据,但是,我需要更新服务中生成的数据,并存在下一行需要活动上下文的问题:
LiveData<List<WifiClass>> wifiViewModel = ViewModelProviders.of(this).get(WifiViewModel.class);
Dao界面:
@Dao
public interface WifiDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(WifiClass wifiObject);
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insertAll(List<WifiClass> wifiList);
@Query("DELETE FROM wifi_table")
void deleteAll();
@Query("SELECT * from wifi_table ORDER BY SSID ASC")
LiveData<List<WifiClass>> getAllWifi();
@Query("SELECT * from wifi_table LIMIT 1")
WifiClass[] getAnyWifi();
}
存储库:
public class WifiRepository {
private WifiDao wifiDao;
private LiveData<List<WifiClass>> allWifi;
WifiRepository(Application application) {
WifiRoomDatabase db = WifiRoomDatabase.getDatabase(application);
wifiDao = db.wifiDao();
allWifi = wifiDao.getAllWifi();
}
// Get all Wifi objects
LiveData<List<WifiClass>> getAllWifi() {
return allWifi;
}
//Insert one Wifi object
public void insert (WifiClass wifiObject){
new insertAsyncTask(wifiDao).execute(wifiObject);
}
private static class insertAsyncTask extends AsyncTask<WifiClass, Void, Void> {
private WifiDao mAsyncTaskDao;
insertAsyncTask(WifiDao wifiDao) {
mAsyncTaskDao = wifiDao;
}
@Override
protected Void doInBackground(final WifiClass... params) {
mAsyncTaskDao.insert(params[0]);
return null;
}
}
}
RoomDatabase:
@Database(entities = {WifiClass.class}, version = 1, exportSchema = false)
public abstract class WifiRoomDatabase extends RoomDatabase implements Serializable{
public abstract WifiDao wifiDao();
private static WifiRoomDatabase INSTANCE;
public static Context context;
static List<ScanResult> results;
private static WifiManager wifiManager;
public static WifiRoomDatabase getDatabase(final Context contextt) {
context = contextt;
if (INSTANCE == null) {
synchronized (WifiRoomDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), WifiRoomDatabase.class, "wifi_database")
.fallbackToDestructiveMigration() // Wipes and rebuilds
.addCallback(sRoomDatabaseCallback) //
.build();
}
}
}
return INSTANCE;
}
private static RoomDatabase.Callback sRoomDatabaseCallback = new RoomDatabase.Callback(){
@Override
public void onOpen (@NonNull SupportSQLiteDatabase db){
super.onOpen(db);
new PopulateDbAsync(INSTANCE).execute();
}
};
//Populate the database in the background.
private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {
private final WifiDao mDao;
PopulateDbAsync(WifiRoomDatabase db) {
mDao = db.wifiDao();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()){
//Toast.makeText(this, "Wifi disable... Activating...", Toast.LENGTH_SHORT).show();
wifiManager.setWifiEnabled(true);
}
results = wifiManager.getScanResults();
wifiManager.startScan();
//Toast.makeText(this, "Scanning...", Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(Void... voids) {
for (ScanResult scanResult : results) {
String ssid = scanResult.SSID;
if (ssid == null || ssid.isEmpty()){
ssid = "Hide SSID";
}
String bssid = scanResult.BSSID;
String capabilities = scanResult.capabilities;
int level = scanResult.level;
int frequency = scanResult.frequency;
long timestamp = scanResult.timestamp;
int passpoint; int channelWidth; int centerFreq0; int centerFreq1; int responder;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (scanResult.isPasspointNetwork() == true) {
passpoint = 1;
}else{
passpoint = 0;
}
channelWidth = scanResult.channelWidth;
centerFreq0 = scanResult.centerFreq0;
centerFreq1 = scanResult.centerFreq1;
if (scanResult.is80211mcResponder() == true){
responder = 1;
}else{
responder = 0;
}
}else{
passpoint = 0;
channelWidth = 0;
centerFreq0 = 0;
centerFreq1 = 0;
responder = 0;
}
WifiClass wifiObject = new WifiClass(ssid, bssid, capabilities, level, frequency,
timestamp, passpoint, channelWidth, centerFreq0, centerFreq1, responder);
mDao.insert(wifiObject);
}
return null;
}
}
ViewModel:
public class WifiViewModel extends AndroidViewModel {
private WifiRepository wifiRepository;
private LiveData<List<WifiClass>> allWifi;
public WifiViewModel(@NonNull Application application) {
super(application);
wifiRepository = new WifiRepository(application);
allWifi = wifiRepository.getAllWifi();
}
LiveData<List<WifiClass>> getAllWifi() {
return allWifi;
}
public void insert(WifiClass wifiObject) {
wifiRepository.insert(wifiObject);
}
}
服务:
public class Wifi_Service extends IntentService {
private Handler handler;
private Runnable runnable;
private static Context classContext;
//private static WifiViewModel wifiViewModel;
static List<ScanResult> results;
private static WifiManager wifiManager;
private LiveData<List<WifiClass>> mAllWifi;
public Wifi_Service(/*WifiRoomDatabase db*/) {
super("wifi_service");
}
@Override
protected void onHandleIntent(Intent intent) {
}
//Populate the database in the background.
private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {
private final WifiDao mDao;
PopulateDbAsync(WifiRoomDatabase db) {
mDao = db.wifiDao();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
wifiManager = (WifiManager) classContext.getApplicationContext().getSystemService(WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()){
Toast.makeText(classContext, "Wifi disable... Activating...", Toast.LENGTH_SHORT).show();
wifiManager.setWifiEnabled(true);
}
results = wifiManager.getScanResults();
wifiManager.startScan();
Toast.makeText(classContext, "Scanning...", Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(Void... voids) {
for (ScanResult scanResult : results) {
String ssid = scanResult.SSID;
if (ssid == null || ssid.isEmpty()){
ssid = "Hide SSID";
}
String bssid = scanResult.BSSID;
String capabilities = scanResult.capabilities;
int level = scanResult.level;
int frequency = scanResult.frequency;
long timestamp = scanResult.timestamp;
int passpoint; int channelWidth; int centerFreq0; int centerFreq1; int responder;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (scanResult.isPasspointNetwork() == true) {
passpoint = 1;
}else{
passpoint = 0;
}
channelWidth = scanResult.channelWidth;
centerFreq0 = scanResult.centerFreq0;
centerFreq1 = scanResult.centerFreq1;
if (scanResult.is80211mcResponder() == true){
responder = 1;
}else{
responder = 0;
}
}else{
passpoint = 0;
channelWidth = 0;
centerFreq0 = 0;
centerFreq1 = 0;
responder = 0;
}
WifiClass wifiObject = new WifiClass(ssid, bssid, capabilities, level, frequency,
timestamp, passpoint, channelWidth, centerFreq0, centerFreq1, responder);
//The problem
wifiViewModel.insert(wifiObject);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(classContext, "Scanning finished", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Scanning...", Toast.LENGTH_SHORT).show();
Bundle extras = intent.getExtras();
if(extras == null) {
Log.d("Service", "null");
}else {
Log.d("Service","not null");
classContext = getApplicationContext();
}
//The problem
// wifiViewModel = ViewModelProviders.of(this).get(WifiViewModel.class);
// mWifiViewModel.getAllWifi().observe(this, new Observer<List<WifiClass>>() {
// @Override
// public void onChanged(@Nullable List<WifiClass> wifiClasses) {
//
// }
// });
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Toast.makeText(this, "Destroy scanning service", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
}
我希望服务在LiveData列表中执行,保存和刷新新结果,但我无法声明LiveData列表。
答案 0 :(得分:0)
由于无法调用和使用LiveData列表,因为我需要传递应用程序上下文,而Android也不接受,因此CommonsWare和The_Martian建议我改用存储库。
由于我的存储库调用了room数据库,所以我调用了在room类中生成的数据库,然后从那里调用了dao接口和方法insert。
代码:
WifiRoomDatabase db = WifiRoomDatabase.getDatabase(getApplicationContext());
db.wifiDao().insert(wifiObject);
我实际上是在学习Android体系结构组件,所以我不知道这是否是最好的解决方案,但是它对我有用。