サーバーからのデータを表示するアプリケーションがあります。また、サーバーからデータを定期的に取得して (休止時間は異なりますが) キャッシュして、場合によっては通知を送信する必要もあります。
通知は非常に重要な場合があるため、サービスを使用していて何らかの理由で強制終了され、キャッシュ時間がスキップされる状況は表示されません(または、それが不可能な場合は、スキップされた時間を最小限に抑える必要があります)。このキャッシング プロセスは、システムの起動後に(アプリケーションが実行中かどうかに関係なく)開始し、デバイスの電源が入っているときに常に実行する必要があります。
では、どのようなアプローチを使用する必要がありますか?
質問する
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 に答える