首页 文章

Android App无法部署到虚拟设备

提问于
浏览
0

Background:
我正在使用Xamarin.Forms创建一个内部移动应用程序,通过网络链接分发 . 我已经安装了Android和iOS几天,但请求是针对推送通知的 . 我们决定与Pushwoosh一起提供这项服务 .

我已经从pushwoosh复制了示例代码,并使用我们的包名和密钥对其进行了修改,但由于某种原因,我收到错误:
The application could not be started. Ensure that the application has been installed to the target device and has a launchable activity (MainLauncher = true). Additionally, check Build->Configuration Manager to ensure this project is set to Deploy for this configuration.

我还应该注意到Pushwoosh使用GCM,所以它也可能与此有关 .

What I've tried:
1.我的第一步显然是检查配置管理器 . 它确实设置为Deploy,并设置为编译x86(其他一些问题的其他答案修复)
2.我确认我的MainActivity.cs文件中的活动是使用MainLauncher = true设置的indead .
3.根据其他问题/答案的建议,我已从虚拟机器人中删除了该应用程序 . (实际上我完全重置了虚拟设备) .
4.正如一些GCM特定的QAs中所提到的,我已将我的包名改为全部小写 .

My relevant code:

[Activity(Label = "mobile", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[IntentFilter(new string[] { "company.MESSAGE" }, Categories = new string[] { "android.intent.category.DEFAULT" })]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
    LocalMessageBroadcastReceiver mMessageReceiver;
    LocalRegisterBroadcastReceiver mRegisterReceiver;

    bool mBroadcastPush = true;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        mMessageReceiver = new LocalMessageBroadcastReceiver();
        mMessageReceiver.activity = this;

        mRegisterReceiver = new LocalRegisterBroadcastReceiver();
        mRegisterReceiver.activity = this;
        registerReceivers();

        PushManager manager = PushManager.GetInstance(this);
        manager.OnStartup(this);

        //Register for push!
        manager.RegisterForPushNotifications();

        checkMessage(Intent);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }

    protected override void OnNewIntent(Intent intent)
    {
        checkMessage(intent);
    }

    public void checkMessage(Intent intent)
    {
        if (null != intent)
        {
            if (intent.HasExtra(PushManager.PushReceiveEvent))
            {
                doOnMessageReceive(intent.Extras.GetString(PushManager.PushReceiveEvent));
            }
            else if (intent.HasExtra(PushManager.RegisterEvent))
            {
                doOnRegistered(intent.Extras.GetString(PushManager.RegisterEvent));
            }
            else if (intent.HasExtra(PushManager.UnregisterEvent))
            {
                doOnUnregisteredError(intent.Extras.GetString(PushManager.UnregisterEvent));
            }
            else if (intent.HasExtra(PushManager.RegisterErrorEvent))
            {
                doOnRegisteredError(intent.Extras.GetString(PushManager.RegisterErrorEvent));
            }
            else if (intent.HasExtra(PushManager.UnregisterErrorEvent))
            {
                doOnUnregistered(intent.Extras.GetString(PushManager.UnregisterErrorEvent));
            }

            resetIntentValues();
        }
    }

    public void doOnRegistered(String registrationId)
    {
        // code to run if device has succesfully registered
    }

    public void doOnRegisteredError(String errorId)
    {
        // code to run if device failed to register
    }

    public void doOnUnregistered(String registrationId)
    {
        // code to run if device has succesfully unregistered
    }

    public void doOnUnregisteredError(String errorId)
    {
        // code to run if device failed to unregister properly
    }

    public void doOnMessageReceive(String message)
    {
        // code to run when device receives notification
    }
    private void resetIntentValues()
    {
        Intent mainAppIntent = Intent;

        if (mainAppIntent.HasExtra(PushManager.PushReceiveEvent))
        {
            mainAppIntent.RemoveExtra(PushManager.PushReceiveEvent);
        }
        else if (mainAppIntent.HasExtra(PushManager.RegisterEvent))
        {
            mainAppIntent.RemoveExtra(PushManager.RegisterEvent);
        }
        else if (mainAppIntent.HasExtra(PushManager.UnregisterEvent))
        {
            mainAppIntent.RemoveExtra(PushManager.UnregisterEvent);
        }
        else if (mainAppIntent.HasExtra(PushManager.RegisterErrorEvent))
        {
            mainAppIntent.RemoveExtra(PushManager.RegisterErrorEvent);
        }
        else if (mainAppIntent.HasExtra(PushManager.UnregisterErrorEvent))
        {
            mainAppIntent.RemoveExtra(PushManager.UnregisterErrorEvent);
        }

        Intent = mainAppIntent;
    }

    protected override void OnResume()
    {
        base.OnResume();

        registerReceivers();
    }

    protected override void OnPause()
    {
        base.OnPause();

        unregisterReceivers();
    }

    public void registerReceivers()
    {
        IntentFilter intentFilter = new IntentFilter(PackageName + ".action.PUSH_MESSAGE_RECEIVE");

        if (mBroadcastPush)
        {
            RegisterReceiver(mMessageReceiver, intentFilter);
        }

        RegisterReceiver(mRegisterReceiver, new IntentFilter(PackageName + "." + PushManager.RegisterBroadCastAction));
    }

    public void unregisterReceivers()
    {
        UnregisterReceiver(mMessageReceiver);
        UnregisterReceiver(mRegisterReceiver);
    }
}

1

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.company.mobile" android:versionCode="1" android:versionName="1.9.8">
    <!--GCM for Pushwoosh-->
    <permission android:name="PACKAGE_NAME.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="PACKAGE_NAME.permission.C2D_MESSAGE" />
    <uses-sdk android:minSdkVersion="15" />
    <application android:label="Company Mobile" android:icon="@drawable/icon">
        <meta-data android:name="com.google.android.gms.version" android:value="XXXXXXXX" />
        <meta-data android:name="PW_APPID" android:value="XXXXX-XXXXX" />
        <meta-data android:name="PW_PROJECT_ID" android:value="XXXXXXX-XXXXXX-XXXXXXX" />
        <!--GCM-->
        <receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.company.mobile" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

我会承认我是android开发的新手(但不是.net或C#),所以这可能是相对明显的,但我不知所措 . 我已经查看了我能找到的与此相关的每个问题,但没有一个解决方案有帮助 . 谁能看到我做错了什么?

1 回答

  • 2

    我相信在这两行中:

    <permission android:name="PACKAGE_NAME.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="PACKAGE_NAME.permission.C2D_MESSAGE" />
    

    应将PACKAGE_NAME更改为您的实际包名称,在您的情况下为 com.company.mobile<manifest> 元素中的 package 属性) .

    所以尝试:

    <permission android:name="com.company.mobile.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.company.mobile.permission.C2D_MESSAGE" />
    

相关问题