Android 重新启动设备后设置警报
为什么我的闹钟在设备重启后不响?我将我的报警时间插入SQLite数据库,并在设备重启后再次设置报警,但这似乎不起作用。我该怎么做才能让闹钟一次响起来? mySQLiteHelper.class:Android 重新启动设备后设置警报,android,sqlite,Android,Sqlite,为什么我的闹钟在设备重启后不响?我将我的报警时间插入SQLite数据库,并在设备重启后再次设置报警,但这似乎不起作用。我该怎么做才能让闹钟一次响起来? mySQLiteHelper.class: public class mySQLiteHelper extends SQLiteOpenHelper { // All Static variables // Database Version public static final int DATABASE_VERSION
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;
}
}
和报警等级:
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()的方式:
和MyReceiver 2.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);
}
}
}
还有我的雄激素清单
<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>
谢谢。因为在android中,如果您的设备重新启动,则清单文件中没有启动完成权限的所有服务/接收器将不会重新启动。由于接收器和服务在重新启动后已“死亡”,因此没有人监听您的“未决意图”以获取警报,没有人发送新的未决意图,而且在重新启动后,这些未决意图也会被清除。
简而言之,在清单中使用该权限,并在接收方中处理重启意图。您需要执行以下操作 添加以下权限
从接收器中删除构造函数
但是如果我的存储在sd卡上,我应该添加READ\u EXTERNAL\u STORAGE
权限吗
如果要从sd卡读取数据,则需要指定此权限。如果您只想在sd卡上写入数据,则不需要这样做。在这种情况下,您需要
WRITE\u EXTERNAL\u STORAGE
权限您的接收器是否工作正常?您在清单中是否具有接收\u启动\u完成
权限?共享您的清单文件您是否也添加了此权限@迈克:嗯,我想弄清楚它是否有效,看看我已经添加了整个舱单file@mustanser-iqbal我编辑了我的问题并添加了整个清单文件有人能解释一下我在这里写错了什么吗我投了反对票,那时我想通过正确的方向阅读来获得正确的知识。这可能是因为BOOT\u COMPLETED
是广播动作。权限为RECEIVE\u BOOT\u COMPLETED
。或者可能是因为你的答案在这一点上只是一个猜测。我们不知道OP是否已经获得了许可。有些用户不喜欢你把猜测作为答案。啊,谢谢。似乎我忘记了权限的正确名称。@rusheel jain但不必有任何未决意图来处理我的新警报,因为我创建了新的未决意图,并在设备重新启动后设置了新警报,对吗?@rusheel jain抱歉,但我从Mustanser Iqbal那里得到了答案
<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>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />