Android 安卓-点击按钮,消息发送不会停止
我正在尝试构建一个应用程序,通过短信分享我的位置。在活动中,我有一个按钮和一个文本视图。单击按钮后,文本消息将发送到提供的指定号码。问题是,单击按钮后,除非卸载应用程序,否则消息发送不会停止。一次发送大约30-40条短信。代码如下: Gps4Activity.java 任何人都可以建议一种方法来检查发送的邮件数量。我只想在点击按钮时发送一条短信Android 安卓-点击按钮,消息发送不会停止,android,android-gps,android-sms,Android,Android Gps,Android Sms,我正在尝试构建一个应用程序,通过短信分享我的位置。在活动中,我有一个按钮和一个文本视图。单击按钮后,文本消息将发送到提供的指定号码。问题是,单击按钮后,除非卸载应用程序,否则消息发送不会停止。一次发送大约30-40条短信。代码如下: Gps4Activity.java 任何人都可以建议一种方法来检查发送的邮件数量。我只想在点击按钮时发送一条短信 谢谢:使用下面的功能只发送一个号码的短信 private void sendMySMS(String personalPhone, String me
谢谢:使用下面的功能只发送一个号码的短信
private void sendMySMS(String personalPhone, String messege)
{
SmsManager sms = SmsManager.getDefault();
List<String> messages = sms.divideMessage(messege);
for (String msg : messages)
{
PendingIntent sentIntent = PendingIntent.getBroadcast(getActivity(), 0, new Intent("SMS_SENT"), 0);
PendingIntent deliveredIntent = PendingIntent.getBroadcast(getActivity(), 0, new Intent("SMS_DELIVERED"), 0);
sms.sendTextMessage(personalPhone, null, msg, sentIntent, deliveredIntent);
}
}
我认为这是因为likelyPlaces可以包含很多地方,而您将sms方法放在likelyPlaces循环中。因此,它将为每个可能的位置数据发送短信。如果它包含40个位置,它将发送40次。您正在为多个位置发送消息,您可以选择最佳匹配,只发送一条这样的消息,或者在发送消息后中断循环
private void callPlaceDetectionApi() throws SecurityException {
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
//Edited
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
Log.i(LOG_TAG, String.format("Place '%s' with " +
"likelihood: %g",
placeLikelihood.getPlace().getName(),
placeLikelihood.getLikelihood()));
display2.setText(placeLikelihood.getPlace().getAddress().toString());
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, placeLikelihood.getPlace().getAddress().toString(),
null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
break;
}
likelyPlaces.release();
// if(likelyPlaces != null && likelyPlaces.get(0) != null) {
// PlaceLikelihood placeLikelihood = likelyPlaces.get(0);
// Log.i(LOG_TAG, String.format("Place '%s' with " +
// "likelihood: %g",
// placeLikelihood.getPlace().getName(),
// placeLikelihood.getLikelihood()));
// display2.setText(placeLikelihood.getPlace().getAddress().toString());
// SmsManager smsManager = SmsManager.getDefault();
// smsManager.sendTextMessage(number, null, placeLikelihood.getPlace().getAddress().toString(),
// null, null);
// Toast.makeText(getApplicationContext(), "SMS sent.",
// Toast.LENGTH_LONG).show();
// likelyPlaces.release();
// }
}
});
}
我可以用getApplicationContext代替getActivity吗?是的…getActivity将在片段中使用…对于必须使用getApplicationContext的活动,请向上投票并勾选我的问题,如果它真的对uItr不起作用有帮助!!!,我的问题是,当我点击按钮时,它会继续向指定号码发送消息,除非我删除应用程序,否则它会继续发送消息,并且不会停止。我只需要发送一条短信到指定的邮箱number@PritomMazumdar检查我的答案。你知道我该怎么做吗?就像我试图创建一个单独的方法来发送短信,但仍然没有工作如果你只是想发送短信,只是把中断;在Toast.makeTextgetApplicationContext下,SMS sent.,Toast.LENGTH\u LONG.show;非常感谢你的帮助!!您提供的代码崩溃并抛出异常,但在我发送消息后设置中断可以正常工作。非常感谢你的帮助!!
private void callPlaceDetectionApi() throws SecurityException {
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
//Edited
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
Log.i(LOG_TAG, String.format("Place '%s' with " +
"likelihood: %g",
placeLikelihood.getPlace().getName(),
placeLikelihood.getLikelihood()));
display2.setText(placeLikelihood.getPlace().getAddress().toString());
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, placeLikelihood.getPlace().getAddress().toString(),
null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
break;
}
likelyPlaces.release();
// if(likelyPlaces != null && likelyPlaces.get(0) != null) {
// PlaceLikelihood placeLikelihood = likelyPlaces.get(0);
// Log.i(LOG_TAG, String.format("Place '%s' with " +
// "likelihood: %g",
// placeLikelihood.getPlace().getName(),
// placeLikelihood.getLikelihood()));
// display2.setText(placeLikelihood.getPlace().getAddress().toString());
// SmsManager smsManager = SmsManager.getDefault();
// smsManager.sendTextMessage(number, null, placeLikelihood.getPlace().getAddress().toString(),
// null, null);
// Toast.makeText(getApplicationContext(), "SMS sent.",
// Toast.LENGTH_LONG).show();
// likelyPlaces.release();
// }
}
});
}