Android - Start service on boot

前端 未结 7 1156
温柔的废话
温柔的废话 2020-11-22 16:05

From everything I\'ve seen on Stack Exchange and elsewhere, I have everything set up correctly to start an IntentService when Android OS boots. Unfortunately it is not start

相关标签:
7条回答
  • 2020-11-22 16:17

    Following should work. I have verified. May be your problem is somewhere else.

    Receiver:

    public class MyReceiver extends BroadcastReceiver{
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_BOOT_COMPLETED.equals(arg1.getAction())) {
                Log.d("TAG", "MyReceiver");
                Intent serviceIntent = new Intent(context, Test1Service.class);
                context.startService(serviceIntent);
            }
        }
    }
    

    Service:

    public class Test1Service extends Service {
        /** Called when the activity is first created. */
        @Override
        public void onCreate() {
            super.onCreate();
            Log.d("TAG", "Service created.");
        }
        
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.d("TAG", "Service started.");
            return super.onStartCommand(intent, flags, startId);
        }
        
        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
            Log.d("TAG", "Service started.");
        }
        @Override
        public IBinder onBind(Intent arg0) {
            return null;
        }
    }
    

    Manifest:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.test"
          android:versionCode="1"
          android:versionName="1.0"
          android:installLocation="internalOnly">
        <uses-sdk android:minSdkVersion="8" />
    
        <application android:icon="@drawable/icon" android:label="@string/app_name">
        
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.BATTERY_STATS" 
        />
    <!--        <activity android:name=".MyActivity">
                <intent-filter>  
                    <action android:name="android.intent.action.MAIN" /> 
                    <category android:name="android.intent.category.LAUNCHER"></category> 
                </intent-filter>
           </activity> -->
            <service android:name=".Test1Service" 
                      android:label="@string/app_name"
                      >
            </service>
            <receiver android:name=".MyReceiver">  
                <intent-filter>  
                    <action android:name="android.intent.action.BOOT_COMPLETED" /> 
                </intent-filter>  
            </receiver> 
        </application>
    </manifest>
    
    0 讨论(0)
  • 2020-11-22 16:20

    Well here is a complete example of an AutoStart Application

    AndroidManifest file

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="pack.saltriver" android:versionCode="1" android:versionName="1.0">
    
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
        <application android:icon="@drawable/icon" android:label="@string/app_name">
    
            <receiver android:name=".autostart">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
    
            <activity android:name=".hello"></activity>
            <service android:enabled="true" android:name=".service" />
        </application>
    </manifest>
    

    autostart.java

    public class autostart extends BroadcastReceiver 
    {
        public void onReceive(Context context, Intent arg1) 
        {
            Intent intent = new Intent(context,service.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(intent);
            } else {
                context.startService(intent);
            }
            Log.i("Autostart", "started");
        }
    }
    

    service.java

    public class service extends Service
    {
        private static final String TAG = "MyService";
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
        public void onDestroy() {
            Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onDestroy");
        }
    
        @Override
        public void onStart(Intent intent, int startid)
        {
            Intent intents = new Intent(getBaseContext(),hello.class);
            intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intents);
            Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onStart");
        }
    }
    

    hello.java - This will pop-up everytime you start the device after executing the Applicaton once.

    public class hello extends Activity 
    {   
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 16:24

    Looks very similar to mine but I use the full package name for the receiver:

    <receiver android:name=".StartupIntentReceiver">
    

    I have:

    <receiver android:name="com.your.package.AutoStart"> 
    
    0 讨论(0)
  • 2020-11-22 16:26

    I have found a way to make your application run well when the device reboots, please follow the steps below to be successful.

    AndroidManifest file

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pack.saltriver" android:versionCode="1" android:versionName="1.0">
    <uses-permission 
    android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    
    <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".UIBootReceiver" android:enabled="true" 
        android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
         <service android:name=".class_Service" />
    </application>
    

    UIBootReceiver

    public class UIBootReceiver extends BroadcastReceiver {
    
    private static final String TAG = "UIBootReceiver";
    @Override
    
        public void onReceive(Context context, Intent arg1)
        {
            Toast.makeText(context, "started", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(context,class_Service.class);
            context.startService(intent);
        }
      }
    

    This is asking permission to not need to manage battery saving for this app so you can run in the background stably.

    Declare this code in onCreate () of MainActivity class:

       Intent myIntent = new Intent();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            myIntent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            myIntent.setData(Uri.parse("package:" + 
       DeviceMovingSpeed.this.getPackageName()));
        }
        startActivity(myIntent);
    
    0 讨论(0)
  • 2020-11-22 16:34

    Just to make searching easier, as mentioned in comments, this is not possible since 3.1 https://stackoverflow.com/a/19856367/6505257

    0 讨论(0)
  • 2020-11-22 16:37

    I've had success without the full package, do you know where the call chain is getting interrupted? If you debug with Log()'s, at what point does it no longer work?

    I think it may be in your IntentService, this all looks fine.

    0 讨论(0)
提交回复
热议问题