1

- - - - -更新 - - - - - - - - - - - - - - - - - - - - -

当应用程序启动时,我在行收到 numberformat 异常:

final long thetime=Long.parseLong(time_value);

但以上内容不在主要活动中......

在我在edittex中的xml文件中

安卓:inputType="数字" 。

这条线在我的服务类中,我有警报管理器(注意我不能使用 catch,因为在下面(alarm.setRepeating)它不识别“时间”值。

protected void onHandleIntent(Intent intent) {
        //try{
            String time_value;
            time_value=(String) intent.getStringExtra("time_value");
            final long thetime=Long.parseLong(time_value);// }
        //  catch (NumberFormatException e) {

            //}
        mContext = getApplicationContext();
        mHandler.post(new Runnable(){

            @Override
            public void run() {

                // Start service using AlarmManager
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.SECOND, 10);
                Intent intent = new Intent(myservice.this,selection.class);
                PendingIntent pintent = PendingIntent.getService(myservice.this, 0, intent,
                        0);
                AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                        thetime*1000, pintent);
             // Tell the user about what we did.
                Toast.makeText(myservice.this, "Configured time interval",
                        Toast.LENGTH_LONG).show();

            }
        });
    }
}

我从另一个活动加载 time_value:

public void onClick(View v) {
        switch (v.getId()){
        case R.id.btn:
             edit11=edit1.getText().toString();
             edit22=edit2.getText().toString();
             Intent i=new Intent(this,selection.class);
             i.putExtra("code_value",edit11);
             Intent k=new Intent(this,myservice.class);
             k.putExtra("time_value",edit22);
             this.startService(k);
             Intent l=new Intent(this,MainActivity.class);
             startActivity(l);  
             break;

     }
     }
4

3 回答 3

0
because I made lonfitude and latitude Strings (I had them as double before). How can I overcome that?

从双重使用中获取字符串String.valueOf();

String latitude = String.valueOf(location.getLatitude());

重复

Also,how can I select to send the data in time intervals that the user will define?

要以特定间隔重复使用AlarmManager

于 2013-03-27T17:24:35.280 回答
0
how can I select to send the data in time intervals that the user will define

您可以使用TimerTimerTask。将用户输入的时间间隔传递给Timer 的schedule()。应该考虑的事情是当应用程序关闭或用户更改时间间隔然后取消以前的 TimerTask 并清除 Timer(如果有)。

对于警报,在您的活动类中执行以下操作:

PendingIntent Sender = null;
        Intent AlarmIntent = new Intent("com.example.gpstrackdemo.RECEIVEALARM");
        AlarmManager AlmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Sender = PendingIntent.getBroadcast(GpsTrackActivity.this, 0,
                AlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, 0, 15 * 60 * 1000,
                Sender);

AlarmIntent 是BroadcastReceiver的意图。这是它的代码:

public class StartServiceReceiver extends BroadcastReceiver 
{
private static final String TAG = "StartServiceReceiver";
@Override
public void onReceive(Context context, Intent intent) 
{
    Intent serviceIntent = new Intent(context, MyLocationService.class);
    context.startService(serviceIntent);
    Log.v(TAG, "onReceive called");
}
}

收到广播后,它将启动位置服务,我们将在其中获取用户的当前位置。

服务等级:

public class MyLocationService extends Service implements
    OnLocationReceivedListener {

private LocationManager manager;
private Location location = null;
PowerManager powerManager;
private WakeLock wakeLock;
private String country;
GPSLocationListener mGPSLocationListener;
NetworkLocationListener mNetworkLocationListener;
private static final int MAX_ATTEMPTS = 250;
private static String TAG = "MyLocationService";
LocTimerTask mTimerTask;
int mSattelites;
Timer myLocTimer;
int count = 0;
boolean isGPS;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.v(TAG, "onStartCommand called");
    getCurrentLocation();
    return START_STICKY;
}

@Override
public void onCreate() {
    Log.v(TAG, "onCreate called");
    powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "mywakelock");
    mGPSLocationListener = new GPSLocationListener();
    mNetworkLocationListener = new NetworkLocationListener();
    wakeLock.acquire();
    super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public void getCurrentLocation() {
    manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    manager.addGpsStatusListener(mGPSStatusListener);
    mTimerTask = new LocTimerTask(LocationManager.GPS_PROVIDER);

    if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        Log.v(TAG, "GPS ENABLED");
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,
                50.0f, mGPSLocationListener);
    }

    if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000L,
                50.0f, mNetworkLocationListener);
    }

    myLocTimer = new Timer("LocationRunner", true);
    myLocTimer.schedule(mTimerTask, 0, 500);
}

public class GPSLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location argLocation) {
        location = argLocation;
    }

    public void onProviderDisabled(String provider) {

    }

    public void onProviderEnabled(String provider) {

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {

    }
}

public class NetworkLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location argLocation) {
        location = argLocation;
    }

    public void onProviderDisabled(String provider) {

    }

    public void onProviderEnabled(String provider) {

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {

    }
}
class LocTimerTask extends TimerTask {
    String provider;

    public LocTimerTask(String provider) {
        this.provider = provider;
    }

    final Handler mHandler = new Handler(Looper.getMainLooper());

    Runnable r = new Runnable() {
        @Override
        public void run() {
            count++;
            Log.v(TAG, "Timer Task run" + i);
            location = manager.getLastKnownLocation(provider);

            if (location != null) {
                Log.v(TAG, "in timer task run in if location not null");
                onLocationReceived(location);
                myLocTimer.cancel();
                myLocTimer.purge();
                mTimerTask.cancel();
                return;
            } else {
                isGPS = false;
                if (location == null && count == MAX_ATTEMPTS) {
                    turnGPSOff();
                    location = manager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {

                        onLocationReceived(location);
                        myLocTimer.cancel();
                        myLocTimer.purge();
                        mTimerTask.cancel();
                        return;
                    }
                } else {
                    return;
                }
            }
            count = 0;
        }
    };

    public void run() {
        mHandler.post(r);
    }
}

private GpsStatus.Listener mGPSStatusListener = new GpsStatus.Listener() {

    @Override
    public synchronized void onGpsStatusChanged(int event) {
        switch (event) {
        case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
            GpsStatus status = manager.getGpsStatus(null);
            mSattelites = 0;
            Iterable<GpsSatellite> list = status.getSatellites();
            for (GpsSatellite satellite : list) {
                if (satellite.usedInFix()) {
                    mSattelites++;
                }
            }
            break;

        case GpsStatus.GPS_EVENT_FIRST_FIX:
            break;

        case GpsStatus.GPS_EVENT_STARTED:
            break;

        case GpsStatus.GPS_EVENT_STOPPED:
            break;
        default:
            break;
        }
    }
};

public void onDestroy() {
    super.onDestroy();
    if (myLocTimer != null) {
        myLocTimer.cancel();
        myLocTimer.purge(); //Timer not required any more
    }

    if (mTimerTask != null) {
        if (mTimerTask.r != null) {
            mTimerTask.mHandler.removeCallbacks(mTimerTask.r);
        }
    }

    if (manager != null) {
        if (mGPSLocationListener != null) {
            manager.removeUpdates(mGPSLocationListener);
        }

        //remove location updates for listener once your work is done, otherwise it will drain battery
        if (mNetworkLocationListener != null) {
            manager.removeUpdates(mNetworkLocationListener);
        }

        if (mGPSStatusListener != null) {
            manager.removeGpsStatusListener(mGPSStatusListener);
        }
    }
}

@Override
public void onLocationReceived(Location mLoc) {
    //Send data to http server once you get location.
}
}

在这里,我的服务类实现了一个侦听器,该侦听器具有一个回调方法onLocationReceived,您可以在获得位置后在其中执行您的操作。

public interface OnLocationReceivedListener {
    public void onLocationReceived(Location mLoc);
}

在您的清单中,分别声明广播接收器和服务:

<receiver
        android:name=".receiver.StartServiceReceiver"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.example.gpstrackdemo.RECEIVEALARM" />

        </intent-filter>
    </receiver>
<service android:name=".service.MyLocationService"
        android:enabled="true"></service>
于 2013-03-27T17:48:04.490 回答
0

好的,我忘记了:

startService() 是我的主要活动。我不得不把它放在我的服务活动中。

谢谢大家

于 2013-03-29T10:14:32.883 回答