如何设置发送短信的时间和日期

时间:2017-08-23 06:59:48

标签: android

我希望设置发送短信的时间和日期,当时间和日期到来然后短信自动发送..现在我成功发送短信发送应用程序以及时间和日期选择器.....但现在的问题是如何设置发送类似警报的时间和日期。我们在其中设置警报时间

public class MainActivity extends AppCompatActivity {
Button button;
EditText edittext1,edittext2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.SEND_SMS)) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, 1);

        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, 1);

        }
    } else {
        //do
    }



    edittext1 = (EditText) findViewById(R.id.editText);
    edittext2 = (EditText) findViewById(R.id.editText2);
    button = (Button) findViewById(R.id.button);
    String number = edittext1.getText().toString();

   button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String number = edittext1.getText().toString();
            String sms = edittext2.getText().toString();

            try {
                SmsManager smsmanager = SmsManager.getDefault();
                smsmanager.sendTextMessage(number, null, sms, null, null);
                Toast.makeText(MainActivity.this, "Send sms", Toast.LENGTH_SHORT).show();

            } catch (Exception e) {

                Toast.makeText(MainActivity.this, "Send sms", Toast.LENGTH_SHORT).show();
            }


        }

    });





    ContentValues values = new ContentValues();
    values.put("address", number);//sender name
    values.put("body", "this is my text");
    getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,  int[] grantResults) {
    switch (requestCode){
        case 1:{
            if(grantResults.length>0&& grantResults[0] ==PackageManager.PERMISSION_GRANTED){
               if (ContextCompat.checkSelfPermission(MainActivity.this,
                        Manifest.permission.READ_PHONE_STATE) ==PackageManager.PERMISSION_GRANTED){
                   Toast.makeText(this,"Premission Granted",Toast.LENGTH_SHORT).show();

                }
            }else{
                Toast.makeText(this, "NO premiision granted", Toast.LENGTH_SHORT).show();
            }
        }

    }
}

}

我只想知道当设定的时间和日期到来时如何自动按下发送按钮...
  谢谢

2 个答案:

答案 0 :(得分:0)

尝试使用BroadcastReceiver。您可以接收时间更改操作,然后发布您的短信

答案 1 :(得分:0)

在您的活动中,使用您所需的时间和日期添加以下代码

 Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    int curHr = calendar.get(Calendar.HOUR_OF_DAY);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);

    if(calendar.getTimeInMillis() < System.currentTimeMillis()) {
        calendar.add(Calendar.DAY_OF_WEEK, 7);
    }
    calendar.set(Calendar.HOUR_OF_DAY, aHOUR);
    calendar.set(Calendar.MINUTE, aMINUTE);
    calendar.set(Calendar.SECOND, aSECOND);
    Intent intent1 = new Intent(getBaseContext(), YourReciever.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);

现在创建一个广播接收器,并在 OnRecieve()方法中调用短信发送功能。