Start activity on boot

前端 未结 6 1157
梦毁少年i
梦毁少年i 2020-12-06 03:19

I\'d like to start my app just after the phone boot. Apparently the app is started after the boot but it immediately crashes (just to be clear the app normally works fine).

相关标签:
6条回答
  • 2020-12-06 03:44

    I managed to solve the problem. Inside the OnCreate() I had this code (related to the USB communication) which was causing the crash:

        act_string = getIntent().getAction();
        if( -1 != act_string.indexOf("android.intent.action.MAIN")){
            restorePreference();
        }           
        else if( -1 != act_string.indexOf("android.hardware.usb.action.USB_ACCESSORY_ATTACHED")){
            cleanPreference();
        }   
    

    Deleting this code solved the start after boot issue.

    0 讨论(0)
  • 2020-12-06 03:46

    do it like this in if condition

     if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
    
    0 讨论(0)
  • 2020-12-06 03:48

    in place of

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

    add also this

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

    some devices like HTC don't catch BOOT_COMPLETED

    0 讨论(0)
  • 2020-12-06 03:50
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>    
    <receiver android:name=".StartMyActivityAtBootReceiver"
                          android:enabled="true"
                          android:exported="true">
                    <intent-filter>
                        <action android:name="android.intent.action.BOOT_COMPLETED"/>
                        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
                    </intent-filter>
                </receiver>
    
    0 讨论(0)
  • 2020-12-06 03:53
    Try this:
    

    1] In AndroidManifest.xml file:

     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
     <application 
     ...
        <receiver
            android:name=".StartMyActivityAtBootReceiver"
            android:enabled="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
    
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
     </application>
    

    2] Inside BroadcastReciever class with StartMyActivityAtBootReceiver as class name.

    @Override
    public void onReceive(Context context, Intent intent) {
    
        Intent i = new Intent(context, MainActivity.class);  
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i); 
    
    } 
    

    This worked for me. The difference in code is as follows:

    • android:permission="android.permission.RECEIVE_BOOT_COMPLETED" inside receiver.
    • included "category android:name="android.intent.category.DEFAULT" " inside intent filter.
    • I am not checking the intent in onRecieve, as i know that code will be executed only if its true
    0 讨论(0)
  • 2020-12-06 03:54

    I would like to add the whole manifest file which workedv for me on oppo neo 5. And,even take care that some phone requires special access to achieve boot start or other special permissions.So,don't forgot to allow the accesses to your app!!.

    Here's the code -

        <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.javacodegeeks.androidserviceonbootexample"
        android:installLocation="internalOnly">
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    
        <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="SilversithService"
            android:theme="@style/AppTheme">
            <receiver
                android:name="com.javacodegeeks.androidserviceonbootexample.BroadcastReceiverOnBootComplete">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <action android:name="android.intent.action.REBOOT"/>
                    <category android:name="android.intent.category.HOME" />
                </intent-filter>
            </receiver>
            <service android:name="com.javacodegeeks.androidserviceonbootexample.AndroidServiceStartOnBoot"
                android:enabled="true"></service>
            <activity
                android:name="com.javacodegeeks.androidserviceonbootexample.MainActivity"
                android:label="SilversithService">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>
    
    0 讨论(0)
提交回复
热议问题