无法发送自动短信

Unable to send Automatic Text Messages

我正在做一个项目,它需要一个日期和时间,并在那天自动向指定的手机号码发送一条预先写好的消息。 我正在为此使用警报管理器,但它不起作用。长期以来我一直在尝试调试我的程序,以至于我看不出到底出了什么问题。

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);
    }
}

如果您浏览 Use of SMS or Call Log permission groups,您会发现从 2018 年 12 月起,Playstore 不允许使用 SEND_SMS 权限的应用,除非它们是默认的 SMS/Dialer 应用。您必须将您的应用程序作为例外文件或删除 SMS 权限。

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.

问题的答案:

好吧,回到您的问题,许多 android 设备制造商正在使用积极的策略来节省电池。当用户从最近的选项卡中清除 his/her 应用程序时,该应用程序将被强制关闭,从而取消所有警报、广播接收器、服务等。大多数设备制造商都会出现这种情况,例如 OnePlus、Huwaei、小米、Vivo、Oppo 等。

他们有 AutoStartManagers/AutoLaunchManagers 阻止应用程序后台 运行。您必须使用 THIS SO ANSWER.

中提到的步骤将您的应用列入白名单