2

1.你好,我需要帮助我正在构建一个应用程序,将日期、时间、电话号码和消息等数据存储在数据库中,并根据时间和日期,通过从数据库中检索特定保存日期的数据自动发送短信,并且时间,我做了下面的代码

 public class MainActivity extends Activity implements OnClickListener {
    // Widget GUI
        Button btnCalendar, btnTimePicker,btnset;
        EditText txtDate, txtTime,txtpno,txtmsg;
        //String pno,msg,date,time;
        // Variable for storing current date and time
        private int mYear, mMonth, mDay, mHour, mMinute;
        private PendingIntent pendingIntent;
        SQLiteDatabase mdatabase=null;
        String pno,msg,date,time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mdatabase=openOrCreateDatabase("timedb", SQLiteDatabase.CREATE_IF_NECESSARY, null);
        String Time_TABLE="create table if not exists SmsTime (pno TEXT PRIMARY KEY,msg TEXT,date TEXT,time TEXT)";

        mdatabase.execSQL(Time_TABLE);
        mdatabase.close();
        btnCalendar = (Button) findViewById(R.id.btnCalendar);
        btnTimePicker = (Button) findViewById(R.id.btnTimePicker);
        btnset=(Button) findViewById(R.id.btnset);

        txtDate = (EditText) findViewById(R.id.txtDate);
        txtTime = (EditText) findViewById(R.id.txtTime);
        txtpno=(EditText) findViewById(R.id.txtpno);
        txtmsg=(EditText) findViewById(R.id.txtmsg);

        btnCalendar.setOnClickListener(this);
        btnTimePicker.setOnClickListener(this);
        btnset.setOnClickListener(this);
        final Handler h=new Handler();

    mdatabase=openOrCreateDatabase("timedb", SQLiteDatabase.OPEN_READONLY, null);
        final Cursor c=mdatabase.query("SmsTime", null,null, null, null, null, null);

        startManagingCursor(c);
        if(c.getCount()==0){
            Toast.makeText(getApplicationContext(), "NO record found", Toast.LENGTH_LONG).show();
        }
        else{




            c.moveToFirst();
            while(c.isAfterLast()==false){
                pno=c.getString(0);
                msg=c.getString(1);
                date=c.getString(2);
                time=c.getString(3);

                Toast.makeText(getApplicationContext(), "Data"+pno+"  "+msg+"  "+date+"  "+time, Toast.LENGTH_LONG).show();


                SimpleDateFormat dateFormat1 = new SimpleDateFormat("HH:mm"); 

                 Date date1 = null;
                try {
                 date1 = dateFormat1.parse(time);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 int date1hh=date1.getHours();
                 int date1mm=date1.getMinutes();
              Toast.makeText(getApplicationContext(), "hh:"+date1hh+"mm:"+date1mm,Toast.LENGTH_LONG).show();

                 SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd-MM-yyyy"); 
                 Date date2 = null;
                try {
                    date2 = dateFormat2.parse(date);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 Toast.makeText(getApplicationContext(), "date"+date2, Toast.LENGTH_LONG).show();
                 int date2yy=date2.getYear()+1900;
                 int date2mm=date2.getMonth()+1;
                 int date2dd=date2.getDate();


                 Calendar zCalendarL = Calendar.getInstance(); 
                SimpleDateFormat zDateFormatL = new SimpleDateFormat("yyyy/MM/dd HH:mm");

                String DateTime=date2yy+"/"+date2mm+"/"+date2dd+" "+date1hh+":"+date1mm;
                //String DateTime="2014/01/31 13:00";
            Date d1 = null;
                try {
                    d1 = zDateFormatL.parse(DateTime);
                    //d1 = zDateFormatL.parse(date);
            } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                long diff =Math.abs(System.currentTimeMillis() - d1.getTime()) ;
                long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diff);
                final int sec=(int)diffInSec;
                Toast.makeText(getApplicationContext(), "diff::"+String.valueOf(sec), Toast.LENGTH_LONG).show();
                //Toast.makeText(getApplicationContext(), "diff::"+String.valueOf(diffInSec), Toast.LENGTH_LONG).show();
                //Toast.makeText(getApplicationContext(), "year:"+date2yy+"month:"+date2mm+"day"+date2dd,Toast.LENGTH_LONG).show();
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub

                        try {
                            Thread.sleep(sec*1000);

                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        h.post(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                SmsManager smsManager = SmsManager.getDefault();
                                  smsManager.sendTextMessage(pno, null, msg, null, null);
                                  Toast.makeText(getApplicationContext(), "Message is sent", Toast.LENGTH_LONG).show();
                            }
                        });

                    }
                }).start();

                            c.moveToNext();
            }
        }

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == btnCalendar) {

            // Process to get Current Date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);

            // Launch Date Picker Dialog
            DatePickerDialog dpd = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                int monthOfYear, int dayOfMonth) {
                            // Display Selected date in textbox
                            txtDate.setText(dayOfMonth + "-"
                                    + (monthOfYear + 1) + "-" + year);

                        }
                    }, mYear, mMonth, mDay);
            dpd.show();
        }
        if (v == btnTimePicker) {

            // Process to get Current Time
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            // Launch Time Picker Dialog
            TimePickerDialog tpd = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {

                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                int minute) {
                            // Display Selected time in textbox
                            txtTime.setText(hourOfDay + ":" + minute);
                        }
                    }, mHour, mMinute, false);
            tpd.show();
        }
        if(v==btnset){


            ContentValues values=new ContentValues();
            values.put("pno", txtpno.getText().toString());
            values.put("msg", txtmsg.getText().toString());
            values.put("date", txtDate.getText().toString());
            values.put("time", txtTime.getText().toString());

            mdatabase=openOrCreateDatabase("timedb", SQLiteDatabase.OPEN_READWRITE, null);
        long newid=mdatabase.insert("SmsTime", null, values);
        //mdatabase.delete("SmsTime", null, null);
            Toast.makeText(getApplicationContext(),"Record inserted Succesfully", Toast.LENGTH_LONG).show();
            txtpno.setText("");
            txtmsg.setText("");
            txtDate.setText("");
            txtTime.setText("");

            mdatabase.close();


        }

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

2.现在的问题是它一次只发送一条短信从数据库再次发送另一个应该运行程序,我应该如何在这段代码中使用服务请帮助我。

4

0 回答 0