I want to create a reminder
I have created a database. Database stores date and time. upto here everything is working.
Now, I want to run an asynctask
in background which checks after every few seconds if there is new reminder if yes then call to another activity using intent that generate notificaton
. Also for checking in to database it is calling a different activity. I have created an infinite while loop so that it keeps on checking.
here is code.In this I am comparing current date and time with the date date and time stored in database.The complete logic of comparison is written in reminder activity.Create_notification is an Activity data has the logic of creating 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;
}
On running logcat
I came across the following errors
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)
Below is reminder class.Reminder class is creating database and it has many methode.
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;
}
Place Thread.sleep
in:
activity.runOnUiThread(new Runnable(){
public void run(){
Thread.sleep();
}
});