gpt4 book ai didi

android - 弄清楚警报管理器

转载 作者:行者123 更新时间:2023-11-30 04:33:54 26 4
gpt4 key购买 nike

我正在尝试创建一个应用程序,它会在 x 分钟后将一些数据发送到网络服务器。有人告诉我,警报管理器将是最好的解决方案,只需在警报内额外 x 分钟调用相同的警报功能,这样即使在后台,它也会不断发送数据。

但是,我只在项目中看到了打开 Intent 的警报。这是否意味着它会在 x 分钟后切换到新 Intent ,或者一切都会在后台运行?

应该发送后台数据而不必在 Intent 之间切换,所以我更希望它只在我的一个 Activity 中调用一个函数。我应该怎么做?

这是我想每 x 分钟调用一次的函数。

谢谢

public class updateloc extends AsyncTask<Void, Void, Void> {

protected void onPostExecute(String response) {
if (response != null) {
// check if this does anything later
}
progressDialog.dismiss();
}

@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(ImTracking.this, "",
"Updating Data...");
}

@Override
protected Void doInBackground(Void... arg0) {
SharedPreferences prefs = getSharedPreferences("Settings", 0);
final String id = prefs.getString("ID", "");
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(
"http://iphone-radar.com/gps/gps_locations");

JSONObject holder = new JSONObject();

try {
holder.put("id", id);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = locationManager.getBestProvider(criteria,
false);
LocationListener loc_listener = new LocationListener() {

@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
try {
Looper.prepare();
locationManager.requestLocationUpdates(bestProvider, 0, 0,
loc_listener);
} catch (Exception e) {
e.printStackTrace();
}
Location location = locationManager
.getLastKnownLocation(bestProvider);
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(
"hh:mmaa MM/dd/yyyy");
holder.put("time", sdf.format(c.getTime()));
holder.put("time_since_epoch", System.currentTimeMillis());
try {
holder.put("lat", location.getLatitude());
holder.put("lon", location.getLongitude());
} catch (NullPointerException e) {
try {
holder.put("lat", -1.0);
holder.put("lon", -1.0);
} catch (JSONException e1) {
e1.printStackTrace();
}
}

StringEntity se = new StringEntity(holder.toString());
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");

ResponseHandler responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httpost, responseHandler);
org.json.JSONObject obj;

obj = new org.json.JSONObject(response);

} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
progressDialog.dismiss();
return null;
}
}

最佳答案

如果你想让它在后台运行,你需要创建一个服务。可以使用 Intent 启动服务,因此您可以使用 AlarmManager 启动服务。看看IntentService ,您只需将您当前在 AsyncTask 中所做的事情移动到 servcie 的 handleIntent() 方法中。

关于android - 弄清楚警报管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7225406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com