2014-12-26 1 views
-3

나는 SMS를 통해 내 GPS 좌표를 보내야하는데, 나는 첫째로 SMS 보낸 프로그램을 만들었다. 그 후 GPS 좌표 코드를 찾았습니다. 이 코드를 연결해야합니다. 버튼을 클릭하면 SMS를 보내야합니다. 의도적으로 시도했지만 문제가 있습니다. 내가 버튼, 가까운 프로그램 정지를 .. 클릭하면 여기 안드로이드에서 인 텐트를 사용하는 방법, 나는 2 개의 작업 프로그램을 가지고 결합 할 수 없다.

여기 내 activity_main.xml

<TextView 
    android:id="@+id/choice" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="coarse accuracy selected (default)" 
    android:layout_below='@+id/fineAccuracy' 
    /> 

<CheckBox 
    android:id="@+id/fineAccuracy" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="fine accuracy" 
    android:layout_below='@+id/chooseRadio' 

    /> 

<Button 
    android:id="@+id/chooseRadio" 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="OK" 
    android:layout_below='@+id/prov' 
    /> 

<TextView 
    android:id="@+id/prov" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="No provider selected yet" 
    android:layout_marginTop="10dp" 
    android:textSize="20dp" 
    android:layout_below='@+id/lat' 
    /> 

<TextView 
    android:id="@+id/lat" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="20dp" 
    android:text="Latitude: -" 
    android:textSize="20dp" 
    android:layout_below="@+id/lon" 
    /> 

<TextView 
    android:id="@+id/lon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:text="Longitude: -" 
    android:textSize="20dp" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="number" 
    android:ems="11" 
    android:id="@+id/telNo" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/choice" 
    android:hint="Telefon Numarasını Giriniz." 
    /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Konumunu SMS Gönder" 
    android:id="@+id/button1" 
    android:layout_below="@+id/telNo" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    /> 

+0

여기에 MainActivity.java –

+0

.java 코드와 로그캣 출력을 보내십시오. – PPD

+0

.java 코드와 logcat을 보내 주셔서 감사합니다. –

답변

0
package com.example.oguz.gpssms; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.provider.Settings; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    private TextView latitude; 
    private TextView longitude; 
    private TextView choice; 
    private CheckBox fineAcc; 
    private Button choose; 
    private TextView provText; 
    private LocationManager locationManager; 
    private String provider; 
    private MyLocationListener mylistener; 
    private Criteria criteria; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button button1 = (Button) findViewById(R.id.button1); 
     button1.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(MainActivity.this, SmsActivity.class); 
       startActivity(intent); 
      } 
     }); 
     latitude = (TextView) findViewById(R.id.lat); 
     longitude = (TextView) findViewById(R.id.lon); 
     provText = (TextView) findViewById(R.id.prov); 
     choice = (TextView) findViewById(R.id.choice); 
     fineAcc = (CheckBox) findViewById(R.id.fineAccuracy); 
     choose = (Button) findViewById(R.id.chooseRadio); 
     // Get the location manager 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     // Define the criteria how to select the location provider 
     criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_COARSE); //default 
     // user defines the criteria 
     choose.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if(fineAcc.isChecked()){ 
        criteria.setAccuracy(Criteria.ACCURACY_FINE); 
        choice.setText("fine accuracy selected"); 
       }else { 
        criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
        choice.setText("coarse accuracy selected"); 
       } 
      } 
     }); 
     criteria.setCostAllowed(false); 
     // get the best provider depending on the criteria 
     provider = locationManager.getBestProvider(criteria, false); 
     // the last known location of this provider 
     Location location = locationManager.getLastKnownLocation(provider); 
     mylistener = new MyLocationListener(); 
     if (location != null) { 
      mylistener.onLocationChanged(location); 
     } else { 
      // leads to the settings because there is no last known location 
    //  Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
    //  startActivity(intent); 
     } 
     // location updates: at least 1 meter and 200millsecs change 
     locationManager.requestLocationUpdates(provider, 200, 1, mylistener); 
    } 
    private class MyLocationListener implements LocationListener { 
     @Override 
     public void onLocationChanged(Location location) { 
      // Initialize the location fields 
      latitude.setText("Latitude: "+String.valueOf(location.getLatitude())); 
      longitude.setText("Longitude: "+String.valueOf(location.getLongitude())); 
      provText.setText(provider + " provider has been selected."); 
      Toast.makeText(MainActivity.this, "Location changed!", 
        Toast.LENGTH_SHORT).show(); 
     } 
     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      Toast.makeText(MainActivity.this, provider + "'s status changed to "+status +"!", 
        Toast.LENGTH_SHORT).show(); 
     } 
     @Override 
     public void onProviderEnabled(String provider) { 
      Toast.makeText(MainActivity.this, "Provider " + provider + " enabled!", 
        Toast.LENGTH_SHORT).show(); 
     } 
     @Override 
     public void onProviderDisabled(String provider) { 
      Toast.makeText(MainActivity.this, "Provider " + provider + " disabled!", 
        Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

인 SmsActivity.java

입니다
package com.example.oguz.gpssms; 

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.net.Uri; 
import android.os.BatteryManager; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

/** 
* Created by OGUZ on 26.12.2014. 
*/ 
public class SmsActivity extends MainActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button button1 = (Button) findViewById(R.id.button1); 
     EditText editText = (EditText)findViewById(R.id.telNo); 

     final String telNo = editText.getText().toString(); 
     button1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Uri uri = Uri.parse("smsto:" +telNo); 
       Intent intent = new Intent(Intent.ACTION_SENDTO,uri); 
       intent.putExtra("sms_body","Merhaba"); 
       startActivity(intent); 

       finish(); 
      } 
     }); 

    }; 
} 
0

먼저 Androidmanifest 파일에서 SmsActivity 클래스를 선언하고 button1의 onclick이 SmsActivity에이 변수 2 위도와 경도를 전달합니다. 온라인 문서 읽기 활동 사이에 데이터를 전달하는 방법과 매니 페스트에서 활동을 추가하는 방법

+0

지금 작동 중입니다. intent.putExtra ("sms_body", "Merhaba"); ,,,,,,, "Merhaba"대신 lon과 lat 값을 써야합니다 ... 어떻게 만들 수 있습니까? –

관련 문제