在不同的android项目eclipse中使用类文件(Using class file in different android project eclipse)

编程入门 行业动态 更新时间:2024-10-24 16:31:04
在不同的android项目eclipse中使用类文件(Using class file in different android project 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项目,使用配置构建路径 - >库 - >添加类文件夹添加了这些类文件,该文件夹在Re​​ferenced库中可见并且可见。 现在我的目标和问题是,如果我可以运行这个新项目,并让它向我显示一个位置,而不添加实际的Java代码(整个MainActivity.java),但只能通过引用它到类文件。 我可以这样做吗? 如果是的话怎么样?

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

i'm new to eclipse and programming but have a university project i need help with. I created an Android Application Project that locate your position using GPS (the code found online actually and works perfectly).

This is my 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 } }

The project will generate class files as BuildConfig.class MainActivity.class and R.class. I created a different new android project, added these class files using Configure Build Path -> libraries -> add class folder, which works and are visible in Referenced libraries. Now my goal and question is if i can run this new project and make it show me a location without adding the actual java code (the whole MainActivity.java) but only by referencing it to the class files. Can i do this? and if yes how to ?

PS: i actually tested a small Java project that only shows an integer; then added its class file into another java project and ran this new project and it worked but this seems more complicated when using a complex android app project.

最满意答案

你可以做两件事

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

You can do two things

If you dont want the java code, Make a jar from the project you want to use and put it in the libs folder Mark one project as library project and use it

更多推荐

本文发布于:2023-08-05 05:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427261.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   项目   eclipse   android   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!