0

SMSが将来送信されるAndroidアプリケーションを開発しようとしています。将来の日付は、ユーザーからの入力として取得されます。ここでの問題は、カウント ダウン タイマーを数日や数時間などの長期間にわたって使用する方法です。

package com.example.sked;

//import java.text.SimpleDateFormat;

import java.util.Calendar;

//import java.util.Locale;

import java.util.concurrent.TimeUnit;

import android.app.Activity;

import android.app.AlertDialog;

import android.os.Bundle;

import android.os.CountDownTimer;

import android.telephony.SmsManager;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.EditText;

import android.widget.TimePicker;

import android.widget.Toast;



public class ScheduleActivity extends Activity
{


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_schedule);
        super.onCreate(savedInstanceState);

        Calendar cal=Calendar.getInstance();

        final int year_set=cal.get(Calendar.YEAR);
        final int month_set=cal.get(Calendar.MONTH);
        final int day_set=cal.get(Calendar.DAY_OF_MONTH);

        final int hr_set=cal.get(Calendar.HOUR);
        final int min_set=cal.get(Calendar.MINUTE);

        final   DatePicker dp_c = (DatePicker) findViewById(R.id.datePicker1);
        final   TimePicker tp_c = (TimePicker) findViewById(R.id.timePicker1);

        dp_c.updateDate(year_set, month_set, day_set); //Setting current date in date picker //

        tp_c.setCurrentHour(hr_set);  //Setting current time in time picker //
        tp_c.setCurrentMinute(min_set);


        //For Scheduling a message//
        final Button view = (Button) findViewById(R.id.set_message);
        {
        view.setOnClickListener(new View.OnClickListener()
        {

        @Override
        public void onClick(View v)
        {
            // Processing here 
            EditText text = (EditText)findViewById(R.id.reci_number);
            final String phno= text .getText().toString();

            EditText text1 = (EditText)findViewById(R.id.message);
            final String msg= text1.getText().toString();

            Calendar cal=Calendar.getInstance();//Calculating current date in milliseconds//

            final int year_curr=cal.get(Calendar.YEAR);
            final int month_curr=cal.get(Calendar.MONTH);
            int day_curr=cal.get(Calendar.DAY_OF_MONTH);

            final int hr_curr=cal.get(Calendar.HOUR);
            final int min_curr=cal.get(Calendar.MINUTE);

            day_curr= year_curr*365+month_curr*30;//Converting year to days and months to days//


            long day_curr_mil=TimeUnit.MILLISECONDS.convert(day_curr, TimeUnit.DAYS);

            long hours_curr_mil=TimeUnit.MILLISECONDS.convert(hr_curr, TimeUnit.HOURS);
            long mins_curr_mil=TimeUnit.MILLISECONDS.convert(min_curr, TimeUnit.MINUTES);

            final long elapsedtimer_curr= day_curr_mil+hours_curr_mil+mins_curr_mil;



            DatePicker dp = (DatePicker) findViewById(R.id.datePicker1); 
            int day = dp.getDayOfMonth(); //Receiving date from the user//
            int month = dp.getMonth() + 1;
            int year = dp.getYear();

            TimePicker tp=(TimePicker) findViewById(R.id.timePicker1);
            int hours= tp.getCurrentHour();//Receiving time from the user//
            int minutes= tp.getCurrentMinute();

            day= year*365+month*30;//Converting year to days and months to days//


            long day_sel=TimeUnit.MILLISECONDS.convert(day, TimeUnit.DAYS);

            long hours_sel=TimeUnit.MILLISECONDS.convert(hours, TimeUnit.HOURS);
            long mins_sel=TimeUnit.MILLISECONDS.convert(minutes, TimeUnit.MINUTES);

            long elapsedtimer_sel= day_sel+hours_sel+mins_sel;

            if(phno.length()==10)//Checking length of the mobile number//
            {
            if(msg.length()<=160 && msg.length()>0) //Checking length of the message//
            {


                        final String phoneNumber = phno;
                        final String message = msg;


                        long elapsed_act=elapsedtimer_sel-elapsedtimer_curr;

                        new CountDownTimer(elapsed_act, 1000) {

                            public void onTick(long millisUntilFinished) {

                            }

                            public void onFinish() 
                            {
                                /*Sending the message*/
                                SmsManager smsManager = SmsManager.getDefault();
                                smsManager.sendTextMessage(phoneNumber, null, message, null, null);
                            }
                         }.start();


                        final AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(ScheduleActivity.this, 0);

                          //set  the dialog  
                            dlgAlert.setMessage("Message successfully scheduled at the specified time & date");
                            dlgAlert.setTitle("Success");
                            dlgAlert.setPositiveButton("OK", null);
                            dlgAlert.setCancelable(true);
                            dlgAlert.create().show();




            }  

            else
            {

                Toast.makeText(getBaseContext(),
                "Message too long or too short .... cannot send ... :( ",Toast.LENGTH_SHORT).show();
            }
            }
            else
            {

                Toast.makeText(getBaseContext(), 
                    "Check the number Entered",Toast.LENGTH_SHORT).show();
            }
            }


        }

        );



        }



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.schedule, menu);
        return true;
    }

}
4

2 に答える 2

0

参考のために、この回答を投稿しています。時間遅延を使用して通知を送信したため、変更して使用できるため、これから助けを得ることができます

public class AutoNotification extends Activity {
    private Timer refresh = null;
    private final long refreshDelay = 5 * 1000;//this delay will be your time that the user will provide you
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_auto_notification);

        sensSMSThread = new sensSMSRequest();
        sensSMSThread.start();

    }
    class sensSMSRequest extends Thread {
        boolean sendHttpRequest;
        public sensSMSRequest() {

            sendHttpRequest = true;
        }

        public void stopSendingHttpRequest() {
            sendHttpRequest = false;
        }

        protected void onStop() {
            sensSMSThread.stopSendingHttpRequest();
            super.stop();
        }

        @Override
        public void run() {
            while (sendHttpRequest) {
                sendSms();


                SystemClock.sleep(refreshDelay);
            }
        }
    }
}

どういうわけかそれがあなたを助けることを願っています

于 2013-09-12T06:01:15.760 に答える
0

インテント フィルタを作成します。

 static {
    s_intentFilter = new IntentFilter();
    s_intentFilter.addAction(Intent.ACTION_TIME_TICK);
    s_intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
    s_intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
}

および放送受信機:

private final BroadcastReceiver m_timeChangedReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(Intent.ACTION_TIME_CHANGED) ||
            action.equals(Intent.ACTION_TIMEZONE_CHANGED))
        {
           //if(current time == sechduled time)  doyourWork();
        }
    }
};

受信者をサービスに登録します。

 public void onCreate() {
    super.onCreate();
    registerReceiver(m_timeChangedReceiver, s_intentFilter);     
}

注 :デバイスの再起動後にサービスを再度開始することを忘れないでください。

于 2013-09-12T06:21:33.057 に答える