package com.example.oguz.gpssms;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView latitude;
private TextView longitude;
private TextView choice;
private CheckBox fineAcc;
private Button choose;
private TextView provText;
private LocationManager locationManager;
private String provider;
private MyLocationListener mylistener;
private Criteria criteria;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SmsActivity.class);
startActivity(intent);
}
});
latitude = (TextView) findViewById(R.id.lat);
longitude = (TextView) findViewById(R.id.lon);
provText = (TextView) findViewById(R.id.prov);
choice = (TextView) findViewById(R.id.choice);
fineAcc = (CheckBox) findViewById(R.id.fineAccuracy);
choose = (Button) findViewById(R.id.chooseRadio);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE); //default
// user defines the criteria
choose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(fineAcc.isChecked()){
criteria.setAccuracy(Criteria.ACCURACY_FINE);
choice.setText("fine accuracy selected");
}else {
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
choice.setText("coarse accuracy selected");
}
}
});
criteria.setCostAllowed(false);
// get the best provider depending on the criteria
provider = locationManager.getBestProvider(criteria, false);
// the last known location of this provider
Location location = locationManager.getLastKnownLocation(provider);
mylistener = new MyLocationListener();
if (location != null) {
mylistener.onLocationChanged(location);
} else {
// leads to the settings because there is no last known location
// Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
// startActivity(intent);
}
// location updates: at least 1 meter and 200millsecs change
locationManager.requestLocationUpdates(provider, 200, 1, mylistener);
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// Initialize the location fields
latitude.setText("Latitude: "+String.valueOf(location.getLatitude()));
longitude.setText("Longitude: "+String.valueOf(location.getLongitude()));
provText.setText(provider + " provider has been selected.");
Toast.makeText(MainActivity.this, "Location changed!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(MainActivity.this, provider + "'s status changed to "+status +"!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(MainActivity.this, "Provider " + provider + " enabled!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this, "Provider " + provider + " disabled!",
Toast.LENGTH_SHORT).show();
}
}
}
인 SmsActivity.java
입니다
package com.example.oguz.gpssms;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.BatteryManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by OGUZ on 26.12.2014.
*/
public class SmsActivity extends MainActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
EditText editText = (EditText)findViewById(R.id.telNo);
final String telNo = editText.getText().toString();
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("smsto:" +telNo);
Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body","Merhaba");
startActivity(intent);
finish();
}
});
};
}
여기에 MainActivity.java –
.java 코드와 로그캣 출력을 보내십시오. – PPD
.java 코드와 logcat을 보내 주셔서 감사합니다. –