重新启动我的设备,发送短信经常是在发送短信一次也同时开关后 [英] After rebooting my device,sending sms frequently is sending sms one more time also while switch on

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

问题描述

我经常发送短信,而重新启动我的设备发送短信更多的时间,而我们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的发送短信的问题。

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


解决方案

  

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


有很多的问题张贴在这个问题上很多用户服务重新启动后自动启动。

由于其中许多的解决方案,在返回值与onStartCommand START_NOT_STICKY()方法的建议(它可能工作了很多,但不适合我)。

请注意:它的工作原理很好较低Android版本2.3.3和3.0,但不是在4.0和更高版本的

分析位后,来到知道startID似乎被赋予不同的值时,它会自动启动,并在用户调用。

所以对我来说startID捉弄

  @覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){
 如果(startId!= 2)
        {
               //去做
            }
 返回super.onStartCommand(意向,旗帜,startId);
}


  

和甚至​​重新启动后,跟踪你的时间。


我不知道,如果机器人跟踪的时候提供了一个API,但它可以用简单的数学计算来实现。

有关这一点,你需要跟踪在什么时候已经报警了发射最后一次(使用共享preference存储毫秒的日期/时间)。​​

 长lastInvoked = preferences.getLong(AndroidAlarmSMS.LASTALARAMINVOKED,-1);
            Log.d(last_time_invoked,+ lastInvoked);
            长currentTime的= System.currentTimeMillis的();
            长周期= 1000 * 60 * 5;用5分钟间隔重复//
            长差异= currentTime的 - lastInvoked;            Log.d(差异化,+差异);            如果(DIFF>期)
            {
                长期结果=差异%期;
                长nextInvokeAt;
                如果(结果>期)
                {
                    nextInvokeAt = currentTime的+(结果 - 结果);
                }
                其他{
                    nextInvokeAt = currentTime的+(期 - 结果);
                }                Log.d(invoked_next_time,+ nextInvokeAt);
                setPendingIntent(背景下,nextInvokeAt,周期);
            }
            其他
            {
                长期结果=期初差异;
                长nextInvokeAt = currentTime的结果+;
                Log.d(next_time_invoked_else,+ nextInvokeAt);
                setPendingIntent(背景下,nextInvokeAt,周期);
            }

和做你的广播接收器类的上述操作对行动BOOT_COMPLETED,并用所得时间设置报警

 公共静态无效setPendingIntent(上下文CTX,时间长了,很长一段){
        AlarmManager alarmManager =
                (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
        意图myIntent =新意图(CTX,
                MyAlarmService.class);
        的PendingIntent的PendingIntent = PendingIntent.getService(CTX,0,myIntent,0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                时间,周期,的PendingIntent);        Toast.makeText(CTX,重复 - >中+时间,Toast.LENGTH_LONG).show();
    }

注意:这是不是解决这个问题的标准答案,但目前它为我工作,希望它可以帮助你解决这个

注意:寻找更好的答案太...

I am sending sms frequently,while rebooting my device sending sms one more time while we "switch on" also.After that its sending sms correct intervals.But its sending the sms one more time when we switch on our device.

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

            }
}

this is my service class

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

The error occuring is sending one more sms while we switch on

if i am seeting to send sms at every 5 minutes, and i am starting at 5.0 clock na its sending 5.00 and 5.05 and after that i switched off my device at 5.07 and switch on at 5.08,so that 5.08 also sending sms,and then its continue to sending sms at 5.01 ,5.15 like that.only that 5.08 its sending sms is the problem.

Service class automatically started after rebooting one time.

解决方案

Service class automatically started after rebooting one time.

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

And For tracking your time even after reboot.

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...

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆