我正在尝试创建一个在后台无助的应用程序。我想要它做的是被动地跟踪速度(基于用户的愿望在MPH和KmPH之间可互换)。当文本消息到达设备时,我希望它检查速度,如果它低于用户定义的阈值,我希望它将SMS发送到默认应用程序。如果超过阈值,我希望它将自动消息发送回发件人。
目前我有2个活动,因为我无法扩展Activity和BroadcastReceiver。
我的第一个问题是......如何被动跟踪速度... 我的第二个问题是......如何在收到短信时将速度传递给短信活动?
这是我的GPSActivity
package biz.midl.drivereply;
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.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity implements LocationListener {
String lsms;
String prefName = "chaosgpsreply";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
this.onLocationChanged(null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
TextView tv = (TextView) findViewById(R.id.textView1);
if (location==null)
{
tv.setText("-.- m/s");
}
else
{
float nCurrentSpeed = location.getSpeed();
tv.setText("" + nCurrentSpeed + " m/s");
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
这是我的SMSActivity
package biz.midl.drivereply;
import java.util.Timer;
import java.util.TimerTask;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
public class SMSportion extends BroadcastReceiver {
String lsms;
String prefName = "chaosgpsreply";
static Timer timer1 = new Timer();
static long delay = 30000;
static TimerTask tt1;
@Override
public void onReceive(Context context, Intent intent) {
String phoneNumber=null, messageText=null;
String textMessage = "I can't come to the phone right now, be back soon";
//---gather up all the necessary user input---
SmsManager sms = SmsManager.getDefault();
try
{
sms.sendTextMessage(phoneNumber, null, messageText, null, null);
}
catch(IllegalArgumentException e)
{
}
//timer(); //this will be integrated in later - ignore this
sendSMS(phoneNumber, textMessage);
}
public void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}