我有这段代码...
desc.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
alert.setTitle("Download");
alert.setIcon(R.drawable.go_down_3_32x32);
alert.setMessage("¿Donwload?");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
asyncForm2 af2 = new asyncForm2();
af2.execute(idU, mDbH);
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert0 = alert.create();
alert0.show();
}
});
其中 desc 是 ImageView 类型的变量,alert 是 AlertDialog.Builder 类型的变量,idU 是一个整数,mDbH 是 DataBaseHelper 的一个实例(我的类来处理 SQLite 数据库)。然后我有后台 AsyncTask ......
class asyncForm2 extends AsyncTask<Object,String,String>{
int idU;
DataBaseHelper db;
downloadData dd;
protected void onPreExecute(){
pDialog2 = new ProgressDialog(screen2.this);
pDialog2.setMessage("Loading...");
pDialog2.setIndeterminate(false);
pDialog2.setCancelable(true);
pDialog2.show();
}
protected String doInBackground(Object... params) {
idU = Integer.parseInt(params[0].toString());
db = (DataBaseHelper) params[1];
dd = new downloadData(idU,db);
if(dd.download(idU) == true){
return "ok";
}else{
return "err";
}
}
protected void onPostExecute(String result){
if(result == "ok"){
pDialog2.dismiss();
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
setAlarms();
Toast.makeText(getApplicationContext(), "Alarms set!!", Toast.LENGTH_SHORT).show();
}else{
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(200);
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
}
这样,与服务器的连接就设置好了,我的应用程序工作所需的所有数据都被下载了……然后在方法 onPostExcecute 中调用方法 setAlarms 并执行以下操作:
private void setAlarms(){
// TODO Auto-generated method stub
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
intent.putExtra("ID_USER", idU);
String name1,name2;
String splits[];
int nameColumnIndex1, nameColumnIndex2;
mDbH.open();
Cursor itemCursor = null;
itemCursor = mDbH.getForms(idU);
if(itemCursor.moveToFirst() == false){
itemCursor.close();
mDbH.close();
}else{
for(itemCursor.moveToFirst();!itemCursor.isAfterLast();itemCursor.moveToNext()){
PendingIntent sender = PendingIntent.getBroadcast(screen.this, 0, intent, 0);
Calendar cal = GregorianCalendar.getInstance();
nameColumnIndex1 = itemCursor.getColumnIndexOrThrow(DataBaseHelper.FORM_SYNC_DURATION);
nameColumnIndex2 = itemCursor.getColumnIndexOrThrow(DataBaseHelper.FORM_SYNC_HOUR);
name1 = itemCursor.getString(nameColumnIndex1);
name2 = itemCursor.getString(nameColumnIndex2);
splits = name2.split(":");
if(name1.equals("DAILY")){
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splits[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(splits[1]));
cal.set(Calendar.SECOND, Integer.parseInt(splits[2]));
}
if(name1.equals("WEEKLY")){
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splits[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(splits[1]));
cal.set(Calendar.SECOND, Integer.parseInt(splits[2]));
if(cal.get(Calendar.DAY_OF_MONTH)+7 > cal.getActualMaximum(Calendar.DAY_OF_MONTH)){
cal.set(Calendar.DAY_OF_MONTH, (cal.get(Calendar.DAY_OF_MONTH)+7) - cal.getActualMaximum(Calendar.DAY_OF_MONTH));
if(cal.get(Calendar.MONTH) + 1 >= cal.getActualMaximum(Calendar.MONTH)+1){
cal.set(Calendar.MONTH, (cal.get(Calendar.MONTH) + 1) - (cal.getActualMaximum(Calendar.MONTH)+1));
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 1);
}else{
cal.set(Calendar.MONTH,cal.get(Calendar.MONTH)+1);
}
}else{
cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)+7);
cal.set(Calendar.MONTH,cal.get(Calendar.MONTH)+1);
}
}
if(name1.equals("FORTNIGHTLY")){
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splits[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(splits[1]));
cal.set(Calendar.SECOND, Integer.parseInt(splits[2]));
if(cal.get(Calendar.DAY_OF_MONTH)+15 > cal.getActualMaximum(Calendar.DAY_OF_MONTH)){
cal.set(Calendar.DAY_OF_MONTH, (cal.get(Calendar.DAY_OF_MONTH)+15) - cal.getActualMaximum(Calendar.DAY_OF_MONTH));
if(cal.get(Calendar.MONTH) + 1 > cal.getActualMaximum(Calendar.MONTH)+1){
cal.set(Calendar.MONTH, (cal.get(Calendar.MONTH) + 1) - (cal.getActualMaximum(Calendar.MONTH)+1));
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 1);
}else{
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
}
}else{
cal.set(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH+15);
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
}
}
if(name1.equals("MONTHLY")){
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splits[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(splits[1]));
cal.set(Calendar.SECOND, Integer.parseInt(splits[2]));
if(cal.get(Calendar.DAY_OF_MONTH) + cal.getActualMaximum(Calendar.DAY_OF_MONTH) > cal.getActualMaximum(Calendar.DAY_OF_MONTH)){
cal.set(Calendar.DAY_OF_MONTH, (cal.get(Calendar.DAY_OF_MONTH) + cal.getActualMaximum(Calendar.DAY_OF_MONTH)) - cal.getActualMaximum(Calendar.DAY_OF_MONTH));
if(cal.get(Calendar.MONTH) + 1 > cal.getActualMaximum(Calendar.MONTH)+1){
cal.set(Calendar.MONTH, (cal.get(Calendar.MONTH) + 1) - (cal.getActualMaximum(Calendar.MONTH)+1));
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 1);
}else{
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
}
}
}
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
itemCursor.close();
mDbH.cerrar();
}
}
如您所见,我根据数据库表中的值设置了几个警报,并且关于此,警报彼此不同(每天、每周等)。我的 AlarmReceiver 类如下:
public class AlarmReceiver extends BroadcastReceiver {
private ProgressDialog pDialog1;
private static DataBaseHelper mDbH;
Context context0;
@Override
public void onReceive(Context context, Intent intent) {
try {
context0 = context;
Bundle bundle = intent.getExtras();
int idU = bundle.getInt("ID_USER");
asyncForm af2 = new asyncForm();
af2.execute(idU, mDbH);
} catch (Exception e) {
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
class asyncForm extends AsyncTask<Object,String,String>{
int idU;
DataBaseHelper db;
sendData sd;
protected void onPreExecute(){
pDialog1 = new ProgressDialog(contexto);
pDialog1.setMessage("Sending...");
pDialog1.setIndeterminate(false);
pDialog1.setCancelable(true);
pDialog1.show();
}
@Override
protected String doInBackground(Object... params) {
idU = Integer.parseInt(params[0].toString());
db = (DataBaseHelper) params[1];
sd = new sendData(idU,mDbH);
if(sd.send(idU) == true){
return "ok";
}else{
return "err";
}
}
protected void onPostExecute(String result){
if(result == "ok"){
pDialog1.dismiss();
Toast.makeText(contexto, "Success", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(contexto, "Error", Toast.LENGTH_SHORT).show();
}
}
}
}
正如你所看到的,在整个过程中,我尝试将数据自动发送到我的服务器(存储在数据库中的表单的响应)。但是由于某种原因,当时间到来时(比如说中午 12:00),什么也没有发生。logcat没有错误,这就是为什么我不知道我做错了什么!请帮忙,我不知道问题是在使用日历时还是实际上不是设置警报的方式......
问候!