简体   繁体   English

如何从Android按钮单击运行单独的应用程序

[英]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. 我尝试在我的Android应用程序中添加两个按钮,从单独的两个应用程序订购系统和库存系统中选择一个应用程序。如图所示。

在此输入图像描述

I have implemented these two applications as separate two Android projects. 我已将这两个应用程序实现为单独的两个Android项目。 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). 我已将Order系统和Inventory系统项目添加到第一个应用程序的构建路径,然后导入它们的包(com.oms.ws和com.inv.ws)。 This may be incorrect, but don't know how to do this. 这可能不正确,但不知道如何做到这一点。 Please help me! 请帮我! I'm new to Android. 我是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. 将org.abc替换为您要启动的应用程序的包名称。

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和链接项目的帮助下,在我自己的应用程序中做了类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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