2017-08-05 7 views
0

私はテキストメッセージで私の場所を共有するアプリケーションを構築しようとしています。 activityでは、1つはbutton、もう1つはTextViewです。 buttonをクリックすると、指定された番号にtext messageが送信されます。問題は、一度buttonをクリックすると、アプリケーションをアンインストールしない限り、message送信が停止しないということです。一度に、約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(); 
     } 
    }); 
} 
} 

。私はちょうどbuttonクリックで送信されるtext messageします。

ありがとうございました:)

答えて

0

あなたは複数の場所でメッセージを送信していますが、最高のマッチを取ってこのようなメッセージを1つだけ送信することもできます。

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

あなたが与えたコードは 'exception'をスローしますが、メッセージを送信してからブレークします。助けてくれてありがとう! –

0

以下の機能を使用して、テキストメッセージを1つの番号だけ送信します。

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

'getActivity()'の代わりに 'getApplicationContext()'を使用できますか? –

+0

はい.... .... getActivityはフラグメントで使用されます...アクティビティの場合はgetApplicationContext()を使用する必要があります –

+0

お願いします。私の質問には本当に役に立ちますか? –

1

私はlikelyPlacesは多くの場所を含めることができるので、それが起こって考えると、あなたはlikelyPlacesループ内のSMS方法を置きます。したがって、すべての可能なPlacesデータに対してsmsを送信します。それは40の場所が含まれている場合、40回送信されます。

+0

行う?私はテキストメッセージを送信する別個のメソッドを作成しようとしたように、まだ動作しません –

+0

あなただけのSMSを送信したい場合は、 Toast.makeText(getApplicationContext()、 "SMS sent。"、 Toast.LENGTH_LONG).show(); – Kharda

+0

それが役に立ったら、回答を受け入れたものとしてマークしてください。 – Kharda

関連する問題