Starting IoT-Ignite Android Things™ Application On Boot
In this tutorial, you will learn how to start an IoT-Ignite Android Things application on boot.
Normally in Android Things, if you want to start your application on top you have to add the following line to your main activity’s intent filter.
<category android:name="android.intent.category.IOT_LAUNCHER" />
If you want to learn more about IOT_LAUNCHER, we advise you to read Create an Android Things Project article.
IoT-Ignite Agent application is set as IOT_LAUNCHER, so, you do not have to add this to your application. When there is more than one application added as IOT_LAUNCHER, on boot up you have to choose one of them.
To start your application on boot, we will write a boot receiver and start our activity into it.
Step 1: Creating Class
Create a class named BootReceiver.
Step 2: Starting Main Activity
Start your MainActivity from the receiver.
Add this code to your receiver’s onReceive() function.
Log.i(TAG,"Boot broadcast received!! Starting application"); Intent applicationIntent = new Intent(context, MainActivity.class); context.startActivity(applicationIntent);
Step 3: Granting Permissions
Add boot receive permission to Android Manifest.
To receive “boot completed” action you must add the following permission to your manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Step 4: Adding Receiver to Android Manifest
Add previously created BootReceiver.class to Android Manifest’s receiver.
<receiver android:name=".receivers.BootReceiver" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
Step 5: Test
Reboot your device and test your application. You should see a log like this:
02-20 07:32:11.196 907-907/? I/art: Late-enabling -Xcheck:jni 02-20 07:32:11.198 907-907/? W/art: Unknown instruction set features for ARM CPU variant (generic) using conservative defaults 02-20 07:32:11.402 907-907/com.example.yavuzerzurumlu.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.yavuzerzurumlu.myapplication-2/lib/arm 02-20 07:32:11.487 907-907/com.example.yavuzerzurumlu.myapplication I/InstantRun: Instant Run Runtime started. Android package is com.example.yavuzerzurumlu.myapplication, real application class is null. 02-20 07:32:12.142 907-907/com.example.yavuzerzurumlu.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.yavuzerzurumlu.myapplication-2/lib/arm 02-20 07:32:12.237 907-907/com.example.yavuzerzurumlu.myapplication I/BootReceiver: Boot broadcast received!! Starting application 02-20 07:32:22.690 907-907/com.example.yavuzerzurumlu.myapplication I/IotIgniteManager: Setting listener.. 02-20 07:32:22.690 907-907/com.example.yavuzerzurumlu.myapplication I/IotIgniteManager: Connecting.. 02-20 07:32:22.721 907-907/com.example.yavuzerzurumlu.myapplication I/IotIgniteManager: Auth Success ! 02-20 07:32:22.722 907-907/com.example.yavuzerzurumlu.myapplication I/IotIgniteManager: IotIgniteManager instance : com.ardic.android.iotignite.nodes.IotIgniteManager@5abf0b2 02-20 07:32:22.722 907-907/com.example.yavuzerzurumlu.myapplication I/IotIgniteManager: Is Service Ready : YES 02-20 07:32:22.726 907-907/com.example.yavuzerzurumlu.myapplication I/IotIgniteManager: getting all nodes.... Size : 0 02-20 07:32:22.728 907-907/com.example.yavuzerzurumlu.myapplication I/IotIgniteHandler: Ignite Connected
Happy Codings,