2015-04-14 74 views
-1

快速的問題,我有我的班級設置和我的短信發送,我沒有任何錯誤。我試圖發送我的位置作爲短信的一部分,作爲谷歌地圖鏈接。問題是我的消息正在發送,但位置鏈接沒有通過?但該文件沒有錯誤並且消息傳遞正常。發送SMS消息問題的位置?

任何想法?此外,我還在我的Manifest文件中包含了「訪問精確位置」。

類別:

Button btnSendSMS; 
EditText txtPhoneNo; 
EditText txtMessage; 
String message; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_send_sms); 
    btnSendSMS = (Button) findViewById(R.id.btnSendSMS); 
    txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo); 
    txtMessage = (EditText) findViewById(R.id.txtMessage); 


    btnSendSMS.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      String phoneNo = txtPhoneNo.getText().toString(); 
      String message = txtMessage.getText().toString(); 
      displayLocation(); 

      if (phoneNo.length()>0 && message.length()>0) 
       sendSMS(phoneNo, message); 
      else 
       Toast.makeText(getBaseContext(), 
         "Please enter both phone number and message.", 
         Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 


    private void displayLocation(){ 
     LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, new LocationListener(){ 
      @Override 
      public void onStatusChanged(String s, int i, Bundle bundle) {} 
      @Override 
      public void onProviderEnabled(String s) {} 
      @Override 
      public void onProviderDisabled(String s) {} 
      @Override 
      public void onLocationChanged(final Location location) {} 
     }); 
     Location myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 
     double longitude = myLocation.getLongitude(); 
     double latitude = myLocation.getLatitude(); 
     message+="https://www.google.co.id/maps/@"+latitude+","+longitude; 
} 





//---sends a SMS message to another device--- 
private void sendSMS(String phoneNumber, String message) 
{ 

    PendingIntent pi = PendingIntent.getActivity(this, 0, 
      new Intent(this, Home.class), 0); 
     SmsManager sms = SmsManager.getDefault(); 
     sms.sendTextMessage(phoneNumber, null, message, pi, null); 


    String SENT = "SMS_SENT"; 
    String DELIVERED = "SMS_DELIVERED"; 

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, 
      new Intent(SENT), 0); 

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, 
      new Intent(DELIVERED), 0); 

    //---when the SMS has been sent--- 
    registerReceiver(new BroadcastReceiver(){ 
     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch (getResultCode()) 
      { 
       case Activity.RESULT_OK: 
        Toast.makeText(getBaseContext(), "SMS sent", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case android.telephony.gsm.SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
        Toast.makeText(getBaseContext(), "Generic failure", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case android.telephony.gsm.SmsManager.RESULT_ERROR_NO_SERVICE: 
        Toast.makeText(getBaseContext(), "No service", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case android.telephony.gsm.SmsManager.RESULT_ERROR_NULL_PDU: 
        Toast.makeText(getBaseContext(), "Null PDU", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case android.telephony.gsm.SmsManager.RESULT_ERROR_RADIO_OFF: 
        Toast.makeText(getBaseContext(), "Radio off", 
          Toast.LENGTH_SHORT).show(); 
        break; 
      } 
     } 
    }, new IntentFilter(SENT)); 

    //---when the SMS has been delivered--- 
    registerReceiver(new BroadcastReceiver(){ 
     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch (getResultCode()) 
      { 
       case Activity.RESULT_OK: 
        Toast.makeText(getBaseContext(), "SMS delivered", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case Activity.RESULT_CANCELED: 
        Toast.makeText(getBaseContext(), "SMS not delivered", 
          Toast.LENGTH_SHORT).show(); 
        break; 
      } 
     } 
    }, new IntentFilter(DELIVERED)); 

    android.telephony.gsm.SmsManager smms = android.telephony.gsm.SmsManager.getDefault(); 
    smms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); 
} 
+0

有什麼問題?你沒有給我們提供有關問題的詳細信息。 – tnw

+0

@tnw我的不良只是編輯。基本上我的消息傳遞有效,但谷歌位置鏈接沒有通過我的消息,但沒有錯誤?和應用程序工作...任何想法? –

回答

0

其在displayLocation()設置的message變量是您的活性(在代碼的頂部聲明)的成員變量。所以當你這樣做時:

String message = txtMessage.getText().toString(); 
displayLocation(); 

該局部變量message沒有修改。

你可以做的是從displayLocation會返回一個字符串,並將其添加到消息:

private String displayLocation() { 
    //... 
    return "https://www.google.co.id/maps/@"+latitude+","+longitude; 
} 

String message = txtMessage.getText().toString() + displayLocation(); 
if (phoneNo.length()>0 && message.length()>0) 
//... 

而且我相信你應該,如果你不使用任何其他地方取出message類的成員變量。

+0

...抱歉有點不確定如何做到這一點,你可以請解釋我如何修改變量,使其工作?所有這一切都是新的。 –

+0

@AnkhitSharma看到我的更新 –