[英]how to call a method of a main_activity from a normal class in android
[英]Android call 2 or more classes from main activity
我是Android新手。 我有主要活动,需要从中调用不同的类来执行不同的功能。 但是,只要我同时调用两个类,就只会调用最后一个意图。 有人可以建议一种在一项活动中同时调用两个或多个班级的方法。 谢谢
下面是我的代码示例
主要活动
public class MainActivity extends AppCompatActivity {
public Api mApi;
Button data;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = (Button) findViewById(R.id.button2);
//calling sygic truck api and implementing sygic call back
mApi = Api.init(getApplicationContext(), "com.sygic.truck", "com.sygic.truck.SygicService", mApiCallback);
//connecting sygic app
mApi.connect();
data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(getApplicationContext(), locationInfo.class);
Intent intent2 = new Intent(getApplicationContext(), routeInfo.class);
Intent[] list = new Intent[2];
list[0] = intent1;
list[1] = intent2;
startActivities(list);
}
});
}
}
locationInfo类
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();
}
}
}
routeInfo类
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();
}
}
}
Android清单
<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>
好,
startActivities()
用于构造所谓的合成后置堆栈。
在应用中,当您从一种活动转移到另一种活动而没有调用
finish()
在任何活动中,您都将构造一个称为“ backstack”的东西。 之所以称为后退堆栈,是因为当您按住后退按钮时,将恢复活动的堆栈。
如果您想人为地创建一个backstack,您将
-startActivities()
这是人为地将除最后一个意图的活动以外的所有内容人为地插入到了Backstack中。
如果您想运行activit 1然后运行activity2,只需调用
startActivity()
分别放在他们身上
如果希望2任务并行运行,则需要使用“ AsyncTask”。 这里的文学
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.