自动发送Android短信

时间:2018-06-13 13:45:32

标签: java firebase-realtime-database sms alarmmanager smsmanager

[Firebase数据库截图] [1]

[![firebase数据库] [1]] [1]

我是android和firebase的初学者,我正在开发一个驾驶学校应用程序。 我将成功地将客户详细信息添加到firebase数据库('名称''电话',' fromdate'以及' todate')。我想向客户发送短信提醒,例如("您的许可证即将到期。")使用'电话'和' date'来自firebase数据库。短信应该在到期日发送(我的意思是' todate'在firebase数据库中)。在这里,我附上了我的firebase数据库截图。请举例说明如何做到这一点。

更新客户详情

public class Updatecustomer extends AppCompatActivity {


private EditText edtname, edtpho;
private Button btnsubmit;
private EditText datepickerto, datepickerfr;
private Calendar mcurrentDate, mlateDate;

int day, month, year;

private DatabaseReference childref;
private DatabaseReference listref;
private DatabaseReference noderef;

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

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle("Update your Customer Details");

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);


    datepickerto = (EditText)findViewById(R.id.datepickerto);
    datepickerfr = (EditText)findViewById(R.id.datepickerfr);

    edtname = (EditText)findViewById(R.id.edtname);
    edtpho = (EditText)findViewById(R.id.edtpho);

    btnsubmit = (Button)findViewById(R.id.btnsubmit);


    mcurrentDate = Calendar.getInstance();

    day = mcurrentDate.get(Calendar.DAY_OF_MONTH);
    month = mcurrentDate.get(Calendar.MONTH);
    year = mcurrentDate.get(Calendar.YEAR);

    day = day+1;

    datepickerto.setText(day+"-"+month+"-"+year);

    datepickerto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            DatePickerDialog datePickerDialog = new 
  DatePickerDialog(Updateproduction.this, new 
  DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int 
  monthOfYear, int dayOfMonth) {
                    monthOfYear =monthOfYear+1;
                    datepickerto.setText(dayOfMonth+"-"+monthOfYear+"- 
  "+year);
                }
            }, year, month, day);
            datePickerDialog.show();
        }
     });

    mlateDate = Calendar.getInstance();

    day = mlateDate.get(Calendar.DAY_OF_MONTH);
    month = mlateDate.get(Calendar.MONTH);
    year = mlateDate.get(Calendar.YEAR);

    month = month+1;

    datepickerfr.setText(day+"-"+month+"-"+year);

    datepickerfr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            DatePickerDialog datePickerDialog = new 
  DatePickerDialog(Updatecustomer.this, new 
  DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int 
  monthOfYear, int dayOfMonth) {
                    monthOfYear =monthOfYear+1;
                    datepickerfr.setText(dayOfMonth+"-"+monthOfYear+"- 
  "+year);
                }
            }, year, month, day);
            datePickerDialog.show();
        }
    });


    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference table_user = database.getReference();

    childref = table_user.child("Rajadriving");


    btnsubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           final ProgressDialog mDiaglog = new 
  ProgressDialog(Updateproduction.this);
           mDiaglog.setMessage("Please Waiting...");
           mDiaglog.show();

            table_user.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

    //if(dataSnapshot.child(spinnershift.getSelectedItem()
   .toString()).exists())
                    //  {
                    //     mDiaglog.dismiss();
                    //    Toast.makeText(Updatecustomer.this, "Data 
 already Stored", Toast.LENGTH_SHORT).show();
                    // }
                    //  else
                    {
                        mDiaglog.dismiss();
                        Datastore user = new 


  Datastore(datepickerfr.getText().toString(),datepickerto.getText()
 .toString(),
                                edtpho.getText().toString().trim(), 
  edtname.getText().toString().trim());


                        listref = 
 childref.child(datepickerfr.getText().toString());
                        noderef =  
 listref.child(edtname.getText().toString());
                        noderef.setValue(user);

                        //Intent addintent = new 
Intent(MainActivity.this,Paid.class);
                        // startActivity(addintent);
                        Toast.makeText(Updatecustomer.this, "Data saved 
successfully !", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    });


 }


 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == android.R.id.home){
        this.finish();
    }
    return super.onOptionsItemSelected(item);
 }
 }

activity_update_customer.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.udayaj.rajadriving.Updatecustomer">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Raja Driving School"
    android:textStyle="bold"
    android:textColor="#f05"
    android:textSize="25dp"
    android:fontFamily="sans-serif"
    android:gravity="center"
    android:id="@+id/textView" />

<EditText
    android:id="@+id/edtname"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/datepickerfr"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="35dp"
    android:hint="Enter Customer Name"
    android:inputType="textPersonName"
    android:textSize="16dp" />

<EditText
    android:id="@+id/edtpho"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/edtname"
    android:layout_below="@+id/edtname"
    android:layout_marginTop="19dp"
    android:hint="Enter Phone No."
    android:inputType="number"
    android:minLines="10"/>


<Button
    android:id="@+id/btnsubmit"
    android:layout_width="150dp"
    android:layout_height="55dp"
    android:layout_below="@+id/edtpho"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="96dp"
    android:background="@drawable/sty_submit"
    android:text="Submit"
    android:textColor="@android:color/white" />

<EditText
    android:id="@+id/datepickerfr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView5"
    android:layout_marginEnd="19dp"
    android:layout_toStartOf="@+id/datepickerto"
    android:hint="Enter From Date"
    android:inputType="date" />

<EditText
    android:id="@+id/datepickerto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/textView5"
    android:layout_marginEnd="42dp"
    android:hint="Enter Expire Date"
    android:inputType="date" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="23dp"
    android:text="Date use this format eg:6-6-2018."
    android:id="@+id/textView5" />

 </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您可以先查看“服务”并创建一个检查今天(每天)的服务,然后从数据库中请求expiry_date == todays_date的任何记录。

Services tutorial

如果是,则收集列表中的电话号码并通过短信发送方式处理它们。

SMS Sending

在测试SMS功能时,请确保您已在Manifest文件中请求了权限,并且在执行privelage之前,根据Android版本请求。