简体   繁体   中英

getExtra crashing the app in Service

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM