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

时间:2015-11-26 10:08:18

标签: c# android xamarin

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

    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命令提示符运行此命令以模拟启动和电话重启!

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

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

    '<?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>'

需要逐步完成这个步骤,但是无法很好地调试它,当我模拟启动时手机重置的方式非常糟糕!

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

似乎它试图打开但崩溃了immediatley,然后重新启动手机

从另一个例子中找到了这个但同样的事情,只需重新启动手机..

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

1 个答案:

答案 0 :(得分:6)

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

崩溃是因为启动接收器生成.... HSSFCellStyle cellStyle1 = workbook.createCellStyle(); cellStyle1.setFillForegroundColor(new HSSFColor.BLACK().getIndex()); cellStyle1.setFillBackgroundColor(new HSSFColor.RED().getIndex()); cellStyle1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); for(int i=0;i < rowCount;i++){ sheet.getRow(i).setRowStyle(cellStyle1); } ...

解决方案:

Android.Util.AndroidRuntimeException