why my alarms don't ring after device was restarted? I insert my alarms' times into SQLite database and set alarms again after device was restarted but this doesn't seem to be worked. What can I do to make my alarms ring at a time? mySQLiteHelper.class:
public class mySQLiteHelper extends SQLiteOpenHelper {
// All Static variables
// Database Version
public static final int DATABASE_VERSION = 2;
// Database Name
private static final String DATABASE_NAME = "alarms.db";
// Contacts table name
private static final String TABLE_ALARMS = "alarms";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_ALARM = "alarm";
private static final String KEY_NAME = "name";
public mySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables"create table alarms ("
//+ "id integer primary key,"
// + "alarm text,"
// + "name text," // added a ','
// + "alarmname text" + ");"
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_ALARMS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_ALARM + " TEXT, "
+ KEY_NAME + " TEXT" + ")";
//String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_ALARMS + "("
// + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_ALARM + " TEXT, "
// + KEY_NAME + " TEXT, UNIQUE ("+KEY_NAME+") ON CONFLICT REPLACE" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALARMS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations"," + alarm.getName() +,name
*/
// Adding new contact
void addAlarm(Alarm alarm) {
SQLiteDatabase db = this.getWritableDatabase();
String sql =
"INSERT or replace " +
"INTO alarms (alarm,"+KEY_NAME+") " +
"VALUES("+ alarm.getAlarm() + ",'" + alarm.getName() + "')";
//String sql =
// "INSERT or replace INTO alarms (alarm,alarmname) VALUES("+ alarm.getAlarm() + ",'" + alarm.getName() + "')" ;
db.execSQL(sql);
db.close(); // Closing database connection
}
// Getting single contact
String getAlarm(String alarmname) {
String query = "Select * FROM " + TABLE_ALARMS + " WHERE " + KEY_NAME + " = \"" + alarmname + "\"";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.getCount()>0)
cursor.moveToFirst();
String alarm = cursor.getString(cursor.getColumnIndex(KEY_ALARM));
cursor.close();
// return contact
return alarm;
}
// Deleting single contact
public void deleteAlarm() {
SQLiteDatabase db = this.getWritableDatabase();
//db.delete(TABLE_ALARMS, KEY_ID + " = ?",Alarm alarm
// new String[] { String.valueOf(alarm.getID()) });
db.execSQL("delete from " + TABLE_ALARMS);
db.close();
}
// Getting contacts Count
public int getAlarmsCount() {
String countQuery = "SELECT * FROM " + TABLE_ALARMS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int i = cursor.getCount();
cursor.close();
// return count
return i;
}
}
And Alarm.class:
public class Alarm {
//private variables
int _id;
String _name;
String _alarm;
// Empty constructor
public Alarm(String alarm,String alarmname){
this._alarm = alarm;
this._name = alarmname;
}
// constructor
public Alarm(int id, String name, String alarm){
this._id = id;
this._name = name;
this._alarm = alarm;
}
// constructor
public Alarm(String alarm){
this._alarm = alarm;
}
// getting ID
public int getID(){
return this._id;
}
// getting name
public String getName(){
return this._name;
}
// getting phone number
public String getAlarm(){
return this._alarm;
}
}
This is how I call addAlarm():
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.MINUTE, timePicker1.getCurrentMinute());
calendar1.set(Calendar.HOUR_OF_DAY,timePicker1.getCurrentHour());
String alarm1 = Long.toString(calendar1.getTimeInMillis());
mySQLiteHelper mySQLiteHelper = new mySQLiteHelper(MainActivity2.this);
mySQLiteHelper.deleteAlarm();
mySQLiteHelper.addAlarm(new Alarm(alarm1,"alarm1"));
And MyReceiver2.class:
public class MyReceiver2 extends BroadcastReceiver {
public MyReceiver2() {
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
//context.startService(new Intent(context, MyService.class));
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(context,StartReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context,0,intent1,0);
mySQLiteHelper mySQLiteHelper = new mySQLiteHelper(context);
String alarm1 = mySQLiteHelper.getAlarm("alarm1");
long alarm2 = Long.parseLong(alarm1);
am.set(AlarmManager.RTC,alarm3,pi);
}
}
}
And also MyReceiver2.class in AndroidManifest
<receiver
android:name=".MyReceiver2"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Thank you.
Aucun commentaire:
Enregistrer un commentaire