Xamarin.android 引导后接收器未启动
无法让我的Xamarin.Android应用程序在启动后启动Toast。我检查了许多公认的解决方案,但似乎没有一个能解决我的问题。我也尝试过各种“有效”的例子,但没有任何运气,所以很明显我遗漏了一些东西 设备:三星Galaxy S3 空气污染指数:19 安卓清单Xamarin.android 引导后接收器未启动,xamarin.android,Xamarin.android,无法让我的Xamarin.Android应用程序在启动后启动Toast。我检查了许多公认的解决方案,但似乎没有一个能解决我的问题。我也尝试过各种“有效”的例子,但没有任何运气,所以很明显我遗漏了一些东西 设备:三星Galaxy S3 空气污染指数:19 安卓清单 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" p
<?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>
看来我想做的事是不可能的。安装我的应用程序后,该应用程序处于停止状态,必须在第一次手动执行,然后才能接收广播
[我想做的似乎是不可能的。一旦安装了我的应用程序,该应用程序将处于停止状态,必须在第一次手动执行,然后才能接收广播
[
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解决方案:
p.S:在真实设备上,启动事件需要一段时间,即:在我的三星手机上解锁屏幕后2分钟。在emulator上,启动事件需要很短的时间 我写了下面的代码来通知它何时会触发: 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解决方案:
在BootReceiver类中设置[BroadcastReceiver(Enabled=true)]属性,请参见此处的示例:添加了[BroadcastReceiver(Enabled=true)],但仍不工作。是否有其他方法测试它是否工作?可能我只是没有看到Toast。设置[BroadcastReceiver(Enabled=true)]属性,请参见这里的示例:添加了[BroadcastReceiver(Enabled=true)],但仍然不起作用。是否有其他方法来测试它是否起作用?也许我只是没有看到Toast。