Prohlédněte si můj profil - Linux/KVM/XEN/RHEL cz.linkedin.com/in/mkonicek/
07 Lis

Android

Vývojové prostředí Eclipse

Zdrojáky ke stažení

eclipse-workspace

Ukázka kódu – jednoduchá služba s JSON

...
// TODO Auto-generated method stub
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
Status = "GPS locator started...";
initListView1();
mHandler.post(mUpdateTask);
Button mybutton = (Button) findViewById(R.id.button2);
mybutton.setText("Minimise...");
mybutton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
moveTaskToBack(true);
}
});
}
};
bindService(svc, conn, Context.BIND_AUTO_CREATE);
}

public void onStop(View v){
if(mService != null && mService.startLocation != null){
new AlertDialog.Builder(this)
.setMessage("Really want to Stop a trip?")
.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
saveTrip();
Status = "Trip saved...";
initListView1();
mHandler.removeCallbacks(mUpdateTask);
stopService(new Intent(MyMileageActivity.this, MyService.class));
finish();
}
})
.setNegativeButton("Cancel", new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
})
.show();
} else if(mService == null) {
new AlertDialog.Builder(this)
.setMessage("You have to start trip first!")
.setPositiveButton("Ok", new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
})
.show();
} else if(mService.startLocation == null){
new AlertDialog.Builder(this)
.setMessage("Waiting for GPS signal!")
.setPositiveButton("Ok", new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
})
.setNegativeButton("Cancel", new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
finish();
}
})
.show();
}

}

public void saveTrip(){
JSONRPCClient client = JSONRPCClient.create(MyMileageActivity.JSON_SERVER);
client.setConnectionTimeout(2000);
client.setSoTimeout(2000);
try {
double startLatitude = MyMileageActivity.mService.startLocation.getLatitude();
double startLongitude = MyMileageActivity.mService.startLocation.getLongitude();
double endLatitude = MyMileageActivity.mService.lastLocation.getLatitude();
double endLongitude = MyMileageActivity.mService.lastLocation.getLongitude();
float distance = MyMileageActivity.mService.distance;
client.call("saveTrip",startLatitude, startLongitude, endLatitude, endLongitude, distance, MyMileageActivity.Hash);
}
catch (JSONRPCException e)
{
e.printStackTrace();
}
}

private Runnable mUpdateTask = new Runnable() {

public void run() {
// TODO Auto-generated method stub
Distance = Float.toString(mService.getDistance());
startAddress = mService.startAddress;
initListView1();
mHandler.postDelayed(mUpdateTask, 100);
}
};
...

Zobrazeno: 738x