BroadcastReceiver启动后不启动

时间:2017-04-01 11:54:51

标签: xamarin.android

启动后无法让我的Xamarin.Android应用程序启动Toast。我检查了许多已接受的解决方案但似乎没有解决我的问题。我也尝试了各种各样的工作"例子,但没有任何运气如此清楚,我错过了一些东西。

设备:三星Galaxy S3 API:19

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.novak" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="16" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application android:label="ReceiverApp">
    <receiver android:enabled="true"
        android:exported="true"
        android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
        android:name="com.novak.BootReceiver" >

      <intent-filter >
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
    </receiver>
  </application>
</manifest>

BootReceiver.cs

using Android.App;
using Android.Widget;
using Android.Content;

namespace ReceiverApp
{

    [BroadcastReceiver]
    [IntentFilter(new[] { Intent.ActionBootCompleted })]
    public class BootReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            Toast.MakeText(context, "Receiver", ToastLength.Long).Show();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

似乎我想做的事情是不可能的。安装我的应用程序后,应用程序处于停止状态,必须在第一次手动执行才能接收广播

[How to start a Service when .apk is Installed for the first time

答案 1 :(得分:0)

P.S:在真实设备上,触发事件需要一段时间,即:在解锁三星屏幕上2分钟后。在模拟器上花费的时间很短。

我写了以下代码来通知何时触发:

Autorun.cs -只需将此文件添加到项目中即可,就是这样,它将在启动后启动。无需清单修改。

using Android;
using Android.App;
using Android.Content;

// we need to ask permission to be notified of these events
[assembly: UsesPermission (Manifest.Permission.ReceiveBootCompleted)]

namespace XamarinCookbook
{
  // we want this to fire when the device boots
  [BroadcastReceiver]
  [IntentFilter (new []{ Intent.ActionBootCompleted })]
  public class ServiceStarter : BroadcastReceiver
  {
    public override void OnReceive (Context context, Intent intent)
    {

      #region Start chrome

      var mesaj = "Autorun started";
      Android.Widget.Toast.MakeText(Android.App.Application.Context, mesaj, Android.Widget.ToastLength.Long).Show();

      var uri = Android.Net.Uri.Parse("https://500px.com");
      var intent1 = new Intent(Intent.ActionView, uri);
      intent1.AddFlags(ActivityFlags.NewTask);

      intent1.SetPackage("com.android.chrome");
      try
      {
        context.StartActivity(intent1);
      }
      catch (ActivityNotFoundException ex)
      {
        //Chrome browser not installed
        intent.SetPackage(null);
        context.StartActivity(intent1);
      }

      #endregion

      /*
            #region Real code
            // just start the service
            var myIntent = new Intent (context, typeof(XamarinService));
          context.StartService (myIntent);

      #endregion
      */
    }
  }
}

VS解决方案:

https://drive.google.com/open?id=1iYZQ2YCvBkyym9-2FvU7KXoBKUWsMdlT