Kód: Vybrat vše
package com.example.gpstracking;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.CheckBox;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class AndroidGPSTrackingActivity extends Activity {
String souradnice;
Button btnShowLocation;
// Definice globální proměnné GPSTracker class
GPSTracker gps;
Calendar c = Calendar.getInstance();
private CheckBox checkBox1;
private CheckBox checkBox2;
public static String soubor="lokace.txt";
String s;
double lat;
double lon;
String datum;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
checkBox1=(CheckBox)findViewById(R.id.checkBox1);
checkBox2=(CheckBox)findViewById(R.id.checkBox2);
// Přiřazení Click listeneru tlačítku pro zobrazení souřadnic
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
/* public void save(View v){
try{
OutputStream MyOutputStreams = openFileOutput(soubor,0);
OutputStreamWriter MyOutputStream = new OutputStreamWriter(MyOutputStreams);
MyOutputStream.write(s);
MyOutputStream.close();
//Log.d("save", "save");
Toast.makeText(this, "Zapis do souboru proběhl v pořádku",Toast.LENGTH_LONG).show();
} catch (Exception e){
//e.printStackTrace();{
// Log.d("eror", "eror");
Toast.makeText(this, "Zapis do souboru se nepodařil",Toast.LENGTH_LONG).show();
}
}*/
public void onClick(View arg0) {
// Vytvoření instance třídy GPSTracker
gps = new GPSTracker(AndroidGPSTrackingActivity.this);
// Kontrola, zda je zapnuta GPS v zařízení
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
if (checkBox1.isChecked()){
SmsManager smsManager=SmsManager.getDefault();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm");
String formattedDate = df.format(c.getTime());
String sms= "http://maps.google.com/maps?f=g&g="+Double.toString(lat)+","+Double.toString(lon)+"&z16";
//sms=sms+Double.toString(lon);
//public String f= formattedDate+"\n"+sms;
s=formattedDate+"\n"+sms+"\n";
smsManager.sendTextMessage("123456978", null, formattedDate+"\n"+sms, null, null);
datum=formattedDate;
}
if (checkBox2.isChecked()){
if (isWriteable()){
write();
}
}
Toast.makeText(getApplicationContext(), "Gps souřadnice jsou - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// Pokud nemůžeme získat souřadnice pomocí GPS
// nebo je získání souřadnic pomocí sítě vypnuto
// zobrazíme uživateli dialogové okno
gps.showSettingsAlert();
}
}
private void write()
{
File sdCard= new File (Environment.getExternalStorageDirectory()+"/msoubory");
if (!sdCard.exists()){
sdCard.mkdir();
}
File f= new File(sdCard.getAbsolutePath(),"Lokace.txt");
try
{
FileOutputStream fos = new FileOutputStream(f,true);
String data = datum+"\n"+"http://maps.google.com/maps?f=g&g="+Double.toString(lat)+","+Double.toString(lon)+"&z16";
try {
fos.write(data.getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
private boolean isWriteable()
{
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
return true;
}else {
return false;
}
}}}}díky za pomoc