I am new to Eclipse and Java development and I am trying to start an activity on boot. I've read multiple threads discussing this topic and while I have managed to start the application on boot, it crashes.
This is my code:
AndroidManifest.xml :
<!--
Below the <manifest> opening tag:
-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!--
Inside the <application> tag:
-->
<receiver android:name="com.example.Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="com.example.service" android:enabled="true" />
Autostart.java :
package com.example;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Autostart extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1)
{
Intent intent = new Intent(arg0, service.class);
arg0.startService(intent);
}
}
service.java :
package com.example;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class service extends Service{
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onStart(Intent intent, int startid)
{
Intent intents = new Intent(getBaseContext(),checker.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
}
}
My checker class has an OnCreate function which alters some settings values.
The only thing that I see when I start my phone is that "YourAppName has stopped working", which means that the application has crashed. I do not see any Toast message.
When I normally open my application and I read the settings that ought to be written on startup, nothing is there.