如何在Android中的后台访问数据库?

时间:2014-01-29 06:47:30

标签: android android-alarms

我正在开发一个应用程序,其中用户可以在db中创建某些任务。我想设置报警管理器按照用户设置的时间执行任务,但主要问题是我没有得到如何为数据库中创建的所有任务设置报警管理器或为多个任务设置报警管理器。请建议我如何满足我的要求。

2 个答案:

答案 0 :(得分:0)

创建不同的计时器任务并根据您的需要执行

CountDownTimer vanishTimer = new CountDownTimer(Timer, tick) {

        @Override
        public void onTick(long millisUntilFinished) {



        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            //your Db operations


        }
    };  

答案 1 :(得分:0)

this way you can use this as u want to get data from sqlite database

package com.location.zealalerts;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Pattern;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.IBinder;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.util.Patterns;

@SuppressWarnings("deprecation")
public class LocationService extends Service {


    Calendar c;
    int mYear, mMonth, mDay, mHour, mMin, mSec;

    ArrayList<String> Tasks = new ArrayList<String>();
    ArrayList<String> Desc = new ArrayList<String>();
    ArrayList<String> Task_date = new ArrayList<String>();
    ArrayList<String> Start_Time = new ArrayList<String>();
    ArrayList<String> Priority = new ArrayList<String>();

    ArrayList<Double> lat_array = new ArrayList<Double>();
    ArrayList<Double> long_array = new ArrayList<Double>();
    ArrayList<Double> radius_array = new ArrayList<Double>();

    ArrayList<Integer> ids = new ArrayList<Integer>();

    ArrayList<Integer> alertid = new ArrayList<Integer>();
    ArrayList<String> type = new ArrayList<String>();
    ArrayList<String> phone_email = new ArrayList<String>();
    ArrayList<String> content = new ArrayList<String>();

    ArrayList<String> temp_Tasks = new ArrayList<String>();
    boolean isOutside[];

    DBAdapter mDbAdapter;
    double distance;
    double longitude,latitude;
    String to;
    String txt_msg;
    @Override
    public void onCreate() {
        Log.i("Background service ", "calls");


        recurscall_time_alert();

    }




    public static double distFrom(double lat1, double lng1, double lat2,
            double lng2) {
        double earthRadius = 3958.75;
        double dLat = Math.toRadians(lat2 - lat1);
        double dLng = Math.toRadians(lng2 - lng1);
        double sindLat = Math.sin(dLat / 2);
        double sindLng = Math.sin(dLng / 2);
        double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
                * Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2));
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double dist = earthRadius * c;

        return dist;
    }

    private void recurscall_time_alert() {
        // TODO Auto-generated method stub
        new Timer().scheduleAtFixedRate(new TimerTask() {

            @SuppressWarnings("deprecation")
            @Override
            public void run() {


                get_latlong();

                get_contacts();
                Log.d("Time Alert", "Call Time Alert");


            }
        }, 7*1000, 180 * 1000);

    }



    void get_latlong() {

        mDbAdapter = new DBAdapter(getApplicationContext());
        mDbAdapter.read_open();

        Cursor cr = mDbAdapter.fetchAllTasks();
        cr.moveToFirst();
        ids.clear();
        Tasks.clear();
        Priority.clear();
        lat_array.clear();
        long_array.clear();
        radius_array.clear();
        Start_Time.clear();
        Task_date.clear();
        Desc.clear();
        while (!cr.isAfterLast()) {

            ids.add(Integer.parseInt(cr.getString(0)));
            Tasks.add(cr.getString(1));
            Priority.add(cr.getString(2));
            Task_date.add(cr.getString(3));
            Start_Time.add(cr.getString(5));
            lat_array.add(Double.valueOf(cr.getString(6)));
            long_array.add(Double.valueOf(cr.getString(7)));
            radius_array.add(Double.parseDouble(cr.getString(8)));
            cr.moveToNext();

        }
        cr.close();

        mDbAdapter.close();
        Log.d("LatLongs::", "Lattitude:" + String.valueOf(lat_array)
                + "long_array:" + String.valueOf(long_array));

        //get_contacts();

    }

    public void get_contacts() {
        mDbAdapter = new DBAdapter(getApplicationContext());
        mDbAdapter.read_open();
        Cursor cr2 = mDbAdapter.fetchAllTaskContacts();
        cr2.moveToFirst();
        alertid.clear();
        phone_email.clear();
        type.clear();
        content.clear();

        while (!cr2.isAfterLast()) {

            alertid.add(Integer.parseInt(cr2.getString(0)));
            phone_email.add(cr2.getString(2));
            type.add(cr2.getString(3));
            content.add(cr2.getString(4));
            cr2.moveToNext();

        }
        cr2.close();
        mDbAdapter.close();
        Log.d("Contacts", String.valueOf(alertid) + phone_email + type
                + content);

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


}