Android:在Alarm Manager中安排SMS

时间:2013-11-24 07:36:40

标签: android

我正在编写应用程序来安排来自警报管理器的短信。它只适用于我一次,第二次它生成多个SMS。

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

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

这是我的编码......请elp me ...我正在为这个时间度过一天......

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

这是我的广播接收器..

private void RegisterAlarmBroadcast()     {

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

0 个答案:

没有答案