Android : Android start service after reboot without activity and loop a function

on Sunday, September 21, 2014


Hello :) well I know that parts of these questions have been answered here and I could figure it out that it is partly working but I face some problems.


Here is a picture of the application behavior which I want to create.


enter image description here


A quick explanation to this:


1. User -> APP-Click: If the User clicks on the APP, the APP should start the MainActivity with some content and at the same time, the APP should start a process which is executing a function every 10 seconds.


2. Reboot: If the smartphone reboots, the APP shouldn´t start the Activity. The APP should just start the process which is executing the function every 10 seconds.


My problem is, that after rebooting, the process starts and the function gets executed a few times and then it just stopps. I am not getting any error message on stuff like this, the process is also still active within my phone but the function won´t get executed anymore.


Here is my source code:


autostart.java



public class autostart extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
Intent intent = new Intent(arg0,service.class);
arg0.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)
{
new Timer().scheduleAtFixedRate(new TimerTask(){

@Override
public void run()
{
Message msg = new Message(getBaseContext());
msg.read();
}
}, 0, 20000);//put here time 1000 milliseconds=1 second

Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}


MainActivity



public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.main --> this line commented out
Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show();
}
}


Summary: If I click on the icon of the APP the MainActivity is shown. If I restart the smartphone, the APP doesn´t appear and the service will be started. The function of sending some messages get´s executed a few times. Everything seems to be working like it should but after a few times of sending this message it just stops without any error. The service is still shown within my smartphone but the functon which get´s executed every 20 seconds won´t be executed anymore or even won´t send any messages anymore. Also after clicking on the APP which shows a simple page, the service or even the messages won´t be sent again.


I could really need some help with this :) thanks in advance and have a nice day =)


0 comments:

Post a Comment