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

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

问题描述

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

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

推荐答案

您需要添加 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天全站免登陆