问题
I am trying to implement a broadcast receiver that gets the broadcast when the device has been rebooted, but is not working (it is supposed to send me a toast when the device has rebooted) with the following code:
Broadcast receiver:
[BroadcastReceiver]
public class RebootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (Intent.ActionBootCompleted.Equals(intent.Action))
{
Toast.MakeText(
context,
"Your app has been rebooted!",
ToastLength.Long).Show();
}
}
}
Manifest.xml
<receiver android:name=".RebootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
And permission inside the manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Hope help, thanks
回答1:
I think there is a problem with your broadcast receiver follow these steps and see if it works:
Add the manifest entry for the boot permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Add the receiver and use a full qualifed name:
<receiver android:name="com.yourpackagename.app.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Add a Name
parameter to the BroadcastReceiverAttribute
for the fully qualified name that you used in the manifest
[BroadcastReceiver(Name = "com.yourpackagename.app.BootBroadcastReceiver", Enabled = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class BootBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (Intent.ActionBootCompleted.Equals(intent.Action))
{
Toast.MakeText(
context,
"Your app has been rebooted!",
ToastLength.Long).Show();
}
}
}
Good luck feel free to get back if you have queries
回答2:
I have solved the problem, the answer of @FreakyAli has also helped in fact of getting the solution
Create Service:
[Service(Name = "com.companyname.app.RebootService")]
public class RebootService : Service
{
public override void OnCreate()
{
base.OnCreate();
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
Toast.MakeText(this, "Service STARTED!", ToastLength.Long).Show();
return StartCommandResult.Sticky;
}
public override void OnDestroy()
{
base.OnDestroy();
Toast.MakeText(this, "Service STOPED", ToastLength.Long).Show();
}
}
}
Create BroadcastReciver:
[BroadcastReceiver(Enabled =true, Name ="com.companyname.Sortex.RebootReceiver")]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class RebootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
}
}
register Service and BroadcastReceiver on AndroidManifest.xml
<service android:name="com.companyname.app.RebootService"/>
<receiver android:name="com.companyname.app.RebootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Call the service on the OnReceive method of the Broadcast receiver:
Intent serviceIntent = new Intent(context, typeof(RebootService));
context.StartService(serviceIntent);
来源:https://stackoverflow.com/questions/60076532/rebooting-receiver-not-working-android-xamarin-android