Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Creating Android services with START_STICKY?

$
0
0

Hi,

I have been trying to find a way to create a sticky Android service in an app without much success. Has anyone been successful in this?

I have tried changing the template for JSIntervalService.java to something like this: (I know I should not be doing this but I do need the functionality and could not find another way)

package <%- appid %>;
 
import ti.modules.titanium.android.TiJSIntervalService;
import android.content.Intent;
import android.app.Service;
import org.appcelerator.titanium.TiApplication;
 
 
public final class <%- service.classname %> extends TiJSIntervalService {
    public <%- service.classname %>() {
        super("<%- service.url %>");
    }
 
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
 
    // When restarted from Android the intent appears to be null
    if(intent == null)
    {
        Intent serviceIntent = new Intent();
        String fullServiceName = "com.myapp.<%- service.classname %>";
 
        serviceIntent.setClassName(TiApplication.getInstance().getApplicationContext(), fullServiceName);
 
        // It appears to need extra parameters ... add default interval
        serviceIntent.putExtra("interval", 45*60*1000L);
        intent = serviceIntent;
    }
 
    super.onStartCommand(intent,flags,startId);
    return START_STICKY;
    }
}
This is partially working in a sense that Android would try to restart the service, however the service would not start.

Without the code inside the if(intent == null) {} I get a null pointer exception. With it things appear to work better but not really - no obvious errors except that the service is no running.

It feels like there should be something one can do inside that if() to make the call to super.onStartCommand() work but I cannot figure out what that should be. Perhaps some extra parameters through putExtra() ?!?!

I would appreciate any suggestions on how to make this work.

Thanks!


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>