0

gpstracking.javaからsetmessage関数を呼び出して、新しいインテントにtextview値を設定していますが、実行しても何も表示されません。何がエラーなのかわかりません。コードを投稿しています。setTextを使用して値を設定しています。緯度と経度をパラメーターとして関数に送信しています。

gpstracking.java

package com.example.gpstracking;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AndroidGPSTrackingActivity extends Activity {  
    Button btnShowLocation;

    // GPSTracker class
    GPSTracker gps;
    List<Address> addresses;
    @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
       final Intent i;
       i=new Intent(this,SMS.class);
        // show location button click event
        btnShowLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {        
                // create class object
                gps = new GPSTracker(AndroidGPSTrackingActivity.this);

            // check if GPS enabled     
                if(gps.canGetLocation()){

                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();

                    // \n is for new line   
                Geocoder gcd = new Geocoder(AndroidGPSTrackingActivity.this, Locale.getDefault());
                    try{
                    List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);

                    if (addresses.size() > 0) 
                    {
                        SMS.setmessage(latitude,longitude);
                    }
                    }catch(Exception e)
                    {
                        System.out.print(e);
                    }
                }else{
                    // can't get location
                    // GPS or Network is not enabled
                    // Ask user to enable GPS/network in settings
                    gps.showSettingsAlert();
                }
                startActivity(i);
            }
        });
    }

}

SMS.java

    package com.example.gpstracking;
import com.example.gpstracking.R;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SMS extends Activity {
    Button btnSendSMS;
    EditText phone_number;
    static TextView message;
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smsmain);
        btnSendSMS = (Button) findViewById(R.id.sendmessage);
        phone_number= (EditText) findViewById(R.id.phone_number);
        message=(TextView) findViewById(R.id.message);
        btnSendSMS.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                String phoneNo = phone_number.getText().toString();
                String msg = message.getText().toString();
                if (phoneNo.length()>0)// && message.length()>0)                
                    sendSMS(phoneNo, msg);                
                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }
        });  
            }               


private void sendSMS(String phoneNumber, String message)
{      
    /*
    PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, test.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(){
        @SuppressWarnings("deprecation")
        @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.SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                break;
            case android.telephony.SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(getBaseContext(), "No service", 
                        Toast.LENGTH_SHORT).show();
                    break;
                case android.telephony.SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case android.telephony.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));        

    @SuppressWarnings("deprecation")
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);               
} 
public static void setmessage(double latitude,double longitude)
{

    message.setText("my lat long is"+latitude+longitude);
}
}
4

1 に答える 1

0

これを試して、緯度と経度を次の活動に入れて取得します。

Intent I = new Intent(AndroidGPSTrackingActivity.this, SMS.class);
I.putExtra("latitude", latitude);
I.putExtra("longitude", longitude);
startActivity(I);

次のクラスのためにこの緯度と経度を取得します。

Intent i = getIntent();
lat = i.getStringExtra("latitude");
lng = i.getStringExtra("longitude");

あなたのコードを編集しました:

public class AndroidGPSTrackingActivity extends Activity {  
Button btnShowLocation;

// GPSTracker class
GPSTracker gps;
List<Address> addresses;
@Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnShowLocation = (Button) findViewById(R.id.btnShowLocation);

    // show location button click event
    btnShowLocation.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {        
            // create class object
            gps = new GPSTracker(AndroidGPSTrackingActivity.this);

        // check if GPS enabled     
            if(gps.canGetLocation()){

                double latitude = gps.getLatitude();
                double longitude = gps.getLongitude();

                // \n is for new line   
            Geocoder gcd = new Geocoder(AndroidGPSTrackingActivity.this, Locale.getDefault());
                try{
                List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);

                if (addresses.size() > 0) 
                {
                    SMS.setmessage(latitude,longitude);
                }
                }catch(Exception e)
                {
                    System.out.print(e);
                }
            }else{
                // can't get location
                // GPS or Network is not enabled
                // Ask user to enable GPS/network in settings
                gps.showSettingsAlert();
            }
             final Intent i;
   i=new Intent(this,SMS.class);
   i.putExtra("latitude", latitude);
i.putExtra("longitude", longitude);
            startActivity(i);
        }
    });

}

そして、 SMS.class 内で緯度と経度を取得しonCreateます。あなたは価値を得るでしょう

于 2013-02-15T07:04:26.293 に答える