我想创建一个提醒
我创建了一个数据库。数据库存储日期和时间。一切都在这里。
现在,我想在后台运行一个asynctask
,如果有新提醒,则每隔几秒检查一次,然后使用生成notificaton
的意图调用另一个活动。同样,为了检入数据库,它正在调用另一个活动。我创建了一个无限循环,以便继续检查。
这里是代码。在此我将当前日期和时间与存储在数据库中的日期日期和时间进行比较。完整的比较逻辑写在提醒活动中.Create_notification是一个活动数据具有创建通知的逻辑
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)
下面是提醒类..Reminder类正在创建数据库,它有很多方法。
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;
}
答案 0 :(得分:0)
将Thread.sleep
放入:
activity.runOnUiThread(new Runnable(){
public void run(){
Thread.sleep();
}
});
答案 1 :(得分:0)
你不应该睡在doInBackground()
。相反,您应该将此代码移至OnPostExecute
或者您可以使用runOnUiThread
其他答案建议的内容。
答案 2 :(得分:0)
U无法从doinbackground方法访问UI线程。在同步任务中需要post execute方法来执行主UI线程上的任何任务。你可以将数组列表传递给post执行,并可以从那里执行其余的工作