1

I'm working on a Project that takes a date and time and automatically sends a pre-written message on that day to the specified Mobile number. I'm using alarm Manager for this, but it's not working. I've been trying to debug my program for so long that I'm unable to see what's exactly wrong.

final Calendar c = Calendar.getInstance();
String date=releaseDateEditText.getText().toString();
String data[]= date.split("-");
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(data[0]));
c.set(Calendar.MONTH,Integer.parseInt(data[1]));
c.set(Calendar.YEAR,Integer.parseInt(data[2]));
c.set(Calendar.AM_PM, Calendar.PM);
c.set(Calendar.HOUR_OF_DAY, 11);
c.set(Calendar.MINUTE, 18);
c.set(Calendar.SECOND, 0);
Intent _myIntent = new Intent(getApplicationContext(), message.class);
_myIntent.putExtra("name", name.getText());
_myIntent.putExtra("agency", agency.getText());
_myIntent.putExtra("book", bookingDateEditText.getText());
_myIntent.putExtra("release", releaseDateEditText.getText());
pintent = PendingIntent.getBroadcast(getApplicationContext(), 1, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pintent);
Toast.makeText(getApplicationContext(), "Alarm set for " + releaseDateEditText.getText(), Toast.LENGTH_LONG).show();

public class message extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String SPhone = "Phonenumber";
        String SSms = intent.getStringExtra("name");
        SSms = SSms + "\n" + intent.getStringExtra("agency") + "\n" + intent.getStringExtra("book") + "\n" + intent.getStringExtra("release");
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(SPhone, null, SSms, null, null);
    }
}
Samir
  • 3,139
  • 2
  • 17
  • 24
Mrak Vladar
  • 598
  • 4
  • 18

1 Answers1

1

If you go through Use of SMS or Call Log permission groups, you will find that from DEC 2018 onwards, apps using permissions to SEND_SMS are not allowed on playstore, unless they are default SMS/Dialer app. Either you will have to file your app as exception or remove the SMS permissions.

For apps requesting access to the SMS or Call Log permissions, the intended and permitted uses include default SMS handling, default phone handling, or Assistant handling capability.

Apps must be actively registered as the default SMS, Phone, or Assistant handler before prompting users to accept any of the above permissions and must immediately stop the use of the permission when they no longer are the default handler.

ANSWER TO QUESTION:

Well, coming back to your question, many android device manufacturers are using aggressive policies to save battery. When a user clears his/her app from recent tabs, the app is force closed, thus cancelling all alarms,broadcastReceivers,services etc. This happens in most of the device manufacturers like OnePlus,Huwaei, Xiaomi, Vivo, Oppo etc.

They have AutoStartManagers/AutoLaunchManagers that prevent the background running of the apps. You will have to white list your app using steps mentioned in THIS SO ANSWER.

Community
  • 1
  • 1
ankuranurag2
  • 2,300
  • 15
  • 30