I have a basic Android application that is supposed to reboot the phone continuously a set number of times. In order to do this, I need the application to be started upon phone startup. In order to that, I essentially followed the instructions found here, adding the permissions to the manifest and creating a BroadcastReceiver class that starts the activity. Here is 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 the 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 appears to be working properly. That is, though my emulator isn't rooted and the phone doesn't execute the reboot commands correctly, I do see that, upon startup, the correct activity is started.
Now, when I'm trying it on a particular system running Android 4.0.4, everything in the application is working properly EXCEPT boot upon startup. Any ideas?
I tried to eliminate the possibility of any hardware problems (since I'm not using a commercially-released phone) by installing another application that boots upon startup and confirming that it does indeed boot upon startup, and it does indeed show up under running apps as a cached process after startup.
I would appreciate any help. Let me know if you need any additional information.
There are a number of issues here.
First, you neglected to post
StartMyServiceAtBootReceiver
, the component you are expecting to get control at boot time, so we cannot comment on whether there are any particular problems with it.Second, unless something has explicitly executed one of your components (e.g., the user launched
MainActivity
from the home screen),StartMyServiceAtBootReceiver
will never be invoked, on Android 3.1+. Make sure you run your activity before trying a reboot, and see if that helps.Third, you implemented a constructor on
StartupManager
, which is a bad idea. Please move this logic toonCreate()
.Fourth, your code will likely crash in that constructor, as
getApplication()
will not return a valid value at this point in the code, particularly since you failed to chain to the superclass' constructor. Again, moving this code toonCreate()
will help here.Fifth, starting an activity from
onCreate()
of a service (let alone its constructor) is very unusual and may not be appreciated by the user. Moreover, if that service is not doing anything else, you could just as easily start that activity fromStartMyServiceAtBootReceiver
and skipStartupManager
entirely.Sixth, you have
<intent-filter>
elements on your services, as if you are expecting third party developers to invoke those services. If that is the case, fine. If not, please remove the<intent-filter>
elements and use explicitIntents
within the rest of your app code to refer to them (e.g.,new Intent(this, StartupManager.class)
), for better security. Or, addandroid:exported="false"
to those services, though that is automatic if you remove the<intent-filter>
elements.