为什么我的闹钟在设备重启后没有响铃?我将警报的时间插入SQLite数据库并在设备重启后再次设置警报,但这似乎不起作用。我可以做些什么来让我的闹钟响铃? 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;
}
}
和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;
}
}
这就是我调用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"));
和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);
}
}
}
还有我的AndroidManifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amadey.myapplication" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher1"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Introduction"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme" >
</activity>
<activity
android:name=".MainActivity2"
android:theme="@style/AppTheme" >
</activity>
<receiver
android:name=".StartReceiver"
android:enabled="true"
android:exported="true" >
</receiver>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" >
</service>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" >
</receiver>
<service
android:name=".MyService2"
android:enabled="true"
android:exported="true" >
</service>
<receiver
android:name=".MyReceiver2"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
谢谢。
答案 0 :(得分:0)
因为在android中,如果您的设备被重新启动,清单文件中没有BOOT_COMPLETED权限的所有服务/接收器将不会重新启动。由于接收器和服务现在在重新启动后“死亡”,因此没有人可以监听您的“待处理意图”以进行警报,没有人发送新的待处理意图,而且在重新启动时,这些待处理意图也会被清除。 简而言之,在清单中使用该权限并处理接收器中的restart-intent。
答案 1 :(得分:0)
您需要做以下事情。
添加以下权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
从接收器中删除构造函数。
但如果我将存储在SD卡上,我应该添加
READ_EXTERNAL_STORAGE
权限吗?
如果您想从SD卡读取数据,则需要指定此权限。如果你只想在SD卡上写数据那么就没有必要了。在这种情况下,您需要WRITE_EXTERNAL_STORAGE
权限