如何在电话启动时运行C#Xamarin.Android应用程序? [英] How can I make C# Xamarin.Android app run at phone start-up?

查看:79
本文介绍了如何在电话启动时运行C#Xamarin.Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试让应用程序在手机启动时运行,并最终在按 KeyCode.CameraButton 时运行该应用程序.我正在使用从 BroadcastReceiver 继承的名为 BootReceiver 的类.这是我的课程:

I have been trying to get an app to run when phone starts, and eventually run app when I press KeyCode.CameraButton. I am using a class called BootReceiver, inherited from BroadcastReceiver. Here is my class:

namespace ColorPoint.Xamarin.XAndroid
{
    [BroadcastReceiver]
    [IntentFilter(new[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.LowPriority)]
    public class BootReceiver : BroadcastReceiver
    { 
        public override void OnReceive(Context context, Intent intent)
        {
            Intent serviceStart = new Intent(context, typeof(MainActivity));
            context.StartActivity(serviceStart);                
        }
    }
}

此刻,当接收到广播时,应用程序将重新启动.我从 adb 命令提示符下运行此程序,以模拟启动和电话重启!

At the moment the app restarts when broadcast received. I run this from adb command prompt to emulate boot up and phone restarts!

adb -s device-or-emulator-id shell am broadcast -a android.intent.action.BOOT_COMPLETED

这是我的清单文件,不确定其是否正确!

Here is my manifest file, not sure if its correct at all!

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ColorPoint.Xamarin.XAndroid" android:installLocation="auto" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" />
    <application android:label="Rexson Barcode Scanner" android:icon="@drawable/icon">
        <receiver android:name=".BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_INPUT_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <activity android:name="MainActivity" />
    <activity android:name="com.google.zxing.client.android.CaptureActivity" android:exported="false" android:windowSoftInputMode="stateAlwaysHidden">
        <intent-filter>
            <action android:name="com.phonegap.plugins.barcodescanner.SCAN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</manifest>

需要逐步进行,但是无法很好地调试它,所以很奇怪,当我模拟启动时手机如何重置!

Need to go through this step by step but can't debug it very well, just so weird how phone resets when I emulate boot up!

我刚刚尝试了手动重启,但应用程序尝试启动,但是说不幸的是[myapp]已停止工作,然后手机再次重启.

I just tried a manual reboot and app tried to start but said unfortunately [myapp] has stopped working then phone reboots again.

似乎它试图打开但立即崩溃,然后重新启动电话.

Seems like it tries to open but crashes immediatley, then reboots phone.

在另一个示例中找到了这个,但是还是一样,只是重启电话.

Found this from another example but same thing, just reboots phone..

public override void OnReceive(Context context, Intent intent)
{
    if (intent.Action == Intent.ActionBootCompleted)
    {
        bool autoRestart = false;
        var sp = context.GetSharedPreferences("preferences", FileCreationMode.Private);
        autoRestart = sp.GetBoolean("autoRestart", false);
        if (autoRestart)
        {
            Intent serviceStart = new Intent(context, typeof(MainActivity));
            serviceStart.AddFlags(ActivityFlags.NewTask);
            context.StartActivity(serviceStart);
        }
    }
}

推荐答案

您需要添加 ="noreferrer"> ActivityFlags.NewTask 标记为意图,因为您要在活动上下文之外启动活动.

You need to add the ActivityFlags.NewTask flag to the intent because you are launching an activity outside of an activity context.

崩溃是因为引导接收器生成了 Android.Util.AndroidRuntimeException .

The crash is because a Android.Util.AndroidRuntimeException is generated by the boot receiver.

解决方案:

[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.LowPriority)]
public class BootReceiver : BroadcastReceiver
{ 
    public override void OnReceive(Context context, Intent intent)
    {
        Intent serviceStart = new Intent(context, typeof(MainActivity));
        serviceStart.AddFlags (ActivityFlags.NewTask);
        context.StartActivity(serviceStart);                
    }
}

这篇关于如何在电话启动时运行C#Xamarin.Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆