有人可以为我编写一个代码,从资源文件夹中打开一个sqlite文件(不是一个新文件),并验证主活动登录表单中的用户名和密码是否与存储的相同? 如果相同,则发送一个吐司说,否则,一个吐司说不 我在同一情况下遇到问题,代码可以编译但无法正常工作 我所拥有的是:
activity.java
package com.example.g.myapplication;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import static android.R.attr.version;
import static android.content.Context.MODE_PRIVATE;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText user = (EditText) findViewById(R.id.user);
final EditText pass = (EditText) findViewById(R.id.pass);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
final LinearLayout blocoLogin = (LinearLayout) findViewById(R.id.blocoLogin);
final LinearLayout blocoMapa = (LinearLayout) findViewById(R.id.blocoMapa);
final String[]paths = {"item 1", "item 2", "item 3"};
final DatabaseHelper login;
login = new DatabaseHelper(this);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*if (user.getText().toString().matches("admin") && pass.getText().toString().matches("123")) {
blocoLogin.setVisibility(View.GONE);
blocoMapa.setVisibility(View.VISIBLE);
};*/
Context context = new Context() {
@Override
public AssetManager getAssets() {
return null;
}
@Override
public Resources getResources() {
return null;
}
@Override
public PackageManager getPackageManager() {
return null;
}
@Override
public ContentResolver getContentResolver() {
return null;
}
@Override
public Looper getMainLooper() {
return null;
}
@Override
public Context getApplicationContext() {
return null;
}
@Override
public void setTheme(int resid) {
}
@Override
public Resources.Theme getTheme() {
return null;
}
@Override
public ClassLoader getClassLoader() {
return null;
}
@Override
public String getPackageName() {
return null;
}
@Override
public ApplicationInfo getApplicationInfo() {
return null;
}
@Override
public String getPackageResourcePath() {
return null;
}
@Override
public String getPackageCodePath() {
return null;
}
@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
return null;
}
@Override
public boolean moveSharedPreferencesFrom(Context sourceContext, String name) {
return false;
}
@Override
public boolean deleteSharedPreferences(String name) {
return false;
}
@Override
public FileInputStream openFileInput(String name) throws FileNotFoundException {
return null;
}
@Override
public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
return null;
}
@Override
public boolean deleteFile(String name) {
return false;
}
@Override
public File getFileStreamPath(String name) {
return null;
}
@Override
public File getDataDir() {
return null;
}
@Override
public File getFilesDir() {
return null;
}
@Override
public File getNoBackupFilesDir() {
return null;
}
@Nullable
@Override
public File getExternalFilesDir(String type) {
return null;
}
@Override
public File[] getExternalFilesDirs(String type) {
return new File[0];
}
@Override
public File getObbDir() {
return null;
}
@Override
public File[] getObbDirs() {
return new File[0];
}
@Override
public File getCacheDir() {
return null;
}
@Override
public File getCodeCacheDir() {
return null;
}
@Nullable
@Override
public File getExternalCacheDir() {
return null;
}
@Override
public File[] getExternalCacheDirs() {
return new File[0];
}
@Override
public File[] getExternalMediaDirs() {
return new File[0];
}
@Override
public String[] fileList() {
return new String[0];
}
@Override
public File getDir(String name, int mode) {
return null;
}
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory) {
return null;
}
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) {
return null;
}
@Override
public boolean moveDatabaseFrom(Context sourceContext, String name) {
return false;
}
@Override
public boolean deleteDatabase(String name) {
return false;
}
@Override
public File getDatabasePath(String name) {
return null;
}
@Override
public String[] databaseList() {
return new String[0];
}
@Override
public Drawable getWallpaper() {
return null;
}
@Override
public Drawable peekWallpaper() {
return null;
}
@Override
public int getWallpaperDesiredMinimumWidth() {
return 0;
}
@Override
public int getWallpaperDesiredMinimumHeight() {
return 0;
}
@Override
public void setWallpaper(Bitmap bitmap) throws IOException {
}
@Override
public void setWallpaper(InputStream data) throws IOException {
}
@Override
public void clearWallpaper() throws IOException {
}
@Override
public void startActivity(Intent intent) {
}
@Override
public void startActivity(Intent intent, Bundle options) {
}
@Override
public void startActivities(Intent[] intents) {
}
@Override
public void startActivities(Intent[] intents, Bundle options) {
}
@Override
public void startIntentSender(IntentSender intent, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) throws IntentSender.SendIntentException {
}
@Override
public void startIntentSender(IntentSender intent, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) throws IntentSender.SendIntentException {
}
@Override
public void sendBroadcast(Intent intent) {
}
@Override
public void sendBroadcast(Intent intent, String receiverPermission) {
}
@Override
public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
}
@Override
public void sendOrderedBroadcast(Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
}
@Override
public void sendBroadcastAsUser(Intent intent, UserHandle user) {
}
@Override
public void sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission) {
}
@Override
public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
}
@Override
public void sendStickyBroadcast(Intent intent) {
}
@Override
public void sendStickyOrderedBroadcast(Intent intent, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
}
@Override
public void removeStickyBroadcast(Intent intent) {
}
@Override
public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
}
@Override
public void sendStickyOrderedBroadcastAsUser(Intent intent, UserHandle user, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
}
@Override
public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
}
@Nullable
@Override
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
return null;
}
@Nullable
@Override
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler) {
return null;
}
@Override
public void unregisterReceiver(BroadcastReceiver receiver) {
}
@Nullable
@Override
public ComponentName startService(Intent service) {
return null;
}
@Override
public boolean stopService(Intent service) {
return false;
}
@Override
public boolean bindService(Intent service, ServiceConnection conn, int flags) {
return false;
}
@Override
public void unbindService(ServiceConnection conn) {
}
@Override
public boolean startInstrumentation(ComponentName className, String profileFile, Bundle arguments) {
return false;
}
@Override
public Object getSystemService(String name) {
return null;
}
@Override
public String getSystemServiceName(Class<?> serviceClass) {
return null;
}
@Override
public int checkPermission(String permission, int pid, int uid) {
return 0;
}
@Override
public int checkCallingPermission(String permission) {
return 0;
}
@Override
public int checkCallingOrSelfPermission(String permission) {
return 0;
}
@Override
public int checkSelfPermission(String permission) {
return 0;
}
@Override
public void enforcePermission(String permission, int pid, int uid, String message) {
}
@Override
public void enforceCallingPermission(String permission, String message) {
}
@Override
public void enforceCallingOrSelfPermission(String permission, String message) {
}
@Override
public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
}
@Override
public void revokeUriPermission(Uri uri, int modeFlags) {
}
@Override
public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
return 0;
}
@Override
public int checkCallingUriPermission(Uri uri, int modeFlags) {
return 0;
}
@Override
public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
return 0;
}
@Override
public int checkUriPermission(Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags) {
return 0;
}
@Override
public void enforceUriPermission(Uri uri, int pid, int uid, int modeFlags, String message) {
}
@Override
public void enforceCallingUriPermission(Uri uri, int modeFlags, String message) {
}
@Override
public void enforceCallingOrSelfUriPermission(Uri uri, int modeFlags, String message) {
}
@Override
public void enforceUriPermission(Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags, String message) {
}
@Override
public Context createPackageContext(String packageName, int flags) throws PackageManager.NameNotFoundException {
return null;
}
@Override
public Context createConfigurationContext(Configuration overrideConfiguration) {
return null;
}
@Override
public Context createDisplayContext(Display display) {
return null;
}
@Override
public Context createDeviceProtectedStorageContext() {
return null;
}
@Override
public boolean isDeviceProtectedStorage() {
return false;
}
};
if(login.loginCheck(user.getText().toString(),pass.getText().toString())) {
blocoLogin.setVisibility(View.GONE);
blocoMapa.setVisibility(View.VISIBLE);
}
}}
); {
};
final Spinner spinner = (Spinner)findViewById(R.id.listaLocais);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0:
// Whatever you want to happen when the first item gets selected
break;
case 1:
// Whatever you want to happen when the second item gets selected
break;
case 2:
// Whatever you want to happen when the thrid item gets selected
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
databasehelper.java
package com.example.g.myapplication;
import android.content.ContentResolver;
import android.content.Context;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Bundle;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by G on 07/08/2018.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "teste.sqlite";
public static final String DBLOCATION = "/data/data/com.example.g.myapplication/databases/";
private Context mContext;
private SQLiteDatabase mDatabase;
public DatabaseHelper(Context context) {
super(context, DBNAME, null, 1);
this.mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void openDatabase() {
String dbPath = mContext.getDatabasePath(DBNAME).getPath();
if (mDatabase != null && mDatabase.isOpen()) {
return;
}
mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public void closeDatabase() {
if (mDatabase != null) {
mDatabase.close();
}
}
public Boolean loginCheck(String a, String b) {
openDatabase();
Cursor cursor = mDatabase.rawQuery("SELECT * FROM usuarios WHERE user = '" + a + "' AND pass = '" + b + "'", null);
cursor.moveToFirst();
//Select * from tabela_de_usuários where user = 'valor' and pass = 'hash_md5_ou_SHA1_da_senha_digitada_nunca_use_ou_armazene_a_senha_real'
boolean e = false;
if (cursor.getCount() > 0) {
e = true;
}
cursor.close();
closeDatabase();
return e;
}
}