Android:在Alarm Manager上调度SMS

Android:在Alarm Manager上调度SMS,android,Android,我正在编写应用程序,以安排来自alarm Manager的短信。它只为我工作一次,第二次以后,它会生成多条短信 时间保存在SQL LITE中,从广播接收器发送短信后读取数据库并设置下一个警报 如果发送了1条短信,则第二次发送2条短信。下次发3条短信,等等。我正在使用amammanager数组和PendingIntents数组设置报警 这是我的代码。。。请帮我。。。我花了一天的时间来做这件事 public void SetAlarm(MessageInfo info, long delay, in

我正在编写应用程序,以安排来自alarm Manager的短信。它只为我工作一次,第二次以后,它会生成多条短信

时间保存在SQL LITE中,从广播接收器发送短信后读取数据库并设置下一个警报

如果发送了1条短信,则第二次发送2条短信。下次发3条短信,等等。我正在使用amammanager数组和PendingIntents数组设置报警

这是我的代码。。。请帮我。。。我花了一天的时间来做这件事

public void SetAlarm(MessageInfo info, long delay, int amcount)
{

    try
    {
        AlarmMSGInfo = info;
        SaveMSGINFOPreference();

        //Save Sent Status to Preference..
        //savePreferences("SENT", old);
        // register the alarm broadcast here
        AMCount = amcount;
        registerReceiver(mReceiver, new IntentFilter("com.rememberme.rm") );
        //pendingIntent = PendingIntent.getBroadcast( this, 0, new Intent("com.techblogon.alarmexample"),0 );

        pendingIntent[amcount] = PendingIntent.getBroadcast( RememberMe.this,amcount, new Intent("com.rememberme.rm"),PendingIntent.FLAG_ONE_SHOT );
        alarmManager[amcount] = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
        alarmManager[amcount].set( AlarmManager.RTC_WAKEUP, delay , pendingIntent[amcount] );
    }
    catch(Exception e)
    {
        //Toast.makeText(RememberMe.this, e.toString(), Toast.LENGTH_LONG).show();
        Toast.makeText(RememberMe.this, "Alarm cannot be set...", Toast.LENGTH_LONG).show();
    }
}
这是我的广播接收器

私有无效注册表ArmBroadcast() {

//这是回调函数(BroadcastReceiver),当您的
//将达到报警时间。
mReceiver=新广播接收器()
{
私有静态最终字符串标记=“报警示例接收器”;
@凌驾
公共void onReceive(上下文、意图)
{
字符串s=intent.getAction().toString();
if(intent.getAction()!=null)
{
Log.i(标记,“BroadcastReceiver::OnReceive()>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
//Toast.makeText(上下文,“恭喜!您的报警时间已到”,Toast.LENGTH_LONG.show();
MessageInfo temp=LoadMSGINFOPreference();
if(temp==null)
{
}
其他的
{
字符串snt=LoadSavedReferences(“已发送”);
如果(snt==“”)
发送=0;
其他的
Sent=整数。valueOf(snt);
int status=0;
如果(发送==0)
{
//MessageInfo SentInfo=dbHandler.Get_MessageInfo(已发送);
//status=SentInfo.getStatus();
//}
//Toast.makeText(RememberMe.this,String.valueOf(msgType),Toast.LENGTH_LONG.show();
//如果(状态==0)
//{
if(temp.getMSGType()==1)
{
_Recipients=ReadPhoneNumbers(temp.getRecipients());
_Message=temp.getMessage();
用于(字符串编号:\个收件人)
{
尝试
{
字符串num=数字;
//Toast.makeText(上下文“恭喜!”。您的报警时间已到:“+num+”:”,Toast.LENGTH_LONG)。show();
sendSMS(num,_消息);
} 
捕获(例外e)
{
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(),“SMS失败,请稍后再试!”,Toast.LENGTH\u LONG.show();
e、 printStackTrace();
}                                     
}
alarmManager[AMCount]。取消(挂起内容[AMCount]);
}
//更新te记录以设置状态=1…(发送)
温度设定状态(1);
dbHandler.Update_MessageInfo(temp);
savePreferences(“已发送”,String.valueOf(temp.getID());
//从da DB获取最近的记录。
MessageInfo minfo=newmessageinfo();
ArrayList msgInfo_array_from_db=dbHandler.Get_MessageInfos();
if(msgInfo_array_from_db.size()>0)
{
minfo=GetLatestMSGInfo(msgInfo\u数组\u来自\u db);
//将tat记录设置为报警管理器。。。
SetAlarm(minfo、minfo.getDelay()、temp.getID());
}
//}
}
}
};
}
私有void SaveMSGINFOPreference()
{
SharedReferences sp=PreferenceManager.GetDefaultSharedReferences(此);
编辑器prefsEditor=sp.edit();
Gson Gson=新的Gson();
字符串json=gson.toJson(AlarmMSGInfo);
putString(“AlarmMSGInfo”,json);
提交();
}

在触发第一条短信后15-20分钟内是否会重复此操作。?Vaibs!是。它会发生。如果发送的短信数为3,则下次它会发送4条短信。是否会重复调用您的setAlarm方法。?是。setAlarm metod会从RegisterAlarmBroadcast()重复调用方法。因为一旦短消息发送,下一个警报应该从此设置。其他明智的做法是没有地方获取下一条短消息。我也发布了我的SaveMSGINFOPreference()。请帮助我摆脱这一困境。
    //This is the call back function(BroadcastReceiver) which will be call when your 
    //alarm time will reached.
    mReceiver = new BroadcastReceiver()
    {

        private static final String TAG = "Alarm Example Receiver";

        @Override
        public void onReceive(Context context, Intent intent)
        {

                String s = intent.getAction().toString();
                if(intent.getAction() != null )
                {
                Log.i(TAG,"BroadcastReceiver::OnReceive() >>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                //Toast.makeText(context, "Congrats!. Your Alarm time has been reached", Toast.LENGTH_LONG).show();


                MessageInfo temp= LoadMSGINFOPreference();

                if(temp == null )
                {
                }
                else
                {
                    String snt = loadSavedPreferences("SENT");
                    if(snt== "")
                        Sent = 0;
                    else
                        Sent = Integer.valueOf(snt);

                    int status = 0;
                    if(Sent == 0)
                    {
                        //MessageInfo SentInfo = dbHandler.Get_MessageInfo(Sent);
                        //status = SentInfo.getStatus();
                        //}
                        //Toast.makeText(RememberMe.this, String.valueOf(msgType) , Toast.LENGTH_LONG).show();
                        //if(status == 0) 
                        //{
                            if(temp.getMSGType() == 1)
                            {
                                _Recipients = ReadPhoneNumbers(temp.getRecipients());
                                _Message = temp.getMessage();
                                for(String number:_Recipients)
                                {
                                    try 
                                    {
                                        String num = number;
                                        //Toast.makeText(context, "Congrats!. Your Alarm time has been reached : " + num + ":", Toast.LENGTH_LONG).show();
                                         sendSMS(num, _Message); 
                                      } 
                                    catch (Exception e) 
                                    {
                                        Toast.makeText(getApplicationContext(), e.toString(),Toast.LENGTH_LONG).show();
                                        //Toast.makeText(getApplicationContext(),"SMS faild, please try again later!",Toast.LENGTH_LONG).show();
                                        e.printStackTrace();
                                      }                                     
                                }
                                alarmManager[AMCount].cancel(pendingIntent[AMCount]);
                            }

                            //Updaate te record to set status = 1...(SEND)
                            temp.setStatus(1);
                            dbHandler.Update_MessageInfo(temp);
                            savePreferences("SENT", String.valueOf(temp.getID()));
                            //take nearest Record from da DB.
                            MessageInfo minfo = new MessageInfo();
                            ArrayList<MessageInfo> msgInfo_array_from_db = dbHandler.Get_MessageInfos();
                            if(msgInfo_array_from_db.size() > 0)
                            {
                                minfo = GetLatestMSGInfo(msgInfo_array_from_db);
                                //Set tat record to alarm manager...
                                SetAlarm(minfo, minfo.getDelay(), temp.getID());
                            }
                        //}

                    }
        }

    };
}
private void SaveMSGINFOPreference()
{
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    Editor prefsEditor = sp.edit();
     Gson gson = new Gson();
     String json = gson.toJson(AlarmMSGInfo);
     prefsEditor.putString("AlarmMSGInfo", json);
     prefsEditor.commit();
}