Attempting to start the service on Android

I was trying to start the service when the device started on android, but it didn't work properly.I've looked at a lot of online links, but none of the code works.Did I forget anything?

AndroidManifest.xml

<receiver
    android:name=".StartServiceAtBootReceiver"
    android:enabled="true"
    android:exported="false"
    android:label="StartServiceAtBootReceiver" >
    <intent-filter>
        <action android:name="android.intent.action._BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service
    android:name="com.test.RunService"
    android:enabled="true" />

Broadcast Receiver

public void onReceive(Context context, Intent intent) {
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent serviceLauncher = new Intent(context, RunService.class);
        context.startService(serviceLauncher);
        Log.v("TEST", "Service loaded at start");
    }
}

#1st floor

along with

<action android:name="android.intent.action.BOOT_COMPLETED" />  

Also used

<action android:name="android.intent.action.QUICKBOOT_POWERON" />

HTC device does not seem to catch up with BOOT_COMPLETED

#2nd floor

Actually, I've had this problem a while ago and it's really easy to fix. If you set the "android.intent.action.BOOT_COMPLETED" permission and intent-filter, you're not actually doing anything wrong.

Note that if you start the boot service on Android 4.X, you must first run the broadcast listener, which means you must first add an activity, and then run the broadcast receiver before your application can run as expected, but on AndroidOn 4.X, I didn't find a way to start the service at startup without any activity, and for security reasons, I think Google did.

#3rd floor

See this link http://khurramitdeveloper.blogspot.in/2013/06/start-activity-or-service-on-boot.html Step-by-step process to start on a service

#4th floor

How to start services on device startup (auto-running applications, etc.)

First: Beginning with Android 3.1 and later, if a user has at least never started your application or if the user has "forced shutdown" the application, he or she will not receive BOOT_COMPLETE.This is done to prevent malware from automatically registering services.This security vulnerability has been closed in a newer version of Android.

Solution:

Create an application with activities.After the user runs once, the application receives a BOOT_COMPLETE broadcast message.

Second: Send BOOT_COMPLETE before installing external memory.If the application is installed in external storage, it will not receive BOOT_COMPLETE broadcast messages.

In this case, there are two solutions:

  1. Install your application to internal storage
  2. Install another small application in the internal storage.The application receives the BOOT_COMPLETE and runs the second application on the external storage.

If your application is already installed in internal storage, the following code can help you understand how to start the service at device startup.

In Manifest.xml

Allow:

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

Register your BOOT_COMPLETED recipient:

<receiver android:name="org.yourapp.OnBoot">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

Register your services:

<service android:name="org.yourapp.YourCoolService" />

In Receiver OnBoot.java:

public class OnBoot extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        // Create Intent
        Intent serviceIntent = new Intent(context, YourCoolService.class);
        // Start service
        context.startService(serviceIntent);

    }

 }

For HTC, if the device does not capture RECEIVE_BOOT_COMPLETED, you may also need to add the following code to the list:

<action android:name="android.intent.action.QUICKBOOT_POWERON" />

The receiver now looks like this:

<receiver android:name="org.yourapp.OnBoot">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

How do I test BOOT_COMPLETED without restarting the emulator or real device?This is easy.Try this:

adb -s device-or-emulator-id shell am broadcast -a android.intent.action.BOOT_COMPLETED

How do I get the device ID?Get a list of connected devices with IDs:

adb devices

By default, you can find adb in ADT

adt-installation-dir/sdk/platform-tools

Enjoy it!)

#5th floor

This problem occurs if I leave an empty constructor in the receiver class.After the empty container is removed, the onRreceive s method begins to function properly.

Tags: Android xml emulator Google

Posted on Wed, 18 Mar 2020 21:54:06 -0400 by daglasen