Java 通过文本发送位置
我无法将我的位置坐标发送到手机 这里是Main.Java的代码Java 通过文本发送位置,java,android,listview,gps,Java,Android,Listview,Gps,我无法将我的位置坐标发送到手机 这里是Main.Java的代码 package com.example.finder; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationListene
package com.example.finder;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
TextView textLat;
TextView textLong;
TextView textAlt;
public String onLocat;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.checkin);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bundle extras = getIntent().getExtras();
String ALT = extras.getString("wysokosc");
String SPE = extras.getString("speed");
sendSMS("09164232662", "wys" + " " + ALT + " " + "spee" + " - "
+ SPE);
}
});
textLat = (TextView) findViewById(R.id.textLat);
textLong = (TextView) findViewById(R.id.textLong);
textAlt = (TextView) findViewById(R.id.textAlt);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
class mylocationlistener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
double pLong = location.getLongitude();
double pLat = location.getLatitude();
double pAlt = location.getAltitude();
String PPro = location.getProvider();
textLat.setText(Double.toString(pLat));
textLong.setText(Double.toString(pLong));
textAlt.setText(Double.toString(pAlt));
Intent i = new Intent(Main.this,
Main.class);
i.putExtra("wysokosc",Double.toString(pLat));
i.putExtra("speed",Double.toString(pLong));
startActivity(i);
}
}
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 sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("09164232662", null, message, null, null);
}
}
package com.example.finder;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
public class GPS extends Activity {
Button checkout;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
checkout = (Button) findViewById(R.id.checkout);
checkout.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Bundle extras = getIntent().getExtras();
String ALT = extras.getString("wysokosc");
String SPE = extras.getString("speed");
sendSMS("09164232662", "wys" + " " + ALT + " " + "spee" + " - "
+ SPE);
}
});
}
// ---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}
这是GPS.Java的代码
package com.example.finder;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
TextView textLat;
TextView textLong;
TextView textAlt;
public String onLocat;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.checkin);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bundle extras = getIntent().getExtras();
String ALT = extras.getString("wysokosc");
String SPE = extras.getString("speed");
sendSMS("09164232662", "wys" + " " + ALT + " " + "spee" + " - "
+ SPE);
}
});
textLat = (TextView) findViewById(R.id.textLat);
textLong = (TextView) findViewById(R.id.textLong);
textAlt = (TextView) findViewById(R.id.textAlt);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
class mylocationlistener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
double pLong = location.getLongitude();
double pLat = location.getLatitude();
double pAlt = location.getAltitude();
String PPro = location.getProvider();
textLat.setText(Double.toString(pLat));
textLong.setText(Double.toString(pLong));
textAlt.setText(Double.toString(pAlt));
Intent i = new Intent(Main.this,
Main.class);
i.putExtra("wysokosc",Double.toString(pLat));
i.putExtra("speed",Double.toString(pLong));
startActivity(i);
}
}
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 sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("09164232662", null, message, null, null);
}
}
package com.example.finder;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
public class GPS extends Activity {
Button checkout;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
checkout = (Button) findViewById(R.id.checkout);
checkout.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Bundle extras = getIntent().getExtras();
String ALT = extras.getString("wysokosc");
String SPE = extras.getString("speed");
sendSMS("09164232662", "wys" + " " + ALT + " " + "spee" + " - "
+ SPE);
}
});
}
// ---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}
这是清单文件的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.finder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.finder.GPS"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
问题是它正在接收信号,但却没有通过我的手机发送
让我知道我做错了什么 更改为:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
final String ALT = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude();
final String SPE = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude();
Button btn1 = (Button) findViewById(R.id.checkin);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendSMS("09164232662", "wys" + " " + ALT + " " + "spee" + " - "
+ SPE);
}
});
textLat = (TextView) findViewById(R.id.textLat);
textLong = (TextView) findViewById(R.id.textLong);
textAlt = (TextView) findViewById(R.id.textAlt);
}
我认为您的应用程序中存在概念问题。首先,您在清单中定义了两个入口点,它应该类似于:
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.finder.GPS" />
然后你的CPS就没用了,你把它叫做什么
同样的活动叫什么
意向i=新意向(Main.this,
主要类别),;
i、 putExtra(“wysokosc”,双倍toString(平台));
i、 putExtra(“速度”,双方向旋转(扑通声));
星触觉(i);
只需将pLong、pLat和pAlt定义为全局,并在onClick中使用它们来发送短信
希望这有帮助。有日志吗?你得到的坐标完美吗?你添加了
SEND\u SMS
权限了吗?对不起,我是堆栈溢出新手,我对发布代码不太熟悉,所以我不得不一个接一个地做。尝试添加Log.v(“Intent Extras”,ALT+SPE)代码>紧接着stringspe=extras.getString(“速度”)代码>以确保您正确获取了额外的意图。