2017-08-05 47 views
0

我正在嘗試構建一個應用程序,通過短信分享我的位置。在activity我有一個button和一個TextView。點擊button後,text message被髮送到指定的號碼。問題是,一旦我點擊buttonmessage發送不會停止,除非我卸載該應用程序。一次去發送大約30-40條短信。代碼如下:Android-點擊按鈕發送消息不會停止

Gps4Activity.java

public class Gps4Activity extends AppCompatActivity implements 
    GoogleApiClient.OnConnectionFailedListener { 

private static final String LOG_TAG = "PlacesAPIActivity"; 
private static final int GOOGLE_API_CLIENT_ID = 0; 
private GoogleApiClient mGoogleApiClient; 
private static final int PERMISSION_REQUEST_CODE = 100; 
private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ; 
private TextView display1,display2,display3,display4,display5; 
private Button button; 
String number="+91xxxxxxxxxx"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_gps4); 
    button=(Button)findViewById(R.id.showL); 
    display1=(TextView)findViewById(R.id.location2); 
    display2=(TextView)findViewById(R.id.location3); 
    display3=(TextView)findViewById(R.id.location4); 
    display4=(TextView)findViewById(R.id.location5); 
    display5=(TextView)findViewById(R.id.location6); 

    mGoogleApiClient = new GoogleApiClient.Builder(Gps4Activity.this) 
      .addApi(Places.PLACE_DETECTION_API) 
      .enableAutoManage(this, GOOGLE_API_CLIENT_ID, this) 
      .build(); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (mGoogleApiClient.isConnected()) { 
       if (ActivityCompat.checkSelfPermission(Gps4Activity.this, 
         android.Manifest.permission.ACCESS_FINE_LOCATION) 
         != PackageManager.PERMISSION_GRANTED) { 
        ActivityCompat.requestPermissions(Gps4Activity.this, 
          new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
          PERMISSION_REQUEST_CODE); 
        ActivityCompat.requestPermissions(Gps4Activity.this, 
          new String[]{Manifest.permission.SEND_SMS}, 
          MY_PERMISSIONS_REQUEST_SEND_SMS); 
       } else { 
        callPlaceDetectionApi(); 
       } 

      } 
     } 
    }); 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
    Log.e(LOG_TAG, "Google Places API connection failed with error code: " 
      + connectionResult.getErrorCode()); 

    Toast.makeText(this, 
      "Google Places API connection failed with error code:" + 
        connectionResult.getErrorCode(), 
      Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case PERMISSION_REQUEST_CODE: 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       callPlaceDetectionApi(); 
      } else { 
       Toast.makeText(getApplicationContext(), 
         "SMS faild, please try again.", Toast.LENGTH_LONG).show(); 
       return; 
      } 
      break; 

    } 
} 

private void callPlaceDetectionApi() throws SecurityException { 
    PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi 
      .getCurrentPlace(mGoogleApiClient, null); 
    result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() { 
     @Override 
     public void onResult(PlaceLikelihoodBuffer likelyPlaces) { 
      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(); 
      } 
      likelyPlaces.release(); 
     } 
    }); 
} 
} 

任何人都可以請提出一個方法來檢查發送的消息數。我只想要一個text message發送button點擊。

謝謝:)

回答

0

您正在爲多個地點發送消息,而是可以採用最佳匹配並只發送一條消息,或者只需發送消息即可中斷循環。

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(); 
//    } 

      } 
     }); 
    } 
+0

您給出的代碼崩潰並拋出一個「異常」,但在發送消息之後放置中斷。十分感謝你的幫助!! –

0

用下面的函數發送短信只有一個號碼;

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); 
    } 
} 
+0

我可以使用'getApplicationContext()'代替'getActivity()'嗎? –

+0

是.... getActivity將在片段中使用...對於一個活動你必須使用getApplicationContext() –

+0

請upvote並打勾我的問題,如果它真的有幫助你 –

1

我認爲它的發生,因爲likelyPlaces可以包含很多地方,你把短信的方式將likelyPlaces循環中。因此,它會發送每個可能的地區數據的短信。如果它包含40個地方,它會發送40次。

+0

因此,任何想法,我應該什麼做?就像我試圖創建一個單獨的方法來發送短信,但仍然不起作用 –

+0

如果你只是想發送一個短信,只是放置休息; Toast.makeText(getApplicationContext(),「SMS sent。」, Toast.LENGTH_LONG).show(); – Kharda

+0

如果有幫助,請將答案標記爲已接受。 – Kharda