i don't know why but i have problem with alarm manager
and sms manager
...my code schedule sms but don't send sms after time that i choose.
I hope that you can help me!
Thanks everybody in advance!
I show you my code:
MAIN ACTIVITY:
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;
}
}
ALARM RECEIVER:
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;
}
}