gpt4 book ai didi

android - 广播接收器中的定时器功能以识别 Android 应用程序中的 gps?

转载 作者:行者123 更新时间:2023-11-29 14:02:32 26 4
gpt4 key购买 nike

嗨,我试图在广播接收器应用程序中添加定时器功能以接收 gps 中的纬度和经度。但我得到了错误。请任何人纠正我的错误。

提前致谢

我更新错误部分

public class MainActivity extends Activity implements LocationListener {
final DBAdapter1 db=new DBAdapter1(this);
private ConnectivityReceiver receiver = null;
private TextView txtNetworkInfo ;
private static TextView latituteField;
private static TextView longitudeField;
private LocationManager locationManager;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
private static final String HostUrl =" xxxxx";
// private static final String HostUrl =" yyyy";
private static final String NAMESPACE = "http://tempuri.org/";
private HttpTransportSE httpTransport = new HttpTransportSE(HostUrl);
private String provider;
private SoapObject request=null;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1800000; // in Milliseconds

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
Log.i("ConnTest",locationManager.toString());
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);

// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
float lat = (float) (location.getLatitude());
float lng = (float) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));

} else {
latituteField.setText("provider not available");
longitudeField.setText("provider not available");
}

txtNetworkInfo = (TextView)findViewById(R.id.txtNetworkInfo);

receiver = new ConnectivityReceiver();

registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));


}
@Override
protected void onResume() {
super.onResume();
//locationManager.requestLocationUpdates(provider,1000, 1, this);
locationManager.requestLocationUpdates(
provider,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
this
);
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
float lat = (float) (location.getLatitude());
float lng = (float) (location.getLongitude());
longitudeField.setText(String.valueOf(lng));
latituteField.setText(String.valueOf(lat));
try {
db.open();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long id=db.insert(latituteField.getText().toString(),longitudeField.getText().toString());
if(id>1)
{
Toast.makeText(getBaseContext(),"one record is inserted",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getBaseContext(),"not inserted",Toast.LENGTH_LONG).show();

}

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Toast.makeText(this, "Disenabled provider " + provider,
Toast.LENGTH_SHORT).show();

}
protected void Display(Cursor c) {
Toast.makeText(this, "rowid: " + c.getString(0) + "\n" +
"Latitude: " + c.getString(1) + "\n" + "Longitude: " + c.getString(2) + "\n" +
Toast.LENGTH_LONG, 0).show();
}

@Override
protected void onDestroy() {
unregisterReceiver(receiver);

super.onDestroy();
}

private String getNetworkStateString(NetworkInfo.State state){
String stateString = "Unknown";

switch(state)
{
case CONNECTED: stateString = "Connected"; break;
case CONNECTING: stateString = "Connecting"; break;
case DISCONNECTED: stateString = "Disconnected"; break;
case DISCONNECTING: stateString = "Disconnecting"; break;
case SUSPENDED: stateString = "Suspended"; break;
default: stateString = "Unknown"; break;
}

return stateString;
}

private class ConnectivityReceiver extends BroadcastReceiver{

private Timer mTimer;
private TimerTask mTimerTask;

@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

if(null != info)
{
String state = getNetworkStateString(info.getState());
if(state.equals("Connected")){
mTimer = new Timer();
mTimer.scheduleAtFixedRate(mTimerTask,1000,5000);
mTimerTask = new TimerTask() {

@Override
public void run() {
result();
}
};
}
else{
//result();
}

}
}
}

private void result() {
SoapPrimitive response=null;
final String methodname="InsertAllGPSInformation";
request = new SoapObject(NAMESPACE,methodname);
envelope.dotNet = true;
request.addProperty("Longitude",longitudeField.getText().toString());
request.addProperty("Latitude",latituteField.getText().toString());
request.addProperty("Date",newtime);

envelope.setOutputSoapObject(request);
String result = null;
try
{
httpTransport.call(NAMESPACE+"IService/"+methodname, envelope);
response = ( SoapPrimitive )envelope.getResponse();
result=response.toString();
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), "Your Net Connected or Not Login to Net"+"", Toast.LENGTH_LONG).show();
Log.e("Upload Picture Error:",e.getMessage());
}
if(result.equals("Saved Successfully")){
Toast.makeText(getBaseContext(), ""+result, Toast.LENGTH_LONG).show();
}
}

}

更新

    01-09 17:13:27.168: ERROR/AndroidRuntime(1983): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
01-09 17:13:27.168: ERROR/AndroidRuntime(1983): at android.os.Handler.<init>(Handler.java:121)
01-09 17:13:27.168: ERROR/AndroidRuntime(1983): at android.widget.Toast.<init>(Toast.java:68)
01-09 17:13:27.168: ERROR/AndroidRuntime(1983): at android.widget.Toast.makeText(Toast.java:231)
01-09 17:13:27.168: ERROR/AndroidRuntime(1983): at com.varma.samples.conntest.MainActivity.result(MainActivity.java:240)
01-09 17:13:27.168: ERROR/AndroidRuntime(1983): at com.varma.samples.conntest.MainActivity.access$1(MainActivity.java:212)
01-09 17:13:27.168: ERROR/AndroidRuntime(1983): at com.varma.samples.conntest.MainActivity$ConnectivityReceiver$1.run(MainActivity.java:199)
01-09 17:13:27.168: ERROR/AndroidRuntime(1983): at java.util.Timer$TimerImpl.run(Timer.java:289)

最佳答案

当您在 ConnectivityReceiver< 的 onRecieve 中调用 mTimer.scheduleAtFixedRate(mTimerTask,1000,5000); 时,

mTimerTask 为空 类。

你需要把那行放在后面

mTimerTask = new TimerTask() {
@Override
public void run() {
result();
}
};

因此您的代码将如下所示:

mTimerTask = new TimerTask() {
@Override
public void run() {
result();
}
};
mTimer.scheduleAtFixedRate(mTimerTask,1000,5000);

要自己解决这个问题,如果您查看错误消息,您会看到(大约一半):

引起:java.lang.NullPointerException

现在您知道是什么导致了您的错误。现在从那里往下看,直到找到您创建的类和方法。在这种情况下:

在 com.varma.samples.conntest.MainActivity$ConnectivityReceiver.onReceive(MainActivity.java:196)

你所在的类文件是 com.varma.samples.conntest.MainActivity 你所在的类是 ConnectivityReveiver 你所在的方法是onReceive。如果这在括号中没有帮助,则为您的 NullPointer 源自的 java 文件和行号。

如果在抛出 NullPointerException 之前通过不同的方法传递变量,有时您可能需要回溯几行。

关于android - 广播接收器中的定时器功能以识别 Android 应用程序中的 gps?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8787441/

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