A有一个显示服务器数据的应用程序。我还需要定期从服务器获取数据(不同的暂停,但是)来缓存它,并在某些情况下发送通知。
通知可以非常重要,这样的情况时,我使用的服务,它就会被杀死因为某些原因和缓存时间将被跳过不应该出现(或者,如果它是不可能的,我应该尽量减少跳过时间)。此缓存过程应在系统引导(无论应用程序是否正在运行)后开始,并在设备开启时始终执行。
那么我应该使用什么方法?选择定期与服务器同步数据的方法android
0
A
回答
0
在你的情况下,最合适的是使用Service
不断更新数据源中的数据。至于它自己的数据,我认为在你的情况下,你最好创建一个SQLite
数据库,以便在系统重启时保留你的数据。
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;
}
}
相关问题
- 1. 同步Android数据库与服务器数据库
- 2. 同步本地数据库(Sqlite)与服务器数据库android
- 3. 同步数据库与服务器
- 4. 与服务器同步数据
- 5. 与服务器同步GIT
- 6. 同步PouchDB与服务器
- 7. 同步android应用程序与Web服务器数据库的数据库
- 8. 同步本地SQLite数据库与服务器SQLite数据库
- 9. 同步本地数据库windows phone与数据库服务器
- 10. Xamarin与服务器形成本地数据库数据同步
- 11. 与服务器同步多个客户端的最佳方法
- 12. 将Android手机与第三方服务器同步
- 13. 同步插入数据库并选择Web服务
- 14. WCF服务方法同步/异步
- 15. Sql服务器数据库同步
- 16. Android AccountAuthenticator和同步服务:显示在“同步和数据”下
- 17. 从设备sqlite到服务器的Android数据同步
- 18. 通过REST API处理Android服务和服务器之间的数据同步
- 19. SQL服务器选择行与数
- 20. 同步客户端数据库与服务器数据库与筛选的数据
- 21. 与FTP服务器的文件同步
- 22. 如何在android应用程序中同步服务器数据?
- 23. 从服务器和Android设备同步数据
- 24. 如何将数据库与微服务(和新服务)同步?
- 25. 用java同步两台不同服务器的方法
- 26. 当应用程序关闭时与服务器同步数据
- 27. 与服务器脱机数据库同步
- 28. 领域数据库:与服务器同步
- 29. 与网络服务器同步核心数据
- 30. 与网络服务器同步设备数据
我想过这个问题,但是服务有时会被杀死。警报管理器或其他方法是否更可靠? – Natalia 2013-04-08 11:45:36
您可以使用警报管理器在操作系统杀死它时再次启动服务,但应使用服务AFAIK完成数据同步。 – 2013-04-08 12:24:10