user3528540
user3528540

Reputation:

Android - Start a program immediately on startup of device

Hello everyone and thanks for your trouble,

I have a program detailed here. This uses a microphone icon in order to start listening for voice. However, I want this program to run immediately on start up of the device. How is this possible? Also, I want the program to run without any visuals and simply return the text translation to my running program, which will take care of what command to execute. Any help on any of these topics? It will be greatly appreciated.

Upvotes: 0

Views: 88

Answers (1)

Rashad
Rashad

Reputation: 11197

Yes you can do it. For this you'll need set permission to AndroidManifest.xml.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

In your AndroidManifest.xml, define your service and listener for the BOOT_COMPLETED action:

<service android:name=".MyService" android:label="My Service">
    <intent-filter>
        <action android:name="com.myapp.MyService" />
    </intent-filter>
</service>

<receiver
    android:name=".receiver.StartMyServiceAtBootReceiver"
    android:enabled="true"
    android:exported="true"
    android:label="StartMyServiceAtBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Then you have to define the receiver that will get the BOOT_COMPLETED action and start your service.

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent serviceIntent = new Intent(context, MySystemService.class);
            context.startService(serviceIntent);
        }
    }
}

Hope this helps .. :)

Upvotes: 1

Related Questions