gpt4 book ai didi

java - 重启我的设备后,频繁发送短信就是在开机时再发送一次短信

转载 作者:太空狗 更新时间:2023-10-29 14:17:38 26 4
gpt4 key购买 nike

我经常发送短信,同时重启我的设备再发送一次短信,同时我们也“开机”。之后它发送短信的时间间隔正确。但是当我们打开我们的设备时它又发送一次短信。

public class BootCompletedIntentReceiver extends BroadcastReceiver {     

@Override
public void onReceive(Context ctxt, Intent i) {
try {
scheduleAlarms(ctxt);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@SuppressLint("SimpleDateFormat")
static void scheduleAlarms(Context ctxt) throws java.text.ParseException {
String share_pref_file = "IMS";
SharedPreferences prefs = ctxt.getSharedPreferences(share_pref_file,
Context.MODE_PRIVATE);

String number = prefs.getString("extraSmsNumber", "");
String message= prefs.getString("extraSmsText", "");
String mytime = prefs.getString("Mytime", "");
int year= prefs.getInt("Year", 0);
int month= prefs.getInt("Month", 0);
int dmonth= prefs.getInt("DMonth", 0);
int hour= prefs.getInt("Hour", 0);
int time= prefs.getInt("Time", 0);
String nyear = String.valueOf(year);
String nmonth = String.valueOf(month);
String ndmonth = String.valueOf(dmonth);
String nhour = String.valueOf(hour);
String ntime = String.valueOf(time);
//int tottime = dmonth + month + year + hour + time;
String newtime = ndmonth + "-" + nmonth + "-" + nyear + "/" + nhour + ":" + ntime;
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy/hh:mm");
Date startDate=null;
try {
startDate = df.parse(newtime);
String newDateString = df.format(startDate);

} catch (ParseException e) {
e.printStackTrace();
}

AlarmManager alarmManager =
(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(ctxt, MyAlarmService.class);
PendingIntent pendingIntent=PendingIntent.getService(ctxt, 0, i, 0);



SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy/hh:mm");
//SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYYY/HH:mm");
String currentLocalTime =sdf.format(new Date());
Date startTDate = null;
try {
startTDate = sdf.parse(currentLocalTime);
String newDateString = df.format(startTDate);
System.out.println(newDateString);
} catch (ParseException e) {
e.printStackTrace();
}


if(startDate.compareTo(startTDate) < 0)
{
Toast.makeText(
ctxt,
"Welcome", Toast.LENGTH_LONG).show();

}



if (mytime.equals("Once")) {
Calendar calendar = Calendar.getInstance();

calendar.set(year, month,
dmonth,
hour,
time, 0);

alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
} else if (mytime.equals("Every 5 Minutes")) {
Calendar calendar = Calendar.getInstance();

calendar.set(year, month,
dmonth,
hour,
time, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 60 * 5,
pendingIntent); // Millisec * Second *
// Minute
} else if (mytime.equals("Every hour")) {
Calendar calendar = Calendar.getInstance();

calendar.set(year, month,
dmonth,
hour,
time, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 60 * 60,
pendingIntent); // Millisec * Second *
// Minute
} else if (mytime.equals("Every day")) {
Calendar calendar = Calendar.getInstance();

calendar.set(year, month,
dmonth,
hour,
time, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
24 * 60 * 60 * 1000, pendingIntent);

} else if (mytime.equals("Weekly")) {
Calendar calendar = Calendar.getInstance();

calendar.set(year, month,
dmonth,
hour,
time, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 7 * 24 * 60
* 60 * 1000, pendingIntent);

} else if (mytime.equals("Weekdays(Mon-Fri)")) {

forWeekdays(ctxt, year, month,
dmonth,
hour,
time);

} else if (mytime.equals("Weekend")) {
forWeekend(ctxt, year, month,
dmonth,
hour,
time);

} else if (mytime.equals("Every month")) {
Calendar calendar = Calendar.getInstance();

calendar.set(year, month,
dmonth,
hour,
time, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 30 * 24 * 60
* 60 * 1000, pendingIntent);

} else {
Calendar calendar = Calendar.getInstance();

calendar.set(year, month,
dmonth,
hour,
time, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 365 * 24 * 60
* 60 * 1000, pendingIntent);

}
}

这是我的服务等级

public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);

String share_pref_file = "IMS";
SharedPreferences prefs = getSharedPreferences(share_pref_file,
Context.MODE_PRIVATE);

String number = prefs.getString("extraSmsNumber", "");
String message = prefs.getString("extraSmsText", "");

Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
.show();
Toast.makeText(
this,
"MyAlarmService.onStart() with \n" + "smsNumberToSend = "
+ number + "\n" + "smsTextToSend = " + message,
Toast.LENGTH_LONG).show();

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, message, null, null);
}

发生的错误是在我们开机时再发送一条短信

如果我每 5 分钟发送一次短信,我从 5.0 开始发送 5.00 和 5.05,然后我在 5.07 关闭我的设备并在 5.08 打开,这样 5.08 也会发送短信,然后它继续在 5.01、5.15 发送短信,就像那样。只有 5.08 发送短信是问题所在。

服务类在重启一次后自动启动。

最佳答案

Service class automatically started after rebooting one time.

关于“重启后服务自动启动”这个问题,很多网友提出了很多问题。

作为解决方案,他们中的许多人建议使用 START_NOT_STICKY 返回值 onStartCommand() 方法(它可能适用于许多人,但不适合我)。

注意:它适用于较低版本的 android 2.3.3 和 3.0,但不适用于 4.0 及更高版本。

经过bit的分析,发现startID在自动启动时和用户调用时似乎赋值不一样。

所以对我来说 startID 起了个作用

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(startId!=2)
{
//to do
}
return super.onStartCommand(intent, flags, startId);
}

And For tracking your time even after reboot.

我不确定android是否提供用于跟踪时间的API,但可以通过简单的数学计算来实现。

为此,您需要跟踪上次触发警报的时间(使用 sharedpreference 以毫秒为单位存储日期/时间)。

long lastInvoked = preferences.getLong(AndroidAlarmSMS.LASTALARAMINVOKED, -1);
Log.d("last_time_invoked",""+lastInvoked);
long currentTime = System.currentTimeMillis();
long period = 1000*60*5; // using 5 min of interval to repeat
long diff = currentTime - lastInvoked;

Log.d("difference",""+diff);

if(diff > period)
{
long result= diff % period;
long nextInvokeAt;
if(result > period)
{
nextInvokeAt = currentTime + (result - result);
}
else{
nextInvokeAt = currentTime + (period - result);
}

Log.d("invoked_next_time",""+nextInvokeAt);
setPendingIntent(context,nextInvokeAt,period);
}
else
{
long result= period-diff;
long nextInvokeAt = currentTime + result;
Log.d("next_time_invoked_else",""+nextInvokeAt);
setPendingIntent(context,nextInvokeAt,period);
}

并在您的广播接收器类中为操作“BOOT_COMPLETED”执行上述操作,并使用结果时间设置闹钟

public static void setPendingIntent(Context ctx,long time,long period){
AlarmManager alarmManager=
(AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(ctx,
MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(ctx, 0, myIntent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
time, period , pendingIntent);

Toast.makeText(ctx, "repeating --> "+time, Toast.LENGTH_LONG).show();
}

注意:这不是解决这个问题的标准答案,但暂时对我有用,希望它能帮助你解决这个问题。

注意:也在寻找更好的答案...

关于java - 重启我的设备后,频繁发送短信就是在开机时再发送一次短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20374238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com