2013-05-22 58 views
1

我的應用程序允許用戶創建一條文本消息並指定他們想要發送的時間和日期。我有一些問題,但。由於待定意圖的性質,我無法爲每封郵件創建一個新的郵件。因此,如果用戶在待執行意圖執行之前提交要發送的新文本,它將覆蓋先前的消息。因此,現在我不能有超過一條消息排隊等待發送。在Android應用程序中實現SQLite數據庫

我對此的解決方案是維護一個包含各種消息細節的SQLite數據庫,並簡單地使用數據庫中接下來發出的任何消息更新掛起的意圖。這還具有額外的好處,即實現顯示要發送的當前消息的列表以及編輯要發送的消息要簡單得多。

問題是我不認爲我正確設置數據庫。當我在調試模式下運行時,它似乎不會進入我的數據庫幫助程序類(MessagesHelper)並實例化數據庫變量。我不知道我在做什麼錯誤,並一直遵循Android SQLite開發指南。希望你能給我提供任何幫助/提示。

表合同類

public class Messages { 
    private Messages(){} 

    public static abstract class Texts implements BaseColumns { 
     public static final String TABLE_NAME = "texts"; 
     public static final String DEFAULT_SORT_ORDER = "stime DESC"; 
     public static final String COLUMN_NAME_RECIPIENT = "recipient"; 
     public static final String COLUMN_NAME_MESSAGE = "message"; 
     public static final String COLUMN_NAME_SEND_TIME = "stime"; 
     public static final String AUTHORITY = "com.rastelliJ.deferredSMS"; 
    } 
} 

DB助手類

public class MessagesHelper extends SQLiteOpenHelper{ 

    private static final String TAG = "MessagesHelper"; 
    private static final String TEXT_TYPE = " TEXT"; 
    private static final String COMMA_SEP = ","; 
    private static final String SQL_CREATE_ENTRIES = 
            "CREATE TABLE " + Messages.Texts.TABLE_NAME + " (" + 
            Messages.Texts._ID + " INTEGER PRIMARY KEY," + 
            Messages.Texts.COLUMN_NAME_MESSAGE + TEXT_TYPE + COMMA_SEP + 
            Messages.Texts.COLUMN_NAME_RECIPIENT + TEXT_TYPE + COMMA_SEP + 
            Messages.Texts.COLUMN_NAME_SEND_TIME + TEXT_TYPE + ")"; 
    private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + Messages.Texts.TABLE_NAME; 

    // If you change the database schema, you must increment the database version. 
    public static final int DATABASE_VERSION = 1; 
    public static final String DATABASE_NAME = "Messages.db"; 

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

    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(SQL_CREATE_ENTRIES); 
    } 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // This database is only a cache for online data, so its upgrade policy is 
     // to simply to discard the data and start over 
     Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
       + newVersion + ", which will destroy all old data"); 
     db.execSQL(SQL_DELETE_ENTRIES); 
     onCreate(db); 
    } 
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     onUpgrade(db, oldVersion, newVersion); 
    } 
    } 

主要活動

public class MainActivity extends FragmentActivity { 
private CustomDateTimePicker customDT; 
private MessagesHelper mDbHelper; 
private EditText phoneName, messageText; 
private String phoneNum, alarmtime; 
private TextView alarmText; 
private Button sendButt; 
private int pickerHour = 0, 
      pickerMin = 0, 
      pickerYear = 0, 
      pickerMonth = 0, 
      pickerDay = 0; 

private static final int CONTACT_PICKER_RESULT = 1; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Set up the custom Date Time Picker 
    customDT = new CustomDateTimePicker(this, new CustomDateTimePicker.ICustomDateTimeListener() { 
      public void onSet(Dialog dialog, Calendar calendarSelected, 
       Date dateSelected, int year, String monthFullName, 
       String monthShortName, int monthNumber, int date, 
       String weekDayFullName, String weekDayShortName, 
       int hour24, int hour12, int min, int sec, 
       String AM_PM) { 
        // Do something with the time chosen by the user 
        pickerYear = year; 
        pickerMonth = monthNumber; 
        pickerDay = date; 
        pickerHour = hour24; 
        pickerMin = min; 
        alarmtime = weekDayFullName + ", " + monthFullName + " " + date + ", " + year + " " + hour12 + ":" + pickerMin + " " + AM_PM; 
        alarmText.setText("Send Date: " + alarmtime); 
       } 

      public void onCancel() {} 
     }); 
    customDT.set24HourFormat(false); 
    customDT.setDate(Calendar.getInstance()); 
    findViewById(R.id.startTimeSetDialog).setOnClickListener(new OnClickListener() 
    { 
      public void onClick(View v) { 
       customDT.showDialog(); 
      } 
     }); 

    // Setup global variables 
    phoneName = (EditText)findViewById(R.id.phoneNo); 
    messageText = (EditText)findViewById(R.id.txtMessage); 
    sendButt = (Button)findViewById(R.id.btnSendSMS); 
    alarmText = (TextView)findViewById(R.id.alarmPrompt); 

    //Create/Find DB 
    mDbHelper = new MessagesHelper(this); 

    // Start Contact finder 
    phoneName.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
      startActivityForResult(intent, CONTACT_PICKER_RESULT); 
     } 
    }); 

    // "Send" the message 
    sendButt.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) { 

      //Make sure the fields are filled 
      if (phoneName.getText().toString().trim().length() == 0) 
      { 
       Toast.makeText(getApplicationContext(), "Please enter a phone number", Toast.LENGTH_LONG).show(); 
       return; 
      } 
      if (messageText.getText().toString().trim().length() == 0) 
      { 
       Toast.makeText(getApplicationContext(), "Please enter your message", Toast.LENGTH_LONG).show(); 
       return; 
      } 

      //Create a calendar variable that equates to the desired time to be sent 
      Calendar cal = Calendar.getInstance(); 
      cal.set(Calendar.YEAR, pickerYear); 
      cal.set(Calendar.MONTH, pickerMonth); 
      cal.set(Calendar.DATE, pickerDay); 
      cal.set(Calendar.HOUR_OF_DAY, pickerHour); 
      cal.set(Calendar.MINUTE, pickerMin); 
      cal.set(Calendar.SECOND, 0); 
      cal.set(Calendar.MILLISECOND, 0); 

      //Set up the pending intent and assign put it in the alarm manger 
      //will change this process once db is set up proper 
      Intent sIntent = new Intent(MainActivity.this, SendTService.class); 
      sIntent.putExtra("phoneNo", phoneNum.toString()); 
      sIntent.putExtra("msgTxt", messageText.getText().toString()); 
      PendingIntent psIntent = PendingIntent.getService(MainActivity.this,0, sIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
      AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
      alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), psIntent); 

      //Add the latest message to the db 
      SQLiteDatabase db = mDbHelper.getWritableDatabase(); 
      ContentValues values = new ContentValues(); 
      values.put(Messages.Texts.COLUMN_NAME_MESSAGE, messageText.getText().toString()); 
      values.put(Messages.Texts.COLUMN_NAME_RECIPIENT, phoneNum.toString()); 
      values.put(Messages.Texts.COLUMN_NAME_SEND_TIME, cal.toString()); 
      db.insert(Messages.Texts.TABLE_NAME, null, values); 

      //Clear all the fields and let the user know what's going on 
      phoneName.setText(""); 
      messageText.setText(""); 
      alarmText.setText(""); 
      Toast.makeText(getApplicationContext(), "Your Message will be sent on " + alarmtime, Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

//Associated with the Contact picker getting it's results 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     switch (requestCode) { 
      case CONTACT_PICKER_RESULT: 
       Cursor cursor = null; 
       String phoneNumber = ""; 
       List<String> allNumbers = new ArrayList<String>(); 
       int phoneIdx = 0; 
       try { 
        Uri result = data.getData(); 
        String id = result.getLastPathSegment(); 
        cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null); 
        phoneIdx = cursor.getColumnIndex(Phone.DATA); 
        if (cursor.moveToFirst()) 
        { 
         while (cursor.isAfterLast() == false) 
         { 
          phoneNumber = cursor.getString(phoneIdx); 
          allNumbers.add(phoneNumber); 
          cursor.moveToNext(); 
         } 
        } 
        else 
        { 
         //no results actions 
        } 
       } 
       catch (Exception e) 
       { 
        //error actions 
       } 
       finally 
       { 
        if (cursor != null) cursor.close(); 

        final CharSequence[] items = allNumbers.toArray(new String[allNumbers.size()]); 
        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 
        builder.setTitle("Choose a number"); 
        builder.setItems(items, new DialogInterface.OnClickListener() 
        { 
         public void onClick(DialogInterface dialog, int item) 
         { 
          phoneNum = items[item].toString(); 
          phoneNum = phoneNum.replace("-", ""); 
          phoneName.setText(phoneNum); 
         } 
        }); 
        AlertDialog alert = builder.create(); 
        if(allNumbers.size() > 1) 
        { 
         alert.show(); 
        } 
        else 
        { 
         phoneNum = phoneNumber.toString(); 
         phoneNum = phoneNum.replace("-", ""); 
         phoneName.setText(phoneNum); 
        } 

        if (phoneNumber.length() == 0) 
        { 
         //no numbers found actions 
        } 
       } 
       break; 
      } 
     } 
    else 
    { 
     //activity result error actions 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 
} 

回答

0

我在itial guess:您的SQL_CREATE_ENTRIES語句缺少CREATE TABLE語句末尾的分號。在谷歌的記事本例子和我的所有數據庫中,我總是包含分號,從來沒有問題。

相關問題