警报管理器和短信管理器的问题

时间:2016-12-29 08:45:23

标签: java android alarmmanager smsmanager

我不知道为什么,但我对alarm managersms manager有问题...我的代码安排短信但不会在我选择的时间后发送短信。

我希望你能帮助我!

提前感谢大家!

我告诉你我的代码:

主要活动:

public class MainActivity extends Activity {

    public String sPhone,sSms;
    private EditText etPhone,etSms;

    private Button bStart,bCancel,bTimeSelect,bPhone;

    static final int TIME_DIALOG_ID=1;
    private static final int REQUEST_CODE = 1;

    Calendar c;
    public int year,month,day,hour,minute;
    private int mHour,mMinute;

    private AlarmManager aManager;
    private PendingIntent pIntent;

    public MainActivity(){
        // Assign current Date and Time Values to Variables
        c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);
    }

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

        etPhone = (EditText)findViewById(R.id.etPhone);
        etSms = (EditText)findViewById(R.id.etSms);

        bStart = (Button)findViewById(R.id.bStart);
        bCancel = (Button)findViewById(R.id.bCancel);
        bTimeSelect = (Button)findViewById(R.id.bTime);
        bPhone = (Button)findViewById(R.id.bCPhone);

        //contact
        bPhone.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Uri uri = Uri.parse("content://contacts");
                Intent intent = new Intent(Intent.ACTION_PICK, uri);
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });

        //start schedule
        bStart.setOnClickListener(new View.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                sPhone = etPhone.getText().toString();
                sSms = etSms.getText().toString();
                etSms.getText().clear();

                Intent i = new Intent(MainActivity.this,AlarmReceiver.class);
                i.putExtra("exPhone", sPhone);
                i.putExtra("exSmS", sSms);


                pIntent = PendingIntent.getService(getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

                aManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                c.setTimeInMillis(System.currentTimeMillis());
                c.set(Calendar.HOUR_OF_DAY, hour);
                c.set(Calendar.MINUTE, minute);
                aManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pIntent);
                Toast.makeText(getApplicationContext(), "Sms scheduled! " + sSms, Toast.LENGTH_SHORT).show();
            }
        });

        //set time to send
        bTimeSelect.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(TIME_DIALOG_ID);
            }
        });

        //Cancel schedule
        bCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                aManager.cancel(pIntent);
                Toast.makeText(getApplicationContext(), "Cancel", Toast.LENGTH_SHORT).show();
            }
        });
    }

    //Choose phone in contact and set edit text
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent i) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, i);

        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Uri uri = i.getData();
                String[] projection = { ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };

                Cursor cursor = getContentResolver().query(uri, projection,
                        null, null, null);
                cursor.moveToFirst();

                int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                String number = cursor.getString(numberColumnIndex);

                etPhone.setText(number);
            }
        }
    }
    // Register  TimePickerDialog listener
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int min) {
                    hour = hourOfDay;
                    minute = min;
                    // Set the Selected Date in Select date Button
                    bTimeSelect.setText(hour+":"+minute);
                }
            };

    // Method automatically gets Called when you call showDialog()  method
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            // create a new TimePickerDialog with values you want to show
            case TIME_DIALOG_ID:
                return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false);
        }
        return null;
    }
}

警报接收器:

public class AlarmReceiver extends Service {

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int onStartCommand(Intent i, int flags, int startId) {
            // TODO Auto-generated method stub
            String SPhone =i.getStringExtra("exPhone");
            String SSms = i.getStringExtra("exSmS");

            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(SPhone, null, SSms, null, null);

            return START_STICKY;
        }

}

0 个答案:

没有答案