Android Manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.caelum.cadastro">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application android:allowBackup="true" android:label="@string/app_name"
android:icon="@drawable/ic_launcher" android:supportsRtl="true">
<activity android:name="br.com.caelum.cadastro.ListaAlunosActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="br.com.caelum.cadastro.FormularioActivity"
android:label="@string/app_name" />
<activity android:name="br.com.caelum.cadastro.WebViewActivity"
android:label="@string/site_aluno" />
<receiver android:name="br.com.caelum.cadastro.SMSReceiver" android:enabled="true">
<intent-filter android:priority="1">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
AlunoDAO.java
package br.com.caelum.cadastro.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
import br.com.caelum.cadastro.model.Aluno;
/**
* Created by guilherme.costa on 16/12/2015.
*/
public class AlunoDAO {
private SQLiteOpenHelper helper ;
private static final String DATABASE = "caelumDB";
private static final int VERSION = 1;
private static final String TABELA = "alunos";
public AlunoDAO(Context context) {
helper = new SQLiteOpenHelper(context, DATABASE, null, VERSION) {
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABELA +
"(id INTEGER PRIMARY KEY," +
" nome TEXT UNIQUE NOT NULL," +
" telefone TEXT," +
" endereco TEXT," +
" site TEXT," +
" nota REAL," +
" foto TEXT" +
");";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS " + TABELA;
db.execSQL(sql);
onCreate(db);
}
};
}
public void inserir(Aluno aluno) {
helper.getWritableDatabase().insert(TABELA, null, toContentValues(aluno));
helper.close();
}
private ContentValues toContentValues(Aluno aluno) {
ContentValues cv = new ContentValues();
cv.put("nome", aluno.getNome());
cv.put("endereco", aluno.getEndereco());
cv.put("telefone", aluno.getTelefone());
cv.put("site", aluno.getSite());
cv.put("nota", aluno.getNota());
cv.put("foto", aluno.getCaminhoFoto());
return cv;
}
public List<Aluno> getLista() {
List<Aluno> alunos = new ArrayList<Aluno>();
String sql = "SELECT * FROM Alunos";
Cursor cursor = helper.getReadableDatabase().rawQuery(sql, null);
while(cursor.moveToNext()){
Aluno aluno = new Aluno();
aluno.setId(cursor.getLong(cursor.getColumnIndex("id")));
aluno.setNome(cursor.getString(cursor.getColumnIndex("nome")));
aluno.setEndereco(cursor.getString(cursor.getColumnIndex("endereco")));
aluno.setTelefone(cursor.getString(cursor.getColumnIndex("telefone")));
aluno.setSite(cursor.getString(cursor.getColumnIndex("site")));
aluno.setCaminhoFoto(cursor.getString(cursor.getColumnIndex("foto")));
aluno.setNota(cursor.getDouble(cursor.getColumnIndex("nota")));
alunos.add(aluno);
}
cursor.close();
return alunos;
}
public void deletar(Aluno aluno) {
String[] args = {aluno.getId().toString()};
helper.getWritableDatabase().delete(TABELA, "id=?", args);
helper.close();
}
public void atualizar(Aluno aluno) {
String[] args = {aluno.getId().toString()};
helper.getWritableDatabase().update(TABELA, toContentValues(aluno), "id=?", args);
}
public boolean isAluno(String telefone) {
Cursor rawQuery = helper.getReadableDatabase().rawQuery("SELECT telefone from " + TABELA
+ " WHERE telefone = ?", new String[]{telefone});
int total = rawQuery.getCount();
rawQuery.close();
return total > 0;
}
}