在不同的android项目eclipse中使用类文件

时间:2013-04-19 06:07:58

标签: java android eclipse

我是eclipse和编程的新手,但有一个我需要帮助的大学项目。 我创建了一个Android应用程序项目,使用GPS定位您的位置(实际在线找到的代码完美无缺)。

这是我的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项目,使用Configure Build Path添加了这些类文件 - >库 - >添加类文件夹,它在Referenced库中可见并可见。 现在我的目标和问题是,如果我可以运行这个新项目,并让它向我显示一个位置,而不添加实际的Java代码(整个MainActivity.java),但只能将其引用到类文件。 我能这样做吗?如果是的话怎么样?

PS:我实际测试了一个只显示整数的小型Java项目;然后将其类文件添加到另一个java项目并运行这个新项目并且它工作但是当使用复杂的Android应用程序项目时这似乎更复杂。

1 个答案:

答案 0 :(得分:0)

你可以做两件事

  • 如果您不想要java代码,请从您想要使用的项目中创建一个jar并将其放在libs文件夹中
  • 将一个项目标记为图书馆项目并使用它