我正试图在BOOT_COMPLETED意图上运行一些代码而没有运气。这是我试过的:
myreciever:
using Android.App;
using Android.Content;
using System;
using System.Collections.Generic;
using System.Linq;
using onesentiment.Helpers;
using Newtonsoft.Json;
using Plugin.LocalNotifications;
using Android.Widget;
namespace onesentiment
{
[BroadcastReceiver(Name = "com.onesentiment.name.BootBroadcastReceiver", Enabled = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class BootUpReceiver : BroadcastReceiver
{
List<TaskReminder> CurrentTaskReminders = new List<TaskReminder>();
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "OnReceive", ToastLength.Long).Show();
if (intent.Action == Intent.ActionBootCompleted)
{
Toast.MakeText(context, "Intent.ActionBootCompleted", ToastLength.Long).Show();
if (Settings.TaskReminderList != string.Empty)
CurrentTaskReminders = JsonConvert.DeserializeObject<List<TaskReminder>>(Settings.TaskReminderList);
// Set up notification icon if necessary
var resourceId = (int)typeof(Resource.Drawable).GetField("ic_launcher").GetValue(null);
LocalNotificationsImplementation.NotificationIconId = resourceId;
// Retrieve notifications and cancel/resend them.
if (CurrentTaskReminders.Count() > 0)
{
for (int i = 0; i < CurrentTaskReminders.Count(); i++)
{
CrossLocalNotifications.Current.Show(
context.Resources.GetString(Resource.String.SetReminders_ReminderTitle),
context.Resources.GetString(Resource.String.SetReminders_ReminderMessage),
CurrentTaskReminders[i].CrossLocalNotificationID,
CurrentTaskReminders[i].Date );
}
}
}
}
}
}
清单上的我有:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.onesentiment.name" android:versionCode="6" android:versionName="1.0" android:installLocation="internalOnly">
<uses-sdk android:minSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="onesentiment" android:hardwareAccelerated="true" android:icon="@drawable/ic_launcher" android:theme="@style/MyTheme">
<receiver android:name="com.onesentiment.name.BootBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="com.onesentiment.name" />
</intent-filter>
</receiver>
<meta-data android:name="android.max_aspect" android:value="2.1" />
</application>
</manifest>
我从共享偏好中获取 Settings.TaskReminderList 。
我没有收到任何Toast,并且没有显示来自 CrossLocalNotifications 的通知。如果我不重新启动手机,他们就会工作。
编辑:我收到以下错误:
Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED pkg=com.onesentiment.name}
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.BOOT_COMPLETED from pid=9941, uid=2000
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:3605)
at com.android.commands.am.Am.sendBroadcast(Am.java:780)
at com.android.commands.am.Am.onRun(Am.java:410)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:51)
at com.android.commands.am.Am.main(Am.java:124)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:359)
对于Boot Completed权限,我只需将其包含在我的清单中。我没有关于此权限的任何其他代码,这是问题吗?
EDIT2: 在模拟器上运行命令adb shell am broadcast -a android.intent.action.BOOT_COMPLETED com.onesentiment.name之后我得到了这个(并且模拟器没有重启):
D/AndroidRuntime( 2417):
D/AndroidRuntime( 2417): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
D/AndroidRuntime( 2417): CheckJNI is OFF
E/cutils-trace( 2417): Error opening trace file: No such file or directory (2)
E/memtrack( 2417): Couldn't load memtrack module (No such file or directory)
E/android.os.Debug( 2417): failed to load memtrack module: -2
D/AndroidRuntime( 2417): Calling main entry com.android.commands.am.Am
D/AndroidRuntime( 2417): Shutting down VM
答案 0 :(得分:0)
我必须转到“设置” /“应用程序” /“ MyApp”并启用自动启动。所以这不是我的代码的问题。某些手机具有类似小米Redmi手机的安全问题