Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从其他应用程序启动应用程序';s的入口点android_C#_Android_Xamarin.android - Fatal编程技术网

C# 从其他应用程序启动应用程序';s的入口点android

C# 从其他应用程序启动应用程序';s的入口点android,c#,android,xamarin.android,C#,Android,Xamarin.android,我有xamarin.android项目Client1和CommonApp项目(在同一个解决方案中),它们都有通用代码。从Client1项目我需要启动CommonApp,我的代码在Client1应用程序中这样做 public class MainActivity : AppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(saved

我有xamarin.android项目Client1和CommonApp项目(在同一个解决方案中),它们都有通用代码。从Client1项目我需要启动CommonApp,我的代码在Client1应用程序中这样做

public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);
        FindViewById<Button>(Resource.Id.btn1).Click += delegate {

            //Starting CommonApp project
            StartActivity(new Intent(Application.Context, typeof(CommonApp.MainActivity)));
        };
    }
}
CommonApp活动\u主要有

 <TextView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:text="This is CommonApp">
</TextView>

说明:我有一个带有2个android应用程序的解决方案。1个通用应用程序和第二个客户端1。Cleint1是具有CommonApp类型的.dll的启动项目。从Cleint1应用程序中,我通过导入CommonApp命名空间启动CommonApp。我可以有很多项目Client1、Client2、Client3……等等,它们都有自己的图标、应用程序名、闪屏和google-service.json。当我在启动时设置Client2时,它将成为Client2项目&CommonApp中提供的所有通用代码

要求

输出屏幕截图

应用程序日志

更新截图:


若您知道另一个应用程序的包名,则可以通过调用intent来实现您的目标

String appName = "appName";
String packageName = "package.of.another.application";
openApp(context, appName, packageName);

public static void openApp(Context context, String appName, String packageName) {
    if (isAppInstalled(context, packageName))
        if (isAppEnabled(context, packageName))
            context.startActivity(context.getPackageManager().getLaunchIntentForPackage(packageName));
        else Toast.makeText(context, appName + " app is not enabled.", Toast.LENGTH_SHORT).show();
    else Toast.makeText(context, appName + " app is not installed.", Toast.LENGTH_SHORT).show();
}

private static boolean isAppInstalled(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException ignored) {
    }
    return false;
}

private static boolean isAppEnabled(Context context, String packageName) {
    boolean appStatus = false;
    try {
        ApplicationInfo ai = context.getPackageManager().getApplicationInfo(packageName, 0);
        if (ai != null) {
            appStatus = ai.enabled;
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return appStatus;
}

确保已安装您的常用应用程序,但未安装。如果已经安装,您可以尝试此方法

 Intent intent = PackageManager.GetLaunchIntentForPackage("Com.CommonAPP.CommonAPP");
 if (intent != null)
 {
    StartActivity(intent);
 }

希望这能对您有所帮助。

我认为CommonApp活动实际上已经启动(如调试器所示),但它没有使用正确的布局文件。
MainActivity
s(来自CommonApp的和来自Client1的)都使用名为
activity\u main
的布局。因此,两个项目都包含一个
activity\u main.xml
文件,似乎其中一个(来自Client1项目)覆盖了另一个

资源没有名称空间,您需要重命名布局(以及您希望保持不同的任何其他资源)以避免冲突。您可以添加前缀,例如
common\u activity\u main
client\u activity\u main

已编辑(评论中的其他信息):


包含公共部分的项目应该是Android类库,而不是Android应用程序。否则它似乎可以工作(没有生成错误),但布局文件不在APK中。

您在日志中得到了什么?Vic,我已经附加了日志链接。看看吧,这与安装普通应用程序无关。单个应用程序本身包含CommonApp。单个解决方案可能包含多个项目。未经安装,您无法启动另一个应用程序(例如:commonApp)。单个解决方案可以有多个项目,但每个项目只有commonApp,因此一次只有一个应用程序设置为启动并声明commonApp。Getting exception
PackageManager.NameNotFoundException
。注意:我没有单独安装CommonApp,单客户端应用程序本身内部有CommonApp。当你说CommonApp时,你是指单独的应用程序还是其他页面?你的CommonApp.MainActivity不是以与我们打开其他活动相同的方式打开的吗。通过使用IntentI,我无法获得您的要求。你能解释一下你的要求吗?你现在有不同的活动名称和不同的布局名称,但它不起作用?会发生什么?是的,我有不同的活动名称&不同的xaml文件名,但行为仍然相同,我的意思是不工作。第一条评论是打印失败。您可以看到调试程序调用了CommonMainActivity中的
OnCreate()
,并将
SetContentView()
与类似“common\u activity\u main”的内容一起使用,它仍然显示client1的外观。您清理/重建了整个解决方案吗?也许有点愚蠢,但根据我的经验,Xamarin有时会错过资源更改。在做了一些测试后,如果公共项目是类库,而不是应用程序,它就会工作
 Intent intent = PackageManager.GetLaunchIntentForPackage("Com.CommonAPP.CommonAPP");
 if (intent != null)
 {
    StartActivity(intent);
 }