Android application does not start at startup

I have a basic Android application that must constantly restart the phone a certain number of times. To do this, I need the application to start when the phone starts. To do this, I essentially followed the instructions found here , adding permissions to the manifest and creating the BroadcastReceiver class that triggers the action. Here are some of my relevant code:

public class StartMyServiceAtBootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { System.out.println("StartMyServiceAtBootReceiver called."); if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { // Intent activityIntent = new Intent("com.example.rebooter.MainActivity"); Intent activityIntent = new Intent(context,MainActivity.class); activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(activityIntent); } } 

}

From manifest file:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.rebooter" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </activity> <service android:name=".RebootManager" android:label="Reboot Manager" > <action android:name="com.example.rebooter.RebootManager" /> </service> <receiver android:name=".StartMyServiceAtBootReceiver" android:enabled="true" android:exported="true" android:label="StartMyServiceAtBootReceiver" > <action android:name="android.intent.action.BOOT_COMPLETED" /> </receiver> </application> 

In the Eclipse emulator, the application works correctly. That is, although my emulator is not implemented and the phone does not execute the reboot commands correctly, I see that the correct activity starts at startup.

Now, when I try to use it on a specific system running Android 4.0.4, everything in the application works correctly. EXCEPT loading at startup. Any ideas?

I tried to fix any hardware problems (since I am not using a commercially released phone) by installing another application that loads at startup and confirms that it really loads at startup, and it does appear when applications start up as a cached process after startup.

I would appreciate any help. Let me know if you need more information.

+1
source share
1 answer

There are several issues here.

Firstly, you neglected sending StartMyServiceAtBootReceiver , the component that you expect to receive at boot time, so we cannot comment on whether there are any problems with it.

Secondly, unless something explicitly performed by one of your components (for example, a user launching MainActivity from the main screen), StartMyServiceAtBootReceiver will never be called on Android 3.1+. Make sure you start your activity before trying to restart your computer and see if this helps.

Thirdly, you have implemented the constructor on StartupManager , which is a bad idea. Move this logic to onCreate() .

Fourth, your code is likely to crash in this constructor, since getApplication() will not return a valid value at this point in the code, especially because you were unable to bind the superclass constructor. Again, moving this code to onCreate() will help here.

Fifthly, starting an activity from the onCreate() service (not to mention its constructor) is very unusual and cannot be evaluated by the user. Moreover, if this service does nothing, you can just as easily start this activity with StartMyServiceAtBootReceiver and skip StartupManager completely.

Sixth, you have <intent-filter> elements on your services, as if you expected third-party developers to reference these services. If so, great. If not, remove the <intent-filter> elements and use the explicit Intents in the rest of your application code to reference them (e.g. new Intent(this, StartupManager.class) ), for better security. Or add android:exported="false" to these services, although this is automatic if you remove the <intent-filter> elements.

+1
source

All Articles