我有一个Android应用程序,将每x分钟记录一次数据。闹钟响起的时间取决于设置页面(10或30分钟)。警报进入启动画面时首先激活。我的问题是,当我在手机上使用它时似乎没有被调用,因此我希望有人能告诉我我是否正确使用该功能。
if (prefs.getBoolean("backgroundupdates", true)) {
Intent setAlarm = new Intent(Splash.this,
UpdateLocation.class);
PendingIntent pendingIntent = PendingIntent.getService(
Splash.this, 0, setAlarm, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ssaa");
History.addHistory(sdf.format(calendar.getTime()), "App Started", "");
calendar.setTimeInMillis(System.currentTimeMillis());
int UPDATE_TIME = prefs.getInt("Update_time", 30);
calendar.add(Calendar.MINUTE, UPDATE_TIME);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
}
检查后台更新是否开启并为UPDATE_TIME分钟设置闹钟(默认为30)
我的UpdateLocation.class中的我在底部有
Intent setAlarm = new Intent(UpdateLocation.this,
UpdateLocation.class);
PendingIntent pendingIntent = PendingIntent.getService(
UpdateLocation.this, 0, setAlarm, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int UPDATE_TIME = prefs.getInt("Update_time", 30);
calendar.add(Calendar.MINUTE, UPDATE_TIME);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
这样警报会在UPDATE_TIME分钟内再次调用自己。我这样做的原因是因为UPDATE_TIME可能会改变(取决于用户的偏好)
谢谢!
这里编辑的是我的UpdateLocation.class
public class UpdateLocation extends IntentService {
public static final String id = "";
public UpdateLocation() {
super("UpdateLocation");
}
@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences prefs = getSharedPreferences("Settings", 0);
final String id = prefs.getString("ID", "");
if (prefs.getBoolean("backgroundupdates", true)) {
HttpParams httpParams = new BasicHttpParams();
// 30seconds and it stops
HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
HttpConnectionParams.setSoTimeout(httpParams, 30000);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
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) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
};
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()/1000);
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) {
// TODO Auto-generated catch block
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;
SimpleDateFormat sdf2 = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ssaa");
try{
History.addHistory(sdf2.format(c.getTime()), "GPS",
"Latitude: " + location.getLatitude() + "\n"
+ "Longitude: " + location.getLongitude());}
catch(NullPointerException e){
History.addHistory(sdf2.format(c.getTime()), "GPS",
"Latitude: " + "No Signal" + "\n"
+ "Longitude: " + "No Signal");
}
obj = new org.json.JSONObject(response);
Intent setAlarm = new Intent(UpdateLocation.this,
UpdateLocation.class);
PendingIntent pendingIntent = PendingIntent.getService(
UpdateLocation.this, 0, setAlarm, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int UPDATE_TIME = prefs.getInt("Update_time", 30);
calendar.add(Calendar.MINUTE, UPDATE_TIME);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
注意,我没有广播接收器,我不太清楚如何使用它,所以任何例子都会非常有用。
答案 0 :(得分:0)