下面给出的代码告诉我,每当我调试时它都能正常工作。在 Mainactivity 类中,当我按下地图上的按钮时。待处理的意图调用广播接收器并传递经度和纬度。我在调试中检查了它。值正确传递,smsmanager也被调用。但是,当我要求调试器发送短信卡时(只需按住F8按钮而不是向前移动)并且没有短信发送到指定的号码。除此之外,我还使用闹钟管理器,但我认为有一些问题我无法想象。
MainActivity.java
@Override
protected void onCreate()
{
setContentView(R.layout.main);
super.onResume();
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
}
OnClickListener btnShareLocationOnClick = new OnClickListener()
{
@Override
public void onClick(View v)
{
if(mapView.getGpsLocation()!= null){
Intent intent = new Intent(this_, SendSmsReceiver.class);
intent.setAction("com.android.myapp.ACTION");
intent.putExtra("longitude", mapView.getGpsLocation().getLongitude());
intent.putExtra("latitude", mapView.getGpsLocation().getLatitude());
PendingIntent pendingIntent = PendingIntent
.getBroadcast(this_, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
alarm.cancel(pendingIntent);
alarm.setRepeating(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis()+1000,
AlarmManager.INTERVAL_DAY,
pendingIntent);
}
d.dismiss();
}
};
d.dismiss 只关闭我按下按钮的对话框
SendSmsReceiver
public class SendSmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String phoneNumberReciver="XXXX";
double lonPresent = intent.getDoubleExtra("longitude", 0.0);
double latPresent = intent.getDoubleExtra("latitude",0.0);
String message="AndroidCoordinates\n"+lonPresent+" Break "+ latPresent;
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumberReciver, "5554", message, null, null);
}
}
XXXX 可以是任何电话号码
Manifast
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<receiver android:name="com.myapp.smssending.SendSmsReceiver">
<intent-filter>
<action android:name="com.android.myapp.ACTION" />
</intent-filter>
</receiver>