0

我正在开发一个短信调度程序应用程序。在这里,用户可以设置时间、号码和消息。只要我需要安排一条消息,我的代码就可以正常工作。但是,如果我想有多个时间表,那是不可能的,因为新的会取代旧的。

正如我阅读的其他一些帖子所建议的那样,我正在使用的技术是创建一组具有不同请求代码的待处理意图,但是新的时间表取代了旧的。

下面是我的代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zax_sms_scheduler);

        Button dateSet=(Button) findViewById(R.id.dateSetBtn);
        Button timeSet=(Button) findViewById(R.id.timeSetBtn);

        dateSet.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthodyear, int dayofmonth) {
                        // TODO Auto-generated method stub
                        mMonth=monthodyear;
                        mYear=year;
                        mDay=dayofmonth;
                        Toast.makeText(getBaseContext(), "Date Set is :"+mDay+"/"+(mMonth+1)+"/"+mYear, Toast.LENGTH_SHORT).show();
                    }
                };
                // TODO Auto-generated method stub
                new DatePickerDialog(ZaxSmsScheduler.this,d,Calendar.YEAR,Calendar.MONTH,Calendar.DAY_OF_MONTH).show();
            }
        });

        timeSet.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view2, int hour, int min) {
                        // TODO Auto-generated method stub
                        mHour=hour;
                        mMin=min;

                        if(mHour>=12)
                            mHour=mHour-12;

                        Log.d("MYAPP", "hh:"+mHour+"\nmm:"+mMin);
                        Toast.makeText(getBaseContext(), "Time Set is:"+mHour+":"+mMin+":00", Toast.LENGTH_SHORT).show();
                    }
                };
                Calendar cal=Calendar.getInstance();
                new TimePickerDialog(ZaxSmsScheduler.this,t,cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),true).show();

            }
        });

        Button saveAndClearBtn=(Button) findViewById(R.id.saveBtn);
        saveAndClearBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Calendar myCal=Calendar.getInstance();
                long timeToTrigger;
                /*myCal.set(Calendar.DAY_OF_MONTH, mDay);
                myCal.set(Calendar.MONTH,mMonth);
                myCal.set(Calendar.YEAR, mYear);*/

                myCal.set(Calendar.HOUR, mHour);
                myCal.set(Calendar.MINUTE, mMin);
                myCal.set(Calendar.SECOND, 0);

                long setTime=myCal.getTimeInMillis();

                if(setTime>System.currentTimeMillis())
                {

                    timeToTrigger=setTime-System.currentTimeMillis();
                    EditText edt1=(EditText) findViewById(R.id.editText1);
                    EditText edt2=(EditText) findViewById(R.id.editText2);
                    msg=edt2.getText().toString();
                    telno=edt1.getText().toString();

                    SQLiteDatabase db=openOrCreateDatabase("MYDB", MODE_PRIVATE, null);
                    db.execSQL("CREATE TABLE IF NOT EXISTS mySmsScheduler(SlNo VARCHAR,Number VARCHAR,Msg VARCHAR);");

                    String s="INSERT INTO mySmsScheduler VALUES ('"+count+"','"+telno+"','"+msg+"');";
                    db.execSQL(s);
                    Log.d("MYREC", "Insertion of data successfull");
                    db.close();

                    edt1.setText("");
                    edt2.setText("");

                     Intent intent = new Intent();
                     intent.setClass(ZaxSmsScheduler.this, MyBroadcastReceiver.class);
                     Bundle b = new Bundle();
                     b.putString("index", Integer.toString(count));
                     intent.putExtras(b);

                     PendingIntent pendingIntent = PendingIntent.getBroadcast(ZaxSmsScheduler.this, (int)Math.random(), intent, 0);
                     intentArray.add(pendingIntent);
                     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                     alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                            + timeToTrigger, intentArray.get(count));
                     count++;
                    Toast.makeText(getBaseContext(), "Sms Scheduled after:"+(timeToTrigger/1000)+" sec.", Toast.LENGTH_SHORT).show();

                    Log.d("MYAPP", "Set Time:"+(setTime/1000)+"Sec. \n"+"Cur time:"+System.currentTimeMillis()/1000);
                    Log.d("MYAPP", "Time to trigger:"+(timeToTrigger/1000)+"sec.");
                }
                else
                {
                    Toast.makeText(getBaseContext(), "Please Enter a valid time", Toast.LENGTH_SHORT).show();
                    Calendar calendar =Calendar.getInstance();
                    int h=calendar.get(Calendar.HOUR);
                    int m=calendar.get(Calendar.MINUTE);

                    Log.d("MYAPP", "cur HH:"+h+"\ncur MM:"+m);
                }

            }
        });
    }

我的广播接收器的代码是:

@Override
public void onReceive(Context arg0, Intent intent) {
    // TODO Auto-generated method stub

    int myCount;
    String cnt=intent.getStringExtra("index");
    if(cnt==null)
        Log.d("MYAPP","Data not received");
    Log.d("MYAPP", "Count:"+cnt);
    myCount=Integer.parseInt(cnt);

    Log.d("MYAPP","Broadcast receiver called...");
    SQLiteDatabase db=arg0.openOrCreateDatabase("MYDB",Context.MODE_PRIVATE, null);

    Cursor c=db.rawQuery("SELECT Number, Msg FROM mySmsScheduler WHERE SlNo=="+myCount, null);
    Log.d("MYAPP", "Cursor reference obtained...");
    if(c!=null)
    {
        c.moveToFirst();
    }
    else
        Log.d("MYAPP", "cursor is null");
   /* c.moveToFirst();*/
    String num=c.getString(c.getColumnIndex("Number"));
    String myMsg=c.getString(c.getColumnIndex("Msg"));

    Log.d("MYAPP", "number:"+num+"\nMsg:"+myMsg);
    SmsManager sm = SmsManager.getDefault();
    sm.sendTextMessage(num, null, myMsg, null, null);
    Log.d("MYAPP", "Message Sent");
    Toast.makeText(arg0, "Msg sent successfully", Toast.LENGTH_LONG).show();
    String table="mySmsScheduler";
    String whereClause = "SlNo = ?";
    String[] whereArgs = new String[] { Integer.toString(ZaxSmsScheduler.count) };
    db.delete(table, whereClause, whereArgs);
    db.close();
    Log.d("MYAPP", "Entry deleted..");
    //sm.sendTextMessage(ZaxSmsScheduler.telno, null, ZaxSmsScheduler.msg, null, null);

}

我已经在我的清单中设置了所需的权限并注册了我的广播接收器。我恳请您提供宝贵的建议来解决同样的问题。

提前致谢。

4

1 回答 1

1

对多个警报使用不同的通知 ID

@Override
    public void onReceive(Context c, Intent i) {

        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();

        Bundle bundle = i.getExtras();
        String title = bundle.getString("title");
        String text = bundle.getString("text");
        String billno = bundle.getString("billno");
        int id = bundle.getInt("id");
        Toast.makeText(c, text, Toast.LENGTH_LONG).show();
        final int NOTIF_ID = id;
        // final int NOTIF_ID = 1;
        NotificationManager notofManager = (NotificationManager) c
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(c, AyyappaGoldActivity.class);
        //notificationIntent.putExtra("billno", billno);
        PendingIntent contentIntent = PendingIntent.getActivity(c, 0,
                notificationIntent, 0);

        Notification notification = new Notification(icon, title, when);

        notification.setLatestEventInfo(c, title, text, contentIntent);

        //notification.flags = Notification.FLAG_INSISTENT;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notofManager.notify(NOTIF_ID, notification);
    }

}

setAlarm 的代码;

int NOTIF_ID = (int) cal.getTimeInMillis();
        NOTIF_ID++;
        Intent intent = new Intent(getApplicationContext(), AlarmActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("text", text);
        intent.putExtra("billno", billno);
        intent.putExtra("id", NOTIF_ID);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getApplicationContext(), NOTIF_ID, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        @SuppressWarnings("static-access")
        AlarmManager alarmManager = (AlarmManager) getApplicationContext()
                .getSystemService(getApplicationContext().ALARM_SERVICE);

        alarmManager.cancel(pendingIntent); // cancel any existing alarms

        alarmManager.set(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(), pendingIntent);
于 2013-05-02T12:24:05.393 回答