I am trying to make a simple service which starts with device boot. Thing is that device return message "Unfortunately, [app_name] has stopped."
I am struggling with this problem from few hours, with looking for mistake, but it is too simple.. Hope, you guys can help me with this problem.
This is my code:
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application android:allowBackup="true" android:label="@string/app_name">
<receiver android:name=".StartReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".PService" />
</application>
StartReceiver.cs
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class StartReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Intent startIntent = new Intent(context, typeof(PService));
context.StartService(startIntent);
}
}
and lastly PService.cs
[Service]
public class PService : 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, "Start", ToastLength.Short).Show();
return StartCommandResult.Sticky;
}
public override void OnDestroy()
{
base.OnDestroy();
Toast.MakeText(this, "Stop", ToastLength.Short).Show();
}
}
Additional this service application is targetted to API 19 (4.4.2 KitKat) Android version.
I think there will be really small mistake, made by me but truly I cant find it out.. Thanks in advance for any help.
By adding the receiver in the manifest and via the BroadcastReceiverAttribute you have two receivers in your manifest. Plus the one in your manifest will not work since it is not the MD5-based Java name that Xamarin creates by default.
1) Remove the receiver and boot permission from your manifest
2) Add your boot permissions via an attribute)
[assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)]
3) Add the manifest entry via attributes:
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class BootBroadcastReceiver : BroadcastReceiver
1) Add the manifest entry for the boot permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
2) Add the receiver and use a full qualify Java class name:
<receiver android:name="com.yourpackagename.app.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
3) Add a Name
parameter to the BroadcastReceiverAttribute
for the fully qualified Java class name that you used in the manifest
[BroadcastReceiver(Name = "com.yourpackagename.app.BootBroadcastReceiver", Enabled = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class BootBroadcastReceiver : BroadcastReceiver
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With