2017-10-20 74 views
0

我想建立一個應用程序,用戶可以將他/她的location發送到sqlitedatabase中存儲的電話號碼。我測試了應用程序,用戶可以將文本信息location發送到一個電話號碼,但它現在可以工作,但現在當我嘗試創建一個list的數字並將其作爲sendTextMessage方法smsManagerlocation作爲參數傳遞給一個文本消息不發送。我已經嘗試了以下至今給定的代碼,短信沒有被髮送到一個號碼列表,但只有一個號碼被指定時發送

代碼

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 display; 
    private Button location_button,contacts_button; 
    //String number="xxxxxxxxxx"; 
    ArrayList<String> numbers; 
    SQLiteDatabase db; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_gps4); 
     numbers=new ArrayList<>(); 
     db = new UserDatabase(this).getReadableDatabase(); 
     location_button=(Button)findViewById(R.id.show_button); 
     contacts_button=(Button)findViewById(R.id.view_button); 
     display=(TextView)findViewById(R.id.location_textview); 

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

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

     contacts_button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent=new Intent(Gps4Activity.this,DetailsActivity.class); 
       startActivity(intent); 
      } 
     }); 
    } 

    public ArrayList<String> getContacts(){ 

     Cursor cursor=db.rawQuery("SELECT * FROM "+UserDatabase.TABLE_NAME,null); 
     while (cursor.moveToNext()){ 
      String contact=cursor.getString(cursor.getColumnIndex(UserDatabase.NUMBER)); 
      numbers.add(contact); 
     } 
     return numbers; 
    } 

    @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())); 
        display.setText(placeLikelihood.getPlace().getAddress().toString()); 
        messageSending(placeLikelihood.getPlace().getAddress().toString()); 
        break; 
       } 
       likelyPlaces.release(); 

      } 
     }); 
    } 

    public void messageSending(String message){ 
     SmsManager smsManager = SmsManager.getDefault(); 
//  smsManager.sendTextMessage(number, null, message, null, null); 
     getContacts(); 
     smsManager.sendTextMessage(String.valueOf(numbers),null,message,null,null); 
     Toast.makeText(getApplicationContext(), "SMS sent."+String.valueOf(numbers), 
       Toast.LENGTH_LONG).show(); 
    } 
} 

註釋行是那些當我試圖只用一個電話號碼來測試應用程序。另外,假設最初在sqlitedatabase中只有一個電話號碼,因爲我多次點擊location_button,這是arraylist多次增長的大小。例如,最初如果arraylist有元素[xxxxxx]下一次我點擊location_buttonarraylist現在將有[xxxxxx,xxxxxx]

任何人都可以幫我解決這個問題嗎?

+0

'SmsManager'方法一次只能處理一個數字。你必須一一發送。 –

+0

是不是有一種方法可以同時向多個號碼發送短信 –

+1

您可以使用循環發送短信嗎? –

回答

0

由於sendTextMessage()方法一次只能取一個數字,所以您需要爲列表中的每個數字執行此方法。

這就是說你應該'循環'通過List。就像這樣:

for (String number : numbers) { 
    smsManager.sendTextMessage(numbers, null, message, null, null); 
} 

什麼它基本上是說的是:

「還好吧,讓我們一個號碼,sendTextMessage一把手,我將繼續這樣做,直到我做了。」

+0

Thnaks的幫助 –

相關問題