我有一个活动来检查本地数据库(sqlite)中是否存在设备ID和设备名称,当设备已经注册时,我将传递给另一个名为ConnectToCostCenter 的活动活动的代码是:
public class StartActivity extends Activity {
Handler handler = new Handler();
DBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_activity);
dbHandler = new DBHandler(this);
handler.postDelayed(new Runnable() {
@Override
public void run() {
String DeviceId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
String DeviceName = Build.MODEL;
RegisterDevice cc = new RegisterDevice(DeviceId, DeviceName);
if (!dbHandler.getAllDevice().isEmpty() && (dbHandler.getAllDevice().contains(cc)) ){
final Intent mainIntent = new Intent(StartActivity.this, ConnectToCostCenter.class);
startActivity(mainIntent);
} else {
Intent intent = new Intent(StartActivity.this, CreateOrEditActivity.class);
startActivity(intent);
}
StartActivity.this.finish();
}
}, 3000);
}
}
和 ArrayList :
public ArrayList<RegisterDevice> getAllDevice() {
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<RegisterDevice> activityList = null;
try{
activityList = new ArrayList<RegisterDevice>();
String QUERY = "SELECT * FROM "+DEVICE_TABLE_NAME;
Cursor cursor = db.rawQuery(QUERY, null);
if(!cursor.isLast())
{
while (cursor.moveToNext())
{
RegisterDevice activity = new RegisterDevice();
activity.setDeviceId(cursor.getString(0));
// activity.setCreated(cursor.getString(2));
// activity.setModified(cursor.getString(3));
activity.setDeviceName(cursor.getString(1));
activityList.add(activity);
}
}
db.close();
}catch (Exception e){
Log.e("error",e+"");
}
return activityList;
}
问题是如果设备存在于数据库中,那么会出现 CreateOrEditActivity 活动而不是 ConnectToCostCenter 有什么帮助吗?