我正在我的一项活动中创建闹钟管理员,例如关注
AlarmManager alarmManager=(AlarmManager) MenuItems.this.getSystemService(Context.ALARM_SERVICE);
Intent intent23 = new Intent(MenuItems.this, MyReceiver.class);
PendingIntent pendingIntent23 = PendingIntent.getBroadcast(MenuItems.this, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),300000,
pendingIntent);
此后我跟随BroadcastReceiver
public class MyReceiver extends WakefulBroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
MyAlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
此广播接收器呼叫我的以下服务
Public class MyAlarmService extends Service
{
private String server_response,data_to_send;
private String server ;
private String setLocation = "/xyz";
private URL url;
public double mLatitude;
private MainActivity main;
private boolean isGpsEnabled = false;
private sendToserver send;
private Context context;
public String vallatlong;
private boolean isNetworkEnabled = false;
private boolean canGetLocation = false;
private Location mLocation;
public MyAlarmService(Context context)
{
this.context = context;
getLocation();
}
public MyAlarmService()
{
}
public double mLongitude;
private LocationManager mLocationManager;
private NotificationManager mManager;
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
main = new MainActivity();
getLocation();
}
public String getLocation()
{
server = getString(R.string.server_add_new);
try {
Log.e("this is getlocation method ", "1");
mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
/*getting status of the gps*/
isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
/*getting status of network provider*/
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGpsEnabled && !isNetworkEnabled) {
Log.e("no provider", "non availability");
} else {
Log.e("this is getlocation method ", "2");
this.canGetLocation = true;
/*getting location from network provider*/
if (isNetworkEnabled) {
// mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
Log.e("this is getlocation method ", "3");
if (mLocationManager != null) {
Log.e("this is getlocation method ", "7");
mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (mLocation != null) {
Log.e("this is getlocation method ", "8");
mLatitude = mLocation.getLatitude();
mLongitude = mLocation.getLongitude();
long user_id = main.getDefaultsint("user_id", MyAlarmService.this);
String stringLatitude = String.valueOf(mLatitude);
String stringLongitude = String.valueOf(mLongitude);
vallatlong = stringLatitude+stringLongitude;
try {
url = new URL(server+setLocation);
data_to_send = "lat=" + mLatitude + "&";
data_to_send += "long=" + mLongitude + "&";
data_to_send += "user_id=" + user_id ;
Log.e("data to send", "url"+data_to_send);
send = new sendToserver(url, data_to_send);
send.execute();
Log.e("this is getlocation method ", "6");
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
/*if gps is enabled then get location using gps*/
if (isGpsEnabled) {
if (mLocation == null) {
// mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
Log.e("this is getlocation method ", "4");
if (mLocationManager != null) {
mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mLocation != null) {
Log.e("this is getlocation method ", "5");
mLatitude = mLocation.getLatitude();
mLongitude = mLocation.getLongitude();
long user_id = main.getDefaultsint("user_id", MyAlarmService.this);
try {
url = new URL(server+setLocation);
data_to_send = "lat=" + mLatitude + "&";
data_to_send += "long=" + mLongitude + "&";
data_to_send += "user_id=" + user_id ;
Log.e("data to send", "url"+data_to_send);
send = new sendToserver(url, data_to_send);
send.execute();
Log.e("this is getlocation method ", "6");
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return vallatlong;
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
public double getLatitude() {
if (mLocation != null) {
mLatitude = mLocation.getLatitude();
}
return mLatitude;
}
public double getLongitude() {
if (mLocation != null) {
mLongitude = mLocation.getLongitude();
}
return mLongitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public class sendToserver extends AsyncTask<URL, String, ArrayList<String>>
{
@Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
}
String data_to_send = "";
URL url;
public sendToserver(URL u, String data){
this.url = u;
this.data_to_send = data;
}
@Override
protected ArrayList<String> doInBackground(URL... params) {
BufferedReader reader=null;
try
{
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data_to_send);
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
Log.e("inside", "while loop");
}
server_response = sb.toString();
}
catch(Exception ex)
{
}
return null;
}
}
}
一切正常,我的活动设置了Alarmmanager,每隔5分钟触发一次广播接收器即使我的应用程序关闭且电话未被使用我也会在我的服务器上获得用户位置,但是在某些手机上执行我的服务喜欢galaxy NoteEdge这段代码不起作用。我做错了什么,每隔5分钟就没有得到用户位置。如果这不是正确的做法,请建议任何相关的方法。