2016-03-23 18 views
0

目覚まし時計アプリを作りたいと思います。そして、私は1つの問題に直面しました:私はデバイスを再起動した後、私は何の警報も得られません。それから、私はこの問題を解決する方法を尋ねました。答えは、データベースを作成し、すべてのアラームをそこに保存し、アラーム時刻を現在の時刻と比較して再起動した後、現在の時刻がアラーム時刻よりも小さい場合です。警報。さて、私はこれをして、エラーがあった。私がそれを解決するのを助けてください。SQLiteDatabaseからのデータの取り込みと取得

java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 
     at com.example.amadey.myapplication.MainActivity2$1.onClick(MainActivity2.java:52) 
     at android.view.View.performClick(View.java:4438) 
     at android.view.View$PerformClick.run(View.java:18422) 
     at android.os.Handler.handleCallback(Handler.java:733) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5001) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
     at dalvik.system.NativeStart.main(Native Method) 

アラームに置く:

AlarmsDataSource datasource = new AlarmsDataSource(this); 
     datasource.open(); 
Alarms alarm = null; 
       Long[] alarms = new Long[] { rt,calendar1.getTimeInMillis() }; 
       alarm = datasource.createComment(alarms[1]); 
       alarm = datasource.createComment(alarms[2]); 

Alarms.class:

public class Alarms { 
    private long id; 
    private String alarms; 

    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     this.id = id; 
    } 

    public String getAlarms() { 
     return alarms; 
    } 

    public void setAlarms(String alarms) { 
     this.alarms = alarms; 
    } 
} 

AlarmsDataSource.class

public class AlarmsDataSource { 
    private SQLiteDatabase database; 
    private MySQLiteHelper dbHelper; 
    private String[] allColumns = { MySQLiteHelper.COLUMN_ID, 
      MySQLiteHelper.COLUMN_ALARM }; 

    public AlarmsDataSource(Context context) { 
     dbHelper = new MySQLiteHelper(context); 
    } 

    public void open() throws SQLException { 
     database = dbHelper.getWritableDatabase(); 
    } 

    public void close() { 
     dbHelper.close(); 
    } 

    public Alarms createComment(Long alarm) { 
     ContentValues values = new ContentValues(); 
     values.put(MySQLiteHelper.COLUMN_ALARM, alarm); 
     long insertId = database.insert(MySQLiteHelper.TABLE_ALARMS, null, 
       values); 
     Cursor cursor = database.query(MySQLiteHelper.TABLE_ALARMS, 
       allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null, 
       null, null, null); 
     cursor.moveToFirst(); 
     Alarms newAlarms = cursorToAlarm(cursor); 
     cursor.close(); 
     return newAlarms; 
    } 

    /*public List<Comment> getAllComments() { 
     List<Comment> comments = new ArrayList<Comment>(); 

     Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, 
       allColumns, null, null, null, null, null); 

     cursor.moveToFirst(); 
     while (!cursor.isAfterLast()) { 
      Comment comment = cursorToComment(cursor); 
      comments.add(comment); 
      cursor.moveToNext(); 
     } 
     // make sure to close the cursor 
     cursor.close(); 
     return comments; 
    }*/ 

    private Alarms cursorToAlarm(Cursor cursor) { 
     Alarms alarms = new Alarms(); 
     alarms.setId(cursor.getLong(0)); 
     return alarms; 
    } 
} 

MySQLiteHelper.class:

public class MySQLiteHelper extends SQLiteOpenHelper { 
    public static final String TABLE_ALARMS = "alarms"; 
    public static final String COLUMN_ID = "_id"; 
    public static final String COLUMN_ALARM = "alarm"; 
    private static final String DB_PATH = "/DATA/data/db1/databases/"; 

    private static final String DATABASE_NAME = "alarms.db"; 
    private static final int DATABASE_VERSION = 1; 

    // Database creation sql statement 
    private static final String DATABASE_CREATE = "create table " 
      + TABLE_ALARMS + "(" + COLUMN_ID 
      + " integer primary key autoincrement, " + COLUMN_ALARM 
      + " text not null);"; 

    public MySQLiteHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase database) { 
     database.execSQL(DATABASE_CREATE); 
    } 
@Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 
    public void openDataBase() throws SQLException { 
     //Open the database 
     String myPath = DB_PATH + DATABASE_NAME; 
     SQLiteDatabase myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 
    } 
} 

これは、私はアラームを取得する方法である:

public class MyReceiver extends BroadcastReceiver { 
    MySQLiteHelper mySQLiteHelper; 
    public String COLUMN_ID = MySQLiteHelper.COLUMN_ID; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){ 
      //context.startService(new Intent(context, MyService.class)); 
      mySQLiteHelper = new MySQLiteHelper(context); 
      mySQLiteHelper.openDataBase(); 

      getData(context); //this is the method to query 
      mySQLiteHelper.close(); 
     } 
    } 
    public void getData(Context context) { 

     final String TABLE_NAME = MySQLiteHelper.TABLE_ALARMS; 

     String selectQuery = "SELECT * FROM " + TABLE_NAME; 
     SQLiteDatabase db = mySQLiteHelper.getReadableDatabase(); 
     Cursor cursor  = db.rawQuery(selectQuery, new String[]{COLUMN_ID}); 
     Long[] data  = null; 
     Intent intent = new Intent(context,MyReceiver1.class); 
     Intent intent1 = new Intent(context,MyReceiver2.class); 
     PendingIntent pi = PendingIntent.getBroadcast(context,0,intent,0); 
     PendingIntent pi1 = PendingIntent.getBroadcast(context,0,intent1,0); 
     if (cursor.moveToFirst()) { 
      do { 
       int keyRowIdColumnIndex = cursor.getColumnIndex(COLUMN_ID); 
       data[keyRowIdColumnIndex] = cursor.getLong(keyRowIdColumnIndex); 

       }while (cursor.moveToNext()); 
      } 
     AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     if (data[1]>System.currentTimeMillis()){ 

       am.set(AlarmManager.RTC,data[1],pi); 
      am.set(AlarmManager.RTC,data[2],pi1); 
     }else if(data[2]>System.currentTimeMillis() && data[1]<System.currentTimeMillis()){ 
      am.set(AlarmManager.RTC,data[2],pi1); 
      context.startService(new Intent(context, MyService2.class)); 
     } 
     cursor.close(); 
    } 

} 

は!!!!!!!!! pleaaaase、私を助けて

答えて

関連する問題