我试图发送当前位置的短信,但是当我选择菜单选项时,没有任何反应。这是我的代码:我可以使用它的任何可能的解决方案吗?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
final LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
final LocationListener mlocListener = new MyLocationListener();
final Location location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateWithNewLocation(location);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
if (Build.VERSION.SDK_INT >= 21) {
// Call some material design APIs here
} else {
// Implement this feature without material design
}
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(final Location location)
{
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
private void updateWithNewLocation(final Location location)
{
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
String addressString = "No address found";
if ( location != null )
{
final double lat = location.getLatitude();
final double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
final double latitude = location.getLatitude();
final double longitude = location.getLongitude();
final Geocoder gc = new Geocoder(this, Locale.getDefault());
try
{
final List<Address> addresses = gc.getFromLocation(latitude, longitude, 4);
final StringBuilder sb = new StringBuilder();
if ( addresses.size() > 0 )
{
final Address address = addresses.get(0);
for ( int i = 0; i < address.getMaxAddressLineIndex(); i++ )
{
sb.append(address.getAddressLine(i)).append("\n");
}
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
}
catch ( final IOException e )
{
}
}
else
{
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString + "\n" + addressString);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
String tv1;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
tv1= myLocationText.getText().toString();
switch (item.getItemId()) {
case R.id.sms_location:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", tv1);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
return(true);
case R.id.email_location:
/* Create the Intent */
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
/* Fill it with Data */
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"to@email.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, tv1);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
return(true);
}
return super.onOptionsItemSelected(item);
}}
这是我的活动:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="40dp"
android:paddingRight="40dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/myLocationText"
/>
</LinearLayout>
菜单下的我的menu_main声明了我的项目。我真的在寻找解决方案而且我在日志猫身上看不到任何东西!
答案 0 :(得分:0)
因为你得到了你的位置。你可以这样做。
public void onLocationChanged(final Location location)
{
updateWithNewLocation(location);
SendSms(location);
}
public void SendSms(Location location){
int phoneNo = "your phone number";
double lattitude = location.getlatitude();
double longitude = location.getLongitude();
Similary any information you want
then
String message = "LAttiude "+lattitude+ " Longitude "+ "longitude"
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);
}
希望你弄清楚,记得加上这个权限。
<uses-permission android:name="android.permission.SEND_SMS" />
以下是更多信息化的教程。
http://www.tutorialspoint.com/android/android_sending_sms.htm