A 有一个显示来自服务器的一些数据的应用程序。我还需要定期从服务器获取数据(尽管有不同的暂停)来缓存它,并且在某些情况下发送通知。
通知可能非常重要,所以当我使用服务并且由于某些原因它被杀死并且缓存时间被跳过时的情况不应该出现(或者,如果不可能,我应该尽量减少跳过的时间)。此缓存过程应在系统启动后启动(无论应用程序是否正在运行),并始终在设备打开时执行。
那么我应该使用什么方法呢?
问问题
963 次
2 回答
0
我用过这样的服务
这可能会帮助你
public class MyService extends Service {
int counter = 0;
static final int UPDATE_INTERVAL = 60 * 1000; // / 1000 = 1 second
private Timer timer = new Timer();
JsonParsers jsonParser = new JsonParsers();
private static final String TAG_SUCCESS = "success";
private static String url_create_product = "http://ayyappagold.com/ayyappa/index.php";
@Override
public IBinder onBind(Intent intent) {
// Not implemented...this sample is only for starting and stopping
// services.
// Service binding will be covered in another tutorial
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Announcement about starting
Toast.makeText(this, "Starting the Service", Toast.LENGTH_SHORT).show();
// Start a Background thread
doSomethingRepeatedly();
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
private void doSomethingRepeatedly() {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
doIn();
Calendar c = Calendar.getInstance();
String month = String.valueOf(c.get(Calendar.MONTH) + 1);
String year = String.valueOf(c.get(Calendar.YEAR));
String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
String months = null;
if (month.startsWith("0") || month.startsWith("1")) {
if (day.startsWith("0") || day.startsWith("1")
|| day.startsWith("2") || day.startsWith("3")) {
months = year + "-" + month + "-" + day;
}
} else {
months = year + "-" + "0" + month + "-0" + day;
}
loadJewelDetails(months);
}
}, 0, UPDATE_INTERVAL);
}
@Override
public void onDestroy() {
super.onDestroy();
if (timer != null) {
timer.cancel();
}
Toast.makeText(this, "Stopping the Service", Toast.LENGTH_SHORT).show();
}
public void set_alarm(int year, int month, int day, String title,
String text, String billno) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
Intent intent = new Intent(getApplicationContext(), AlarmActivity.class);
intent.putExtra("title", title);
intent.putExtra("text", text);
intent.putExtra("billno", billno);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1234, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
@SuppressWarnings("static-access")
AlarmManager alarmManager = (AlarmManager) getApplicationContext()
.getSystemService(getApplicationContext().ALARM_SERVICE);
alarmManager.cancel(pendingIntent); // cancel any existing alarms
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
Database db = new Database(getApplicationContext());
db.insertFlg(1, billno);
}
public void loadJewelDetails(String month) {
try {
Cursor cursor = null;
Database db = new Database(getApplicationContext());
cursor = db.getGoldPush(month);
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
do {
String billno = cursor.getString(cursor
.getColumnIndex("id"));
String title = "Ayyappa Gold";
String name = cursor.getString(cursor
.getColumnIndex("items"));
String dates = cursor.getString(cursor
.getColumnIndex("date"));
String yr = dates.substring(0, 4);
int year = Integer.parseInt(yr);
String mon = dates.substring(5);
String mo = mon.substring(0, 2);
int months = Integer.parseInt(mo);
String da = dates.substring(9);
int day = Integer.parseInt(da);
String tex = name.replace("*", "\n");
String text = tex;
// Ask our service to set an alarm for that date,
// this
// activity talks to the client that talks to the
// service
int flg = getFlag(billno);
if (flg == 0) {
set_alarm(year, months, day, title, text, billno);
}
System.out.println(dates);
} while (cursor.moveToNext());
}
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
// looping through All Contacts
}
public int getFlag(String bill) {
int flag = 0;
try {
Cursor cursor = null;
Database db = new Database(getApplicationContext());
cursor = db.getFlag(bill);
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
do {
flag = Integer.parseInt(cursor.getString(cursor
.getColumnIndex("flag")));
} while (cursor.moveToNext());
}
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
// looping through All Contacts
return flag;
}
public void doIn() {
// Building Parameters
int id = getId();
String name = String.valueOf(id);
Database db = new Database(getApplicationContext());
System.out.println("read the Gold and Silver rate details");
JSONArray jsonarr = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("start", name));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Getting Array of Contacts
jsonarr = json.getJSONArray("ayyappagolddetails");
for (int i = 0; i < jsonarr.length(); i++) {
JSONObject c = jsonarr.getJSONObject(i);
String billno = c.getString("id");
String item = c.getString("items");
String date = c.getString("date");
String time = c.getString("time");
db.addGoldItemDetails(billno, item, date, time);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public int getId() {
int id = 0;
try {
System.out.println("read the id value");
Cursor cursor = null;
Database db = new Database(getApplicationContext());
cursor = db.getId();
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
do {
id = Integer.parseInt(cursor.getString(cursor
.getColumnIndex("id")));
} while (cursor.moveToNext());
}
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
// looping through All Contacts
System.out.println("last id value is" + id);
return id;
}
}
于 2013-04-08T11:48:22.120 回答
0
在您的情况下,最合适的方法是使用Service
for 不断更新数据源中的数据。至于数据本身,我认为在您的情况下,您最好创建一个SQLite
数据库,以便在系统重新启动时保留您的数据。
于 2013-04-08T11:40:49.353 回答