【问题标题】:Android call 2 or more classes from main activityAndroid 从主要活动中调用 2 个或更多类
【发布时间】:2016-03-22 21:17:14
【问题描述】:

我是安卓新手。我有主要活动,我需要从中调用不同的类来执行不同的功能。但是每当我同时调用两个类时,只会调用最后一个意图。有人可以建议一种在活动中同时调用 2 个或更多类的方法。谢谢

下面是我的代码示例

主要活动

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>

【问题讨论】:

  • 在最后一个 Activity 启动后按下返回按钮会发生什么?据我所知,startActivities() 仅用于构建合成回栈,而不是串行启动活动 此链接有更多信息:here
  • 嗨,Srinivas,在按下返回按钮时,它会调用第一意图。有什么方法可以同时调用吗?
  • 也是同时定义两个类。在任何给定时间点,只有 1 个活动可以位于堆栈顶部。 startActivities 不会启动 2 个活动并让它们同时运行
  • 那么当你在栈顶时按回键会让你回到主活动?
  • 拒绝第一个意图,即 locationInfo 类

标签: java android listview android-intent android-activity


【解决方案1】:

好的,

startActivities()

用于构造称为合成后栈的东西。

在一个应用中,当你从一个活动移动到另一个而不调用一个活动时

finish()

在任何活动中,您都构建了称为“backstack”的东西。它被称为后退堆栈,因为它是在您按住后退按钮时恢复的活动堆栈。

如果你想人为地创建一个backstack,你会

-startActivities()

这样做是人为地将除最后一个意图的活动之外的所有内容插入到后台堆栈中。

如果你想运行 activit 1 然后运行 ​​activity2 打个电话

startActivity()

分别

如果您希望这 2 个任务并行运行,您需要使用称为“AsyncTask”的东西。文学here

【讨论】:

  • 我认为异步任务通常用于获取或发送某些内容到 url 链接
  • 这不是唯一的用途。异步任务是您不会阻塞 UI 线程的东西。例如,正如您所指出的,如果在主线程上完成网络调用可能会阻塞,因此它们是使用异步任务完成的。您还可以使用 Android 处理程序和线程池变体。但是异步任务也应该可以工作
  • 你可以为我的案例发布一个异步任务的例子吗,因为我发现其他的都是 url 链接?
  • This 灵魂帮助。 “doInBackground”方法在另一个线程中运行所有内容,重构代码以适应 AsyncTask 设计
  • @GauravRam 有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
  • 1970-01-01
  • 2012-06-28
  • 2022-01-25
  • 1970-01-01
  • 2011-10-18
相关资源
最近更新 更多