日食とプログラミングは初めてですが、大学のプロジェクトで助けが必要です。GPS を使用して位置を特定する Android アプリケーション プロジェクトを作成しました (コードは実際にオンラインで見つかり、完全に機能します)。
これは私の MainActivity.java です:
package com.example.locationfromgps;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener{
LocationManager locationManager ;
String provider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting LocationManager object
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Creating an empty criteria object
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
provider = locationManager.getBestProvider(criteria, false);
if(provider!=null && !provider.equals("")){
// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 1, this);
if(location!=null)
onLocationChanged(location);
else
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
// Getting reference to TextView tv_longitude
TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude);
// Getting reference to TextView tv_latitude
TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);
// Setting Current Longitude
tvLongitude.setText("Longitude:" + location.getLongitude());
// Setting Current Latitude
tvLatitude.setText("Latitude:" + location.getLatitude() );
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
プロジェクトは、BuildConfig.class MainActivity.class および R.class としてクラス ファイルを生成します。別の新しい Android プロジェクトを作成し、[ビルド パスの構成] -> [ライブラリ] -> [クラス フォルダーの追加] を使用してこれらのクラス ファイルを追加しました。これは機能し、参照ライブラリに表示されます。私の目標と質問は、この新しいプロジェクトを実行して、実際の Java コード (MainActivity.java 全体) を追加せずに、クラス ファイルを参照するだけで場所を表示できるかどうかです。私はこれを行うことができますか?はいの場合、どのように?
PS: 整数のみを表示する小さな Java プロジェクトを実際にテストしました。次に、そのクラス ファイルを別の Java プロジェクトに追加し、この新しいプロジェクトを実行しましたが、機能しましたが、複雑な Android アプリ プロジェクトを使用する場合、これはより複雑に見えます。