我是android新手。我有一个主要的活动,我需要调用不同的类来执行不同的功能。但是每当我同时调用两个类时,只调用最后一个意图。有人可以建议在活动中同时调用2个或更多课程的方法。谢谢
以下是我的代码示例
主要活动
public class locationInfo extends AppCompatActivity {
public Api mApi;
public TextView coordinates;
public String currentLocation = "";
public String longitude;
public String latitude;
public Button data;
public String altitude, speed;
public int speedLimit;
//calling gps class from sygic lib
GpsPosition gpsPos = new GpsPosition();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coordinates = (TextView) findViewById(R.id.textView);
// sygicGetData();
try {
Log.v("debug", "Location is...1" );
boolean satInfo = true;
int maxTime = 0;
gpsPos = ApiNavigation.getActualGpsPosition(satInfo, maxTime);
longitude = String.valueOf(gpsPos.getLongitude());
latitude = String.valueOf(gpsPos.getLatitude());
altitude = String.valueOf(gpsPos.getAltitude());
speed = String.valueOf(gpsPos.getSpeed());
speedLimit = ApiInfo.getCurrentSpeedLimit(maxTime);
Log.v("debug", "Location is...2" );
Position pos = new Position();
pos.setPosition(gpsPos.getLongitude(), gpsPos.getLatitude());
currentLocation = ApiLocation.getLocationAddressInfo(pos, maxTime);
coordinates.setText("\n" + "Current Location:" + currentLocation + "\n" + "Longitude:" + longitude + "\n" + "Latitude:" + latitude +
"\n" + "Altitude:" + altitude + "\n" + "Current Speed:" + speed +
"\n" + "Speed Limit:" + speedLimit);
} catch (GpsException e) {
Log.e("GpsPosition", "Error code:" + e.getCode());
} catch (InvalidLocationException e) {
Log.e("Location", "Error code:" + e.getCode());
} catch (GeneralException e) {
e.printStackTrace();
}
}
}
locationInfo类
public class routeInfo extends AppCompatActivity {
public String remainingDistance, remainingTime;
public String totalDistance, totalTime;
public Button data;
public TextView coordinates;
public RouteInfo routeInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coordinates = (TextView)findViewById(R.id.textView2);
// sygicGetData();
try {
boolean info = true;
int maxTime = 0;
routeInfo = ApiNavigation.getRouteInfo(info, maxTime);
totalDistance = String.valueOf(routeInfo.getTotalDistance());
remainingDistance = String.valueOf(routeInfo.getRemainingDistance());
totalTime = String.valueOf(routeInfo.getTotalTime());
remainingTime = String.valueOf(routeInfo.getRemainingTime());
coordinates.setText("\n" + "Total Travel Distance:" + totalDistance + "\n" + "Remaining Travel Distance:" + remainingDistance + "\n" + "Total Travel Time:" + totalTime + "\n" +
"Remaining Travel Time:" + remainingTime + "\n");
} catch (GpsException e) {
Log.e("GpsPosition", "Error code:" + e.getCode());
} catch (InvalidLocationException e) {
Log.e("Location", "Error code:" + e.getCode());
} catch (GeneralException e) {
e.printStackTrace();
}
}
}
routeInfo类
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jackfruit.sygicdata4">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".getters.locationInfo"></activity>
<activity android:name=".getters.routeInfo"></activity>
</application>
</manifest>
Android清单
{{1}}
答案 0 :(得分:0)
确定,
startActivities()
用于构造称为合成后向堆栈的东西。
在应用程序中,当您从1个活动移动到另一个活动而不调用
时finish()
在您构建称为“backstack”的任何活动中。它被称为后台堆栈,因为当您持续按下后退按钮时,它是一堆活动。
如果你想人为地制作一个后座,你会
-startActivities()
这样做是人为地将最后一个意图的活动插入到后台堆栈中。
如果您想运行activit 1然后运行activity2 只需致电
startActivity()
单独
如果你想让2个任务并行运行,你需要使用一个名为“AsyncTask”的东西。文献here