2

I search a lot and I tried several ways but I couldn't find anything that avoid my error.

I'm working on app for my learning, where is an String in my MainActivity and then I call it in my Service. I tried something like this:

This next one goes in my myService.class

//In my myService.class that extends Service

public class myService extends Service{

AlarmManager manager;
PendingIntent pintent;
String te;


@Override
public void onCreate()
{
    manager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
    pintent = PendingIntent.getBroadcast( this, 0, new Intent("blahblah"), 0 );}

@Override
public int onStartCommand(Intent intent, int flags, int startid)
{

    super.onStartCommand(intent, flags, startid);
            te = intent.getStringExtra("tst"); //if I change this line to te = "something", everything goes fine
    BroadcastReceiver receiver = new BroadcastReceiver() {
           @Override 
            public void onReceive( Context context, Intent intent )
           {
                Toast.makeText(getApplicationContext(),te, Toast.LENGTH_SHORT).show();
           }
    };
            this.registerReceiver(receiver, new IntentFilter("blahblah") );

            // set alarm to fire 30 min and waits 2 min
                 manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 1000*30*60, 1000*2*60, pintent);
    return START_STICKY;}

public IBinder onBind(Intent p1)
{
    // TODO: Implement this method
    return null;
}

}

Here the code runs perfectly, but when I exit my App, it crashes. After 1 minute, my device shows again that my App crashes, confirming my app "successfully" ran into background. What is wrong with it? I also learned I could use IntentService instead of Service, wich one is better for long tasks and what is the difference between them ?

EDIT***

I received the following error: java.lang.NullPointerExeption. So I change this:

te = intent.getStringExtra("tst");

To this:

try { te = intent.getStringExtra("tst"); } catch(NullPointerException e) {}

When I changed it my app works with any error, but The problem is: I need to retrieve my String from my MainActivity, when I close my app my service runs without errors but my "te" String takes null valor, what can I do to "save" my String in my service, to be able to use it and keeping showing the "working" message after I close my App ? Should I use SharedPreferences ?

Hope I was clear.

Murillo Ferreira
  • 1,423
  • 1
  • 16
  • 31
  • post the crash logs, please. – Steelight Jul 15 '13 at 08:39
  • I don't have any Logs, tomorrow I can use the Eclipse to see it, now I'm using the AIDE (an IDE that works directly in my device), so I compile it and run it. Once the App are installed, I press my button, the AlarmManager works as expected, but when I exit from my app a message appears: MyApp stopped – Murillo Ferreira Jul 15 '13 at 08:45

1 Answers1

1

IntentService is different from Service.

IntentService Self kills the service as it finishes the task. Whereas Service runs for ever unless you kill it.

For my coding experience, I would use IntentService only for small tasks that run for a couple of seconds and use Service for long running one and call StopSelf() as needed.

Kindly post the log to answer your problem

Naresh
  • 3,174
  • 1
  • 17
  • 22
  • 1
    Here the `intent` is null. I'll be null if the android OS starts the service. For more info check http://stackoverflow.com/questions/8421430/reasons-that-the-passed-intent-would-be-null-in-onstartcommand – Naresh Jul 16 '13 at 04:23
  • 1
    This solved my problem, the only thing I needed to was change the return value to START_REDELIVER_INTENT. Thanks very much for your help. – Murillo Ferreira Jul 16 '13 at 06:50