本文介绍了重新启动我的设备,发送短信经常是在发送短信一次也同时开关后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发送短信,而重新启动我的设备发送短信更多的时间,而我们also.After的开关,其发送短信纠正intervals.But其发送短信一次,当我们切换我们的设备上。

 公共类BootCompletedIntentReceiver扩展广播接收器{          @覆盖
          公共无效的onReceive(上下文ctxt,意图我){
            尝试{
                scheduleAlarms(ctxt);
            }赶上(java.text.ParseException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }
          }          @燮pressLint(的SimpleDateFormat)
        静态无效scheduleAlarms(上下文ctxt)抛出java.text.ParseException {
              字符串share_ pref_file =IMS;
                共享preferences preFS = ctxt.getShared preferences(share_ pref_file,
                Context.MODE_PRIVATE);                串号= prefs.getString(extraSmsNumber,);
                字符串消息= prefs.getString(extraSmsText,);
                字符串数值指明MyTime = prefs.getString(数值指明MyTime,);
                年整型= prefs.getInt(年,0);
                INT月= prefs.getInt(月,0);
                INT d月= prefs.getInt(d月,0);
                INT小时= prefs.getInt(一小时,0);
                INT时间= prefs.getInt(时代,0);
                字符串nyear =将String.valueOf(年);
                字符串nmonth =将String.valueOf(月);
                字符串ndmonth =将String.valueOf(d月);
                串nhour =将String.valueOf(小时);
                字符串n时间=将String.valueOf(时间);
                // INT tottime = d月+月+年+小时+的时间;
                串NEWTIME = ndmonth + - + nmonth + - + nyear +/+ nhour +:+ n时间;
                SimpleDateFormat的DF =新的SimpleDateFormat(DD-MM-YYYY / HH:MM);
                日期的startDate = NULL;
                尝试{
                    的startDate = df.parse(NEWTIME);
                    字符串newDateString = df.format(的startDate);                }赶上(ParseException的E){
                    e.printStackTrace();
                }                 AlarmManager alarmManager =
                            (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
                        意图I =新意图(ctxt,MyAlarmService.class);
                        的PendingIntent的PendingIntent = PendingIntent.getService(ctxt,0,I,0);                        SimpleDateFormat的SDF =新的SimpleDateFormat(DD-MM-YYYY / HH:MM);
                 // SimpleDateFormat的SDF =新的SimpleDateFormat(DD / MM / YYYY / HH:MM);
                 字符串currentLocalTime = sdf.format(新的Date());
                    日期startTDate = NULL;
                    尝试{
                        startTDate = sdf.parse(currentLocalTime);
                        字符串newDateString = df.format(startTDate);
                        的System.out.println(newDateString);
                    }赶上(ParseException的E){
                        e.printStackTrace();
                    }
            如果(startDate.compareTo(startTDate)℃,)
            {
                Toast.makeText(
                        ctxt,
                        欢迎光临,Toast.LENGTH_LONG).show();            }            如果(mytime.equals(曾经)){
                台历挂历= Calendar.getInstance();                calendar.set(年,月,
                        d月,
                        小时,
                        时间,0);                alarmManager.set(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(),的PendingIntent);
            }否则如果(mytime.equals(每5分钟)){
                台历挂历= Calendar.getInstance();                calendar.set(年,月,
                        d月,
                        小时,
                        时间,0);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(),1000 * 60 * 5,
                        的PendingIntent); //毫秒*秒*
                                        //分钟
            }否则如果(mytime.equals(每个小时)){
                台历挂历= Calendar.getInstance();                calendar.set(年,月,
                        d月,
                        小时,
                        时间,0);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(),1000 * 60 * 60,
                        的PendingIntent); //毫秒*秒*
                                        //分钟
            }否则如果(mytime.equals(天天向上)){
                台历挂历= Calendar.getInstance();                calendar.set(年,月,
                        d月,
                        小时,
                        时间,0);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(),
                        24 * 60 * 60 * 1000的PendingIntent);            }否则如果(mytime.equals(周末画报)){
                台历挂历= Calendar.getInstance();                calendar.set(年,月,
                        d月,
                        小时,
                        时间,0);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(),7 * 24 * 60
                                * 60 * 1000的PendingIntent);            }否则如果(mytime.equals(平日(周一至周五))){                forWeekdays(ctxt,年,月,
                        d月,
                        小时,
                        时间);            }否则如果(mytime.equals(周末)){
                forWeekend(ctxt,年,月,
                        d月,
                        小时,
                        时间);            }否则如果(mytime.equals(每个月)){
                台历挂历= Calendar.getInstance();                calendar.set(年,月,
                        d月,
                        小时,
                        时间,0);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(),30 * 24 * 60
                                * 60 * 1000的PendingIntent);            }其他{
                台历挂历= Calendar.getInstance();                calendar.set(年,月,
                        d月,
                        小时,
                        时间,0);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(),365 * 24 * 60
                                * 60 * 1000的PendingIntent);            }
}

这是我的服务类

 公共无效在onStart(意向意图,诠释startId){
        super.onStart(意向,startId);        字符串share_ pref_file =IMS;
        共享preferences preFS = getShared preferences(share_ pref_file,
                Context.MODE_PRIVATE);        串号= prefs.getString(extraSmsNumber,);
        字符串消息= prefs.getString(extraSmsText,);        Toast.makeText(这一点,MyAlarmService.onStart(),Toast.LENGTH_LONG)
                。显示();
        Toast.makeText(
                这个,
                MyAlarmService.onStart()与\\ n+smsNumberToSend =
                        +数字+\\ n+smsTextToSend =+消息,
                Toast.LENGTH_LONG).show();        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(数字,空,消息,NULL,NULL);
    }

错误发生的历史是发送一条短信多,而我们打开

如果我seeting以每5分钟发送短信,我开始在5.0时钟呐其发送5.00和5.05,之后我关掉我的设备在5.07和5.08接通,使5.08也发送短信,然后将其继续发送短信5.01,5.15像that.only了5.08的发送短信的问题。

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


解决方案

There are lots of questions posted by many users on this issue "Service automatically started after rebooting".

As a solution many of them suggested on return value onStartCommand() method with START_NOT_STICKY (It may worked for many, but isn't for me).

Note: it works good for lower versions of android 2.3.3 and 3.0 but not on 4.0 and later versions.

After analyzing bit ,came to known that startID seems to be giving different value when it is starting automatically, and on invoking by users.

So for me startID played a trick

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

I am not sure if android provide a API for tracking the time,but it can be achieved with simple mathematical computation.

For this you need to track at what time Alarm has fired for last time (use sharedpreference storing date/time in milli seconds).

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

and do the above operations in your Broadcast receiver class for action "BOOT_COMPLETED" and set the alarm with the resultant time

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

Note: It is not a standard answer to resolve this issue, but for time being it work for me, hope it may help you to resolve this.

Note: Looking for better answers too...

这篇关于重新启动我的设备,发送短信经常是在发送短信一次也同时开关后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 03:41