简体   繁体   中英

How to run separate application from Android button click

I tried to add two buttons in my Android application to select an application from separate two applications Order system and Inventory system.As shown in the image.

在此输入图像描述

I have implemented these two applications as separate two Android projects. When I try to run this application it comes until to the the selecting window correctly, but when one button is pressed emulator shows "Force Close" message. I have added Order system and Inventory system projects to first application's build path and then import their packages(com.oms.ws and com.inv.ws). This may be incorrect, but don't know how to do this. Please help me! I'm new to Android. I want to test this application using the emulator!

Here is the code I have used to select applications.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.oms.ws.*;

 public class ThirdScreen extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.thirdscreen);

    Button oms;
    oms = (Button)findViewById(R.id.orderSystem); 
    oms.setOnClickListener(ordrMnagemntSys);

    Button inventory;
    inventory = (Button)findViewById(R.id.inventorySystem); 
    inventory.setOnClickListener(inventorySys);

}

private OnClickListener ordrMnagemntSys = new OnClickListener(){
    public void onClick(View v) {

            Intent oMs = new Intent(getApplicationContext(), com.oms.ws.TestOms.class);
            startActivity(oMs);
            }
};

private OnClickListener inventorySys = new OnClickListener(){
    public void onClick(View v) {

            Intent inven = new Intent(getApplicationContext(), com.inv.ws.TestInventory.class);
            startActivity(inven);
            }
};
}

Thanks!

Ok This works

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("org.abc");
startActivity(LaunchIntent);

Replace org.abc with package name of the application which you want to start.

try this instead:

String app = "com.inv.ws/TestInventory";
Intent intent = new Intent(Intent.ACTION_MAIN);             
intent.setComponent(ComponentName.unflattenFromString(app));             
intent.addCategory(Intent.CATEGORY_LAUNCHER);             
startActivity(intent); 

OR use this:

private void  launchComponent(String packageName, String name){
    Intent launch_intent = new Intent("android.intent.action.MAIN");
    launch_intent.addCategory("android.intent.category.LAUNCHER");
    launch_intent.setComponent(new ComponentName(packageName, name));
    launch_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    activity.startActivity(launch_intent);
}

这个 answear和链接项目的帮助下,在我自己的应用程序中做了类似的东西。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM