简体   繁体   English

如何让 C# Xamarin.Android 应用程序在手机启动时运行?

[英]How can I make C# Xamarin.Android app run at phone start-up?

I have been trying to get an app to run when phone starts, and eventually run app when I press KeyCode.CameraButton .我一直试图让应用程序在手机启动时运行,并最终在我按下KeyCode.CameraButton时运行应用程序。 I am using a class called BootReceiver , inherited from BroadcastReceiver .我正在使用一个名为BootReceiver的类,它是从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);                
        }
    }
}

At the moment the app restarts when broadcast received.目前,应用程序在收到广播时重新启动。 I run this from adb command prompt to emulate boot up and phone restarts!我从adb命令提示符运行它来模拟启动和手机重启!

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!需要一步一步来,但不能很好地调试它,当我模拟启动时手机如何重置真是太奇怪了!

I just tried a manual reboot and app tried to start but said unfortunately [myapp] has stopped working then phone reboots again.我只是尝试手动重启,应用程序尝试启动,但unfortunately [myapp] has stopped working然后手机再次重启。

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);
        }
    }
}

You need to add the ActivityFlags.NewTask flag to the intent because you are launching an activity outside of an activity context.您需要将ActivityFlags.NewTask标志添加到意图中,因为您要在活动上下文之外启动活动。

The crash is because a Android.Util.AndroidRuntimeException is generated by the boot receiver.崩溃是因为启动接收器生成了Android.Util.AndroidRuntimeException

The solution:解决方案:

[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);                
    }
}

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

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