我想创建提醒
我已经创建了一个数据库。数据库存储日期和时间。到这里一切正常。现在,我想asynctask
在后台运行一个,每隔几秒钟检查一次是否有新的提醒,如果是,然后使用生成的意图调用另一个活动notificaton
。此外,为了签入数据库,它正在调用不同的活动。我创建了一个无限的 while 循环,以便它继续检查。
这是代码。这里我将当前日期和时间与存储在数据库中的日期日期和时间进行比较。比较的完整逻辑写在提醒活动中。创建通知是一个活动数据,具有创建通知的逻辑
public class Async_task extends AsyncTask<Void, Void, ArrayList<String>>{
MainActivity async_task_context = null;
public Async_task(MainActivity context) {
async_task_context = context;
// TODO Auto-generated constructor stub
}
@Override
protected ArrayList<String> doInBackground(Void... params) {
// TODO Auto-generated method stub
Log.d("in async task", "msg");
while(1<2){
Calendar cal = Calendar.getInstance();
int current_day = cal.get(Calendar.DAY_OF_MONTH);
Log.d("in do in background", "");
int cuurent_month = cal.get(Calendar.MONTH);
int current_year = cal.get(Calendar.YEAR);
int curent_hour = cal.get(Calendar.HOUR_OF_DAY);
int current_minute = cal.get(Calendar.MINUTE);
String date = current_day+"/"+cuurent_month+"/"+current_year;
String time = curent_hour+":"+current_minute;
ArrayList<String> arr = new ArrayList<String>();
Reminder fetch_info = new Reminder(async_task_context);
fetch_info.open();
arr = fetch_info.fetch(date,time);
fetch_info.close();
Log.d("arr in oncreate", ""+arr);
int size = arr.size();
if(size !=0){
Intent in = new Intent(async_task_context,Create_notification.class);
in.putExtra("information", arr);
try {
Thread.sleep(9000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} return arr;
}
在运行时logcat
我遇到了以下错误
08-30 18:13:18.701: E/AndroidRuntime(797): java.lang.RuntimeException: An error occured while executing doInBackground()
08-30 18:13:18.701: E/AndroidRuntime(797): at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-30 18:13:18.701: E/AndroidRuntime(797): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
08-30 18:13:18.701: E/AndroidRuntime(797): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-30 18:13:18.701: E/AndroidRuntime(797): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-30 18:13:18.701: E/AndroidRuntime(797): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-30 18:13:18.701: E/AndroidRuntime(797): at android.os.Handler.<init>(Handler.java:197)
08-30 18:13:18.701: E/AndroidRuntime(797): at com.Itspark.smartreminder.Reminder.<init>(Reminder.java:74)
08-30 18:13:18.701: E/AndroidRuntime(797): at com.Itspark.smartreminder.Async_task.doInBackground(Async_task.java:36)
下面是提醒类。提醒类正在创建数据库,它有很多方法。
public class Reminder extends Activity{
public static final String Column_Id="_Id";
private static final String name = "Event_Name";
private static final String date = "Event_date";
private static final String time = "Event_time";
private static final String location = "Event_location";
private static final String Table_Name="Data_container";
private static final String Database_Name="Data_Ship";
private static final int Version=1;
private Context ourcontext;
private SQLiteDatabase ourSQLiteDatabase;
private DbHelper ourHelper;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, Database_Name, null, Version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("in on create", "before");
db.execSQL("CREATE TABLE "+ Table_Name + "(" +Column_Id+" INTEGER PRIMARY KEY AUTOINCREMENT, " + name +" TEXT NOT NULL, "
+ date +" TEXT NOT NULL, "+ time +" TEXT NOT NULL, "+ location +" TEXT );");
Log.d("in on create", "");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
db.execSQL("DROP TABLE IF EXISTS "+Table_Name);
onCreate(db);
}
}
public Reminder(Context c) { //**This is line 74**
ourcontext = c;
}
public Reminder open(){
ourHelper = new DbHelper(ourcontext);
ourSQLiteDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public ArrayList<String> fetch(String date2, String time2) {
// TODO Auto-generated method stub
String anArray[] = new String[] {Column_Id,name,date,time,location};
Cursor cu = ourSQLiteDatabase.query(Table_Name, anArray, date+" =?"+" AND "+time+" =?", new String[] {date2,time2}, null, null, null);
Log.d("after cursor","ere");
int name1=cu.getColumnIndex(name);
int date1 = cu.getColumnIndex(date);
int time1 = cu.getColumnIndex(time);
int location1 = cu.getColumnIndex(location);
ArrayList<String> arr = new ArrayList<String>();
for(cu.moveToFirst();!cu.isAfterLast();cu.moveToNext()){
arr.add("Name "+cu.getString(name1)+ "\nDate "+cu.getString(date1)+"\nTime "+cu.getString(time1)+"\nLocation "+cu.getString(location1));
}
return arr;
}