开发者

I have created timer in service so I get error how to solve this in Android

开发者 https://www.devze.com 2023-03-04 20:16 出处:网络
I have created one service and also added it in manifest. When I use timer to call this service periodically and in timer I have used location manager to get location of user contin. But there is erro

I have created one service and also added it in manifest. When I use timer to call this service periodically and in timer I have used location manager to get location of user contin. But there is error like that:

05-11 11:57:17.574: ERROR/AndroidRuntime(3305): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Tell me where is problem in my code.

code is here

package com.collabera.labs.sai;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class SimpleService extends Service {
    Timer mytimer;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
    mytimer = new Timer();
    mytimer.schedule(new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            //getdata();
            LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            LocationListener mlocListener = new MyLocationListener();
            mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                    mlocListener);

        }
    },0,1000);

}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}
public void getdata()
{

}
public class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        loc.getLatitude();
        loc开发者_高级运维.getLongitude();
        String Text = "My current location is: " + "Latitude = "
                + loc.getLatitude() + "Longitude = " + loc.getLongitude();
        /*Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
                .show();*/
        Log.d("TAG", "Starting..");
    }

    @Override
    public void onProviderDisabled(String provider) {
        /*Toast.makeText(getApplicationContext(), "Gps Disabled",
                Toast.LENGTH_SHORT).show();*/
    }

    @Override
    public void onProviderEnabled(String provider) {
        /*Toast.makeText(getApplicationContext(), "Gps Enabled",
                Toast.LENGTH_SHORT).show();*/
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}/* End of Class MyLocationListener */
}


Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();

You can't modify UI from the non-UI thread. Services are never executed on the UI thread. Log this instead of making a Toast.

0

精彩评论

暂无评论...
验证码 换一张
取 消