Stopping traccar client programmatically

ghotik4 years ago

Hi. I'm writing an Android App that, under certain conditions, must start a tracing service and, on other conditions, stop it.
I'm trying to use the traccar client as is, by starting and stopping it from my external App.
Starting the traccar client works. I used this java code (extract from my App source):

PackageManager pm = mContext.getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("org.traccar.client");
intent.addCategory(Intent.CATEGORY_LAUNCHER);
mContext.startActivity(intent);

Now I'm trying to stop / kill the client, I used this code:

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
am.killBackgroundProcesses("org.traccar.client");

but this procedure doesn't work, the traccar client stays alive and keeps sending traces. Is there a better and working way to stop it from the App that launched it?

Anton Tananaev4 years ago
  1. Why do you need external app? Just take the code and integrate it directly into your own app.
  2. Even if you use external app, what you are doing doesn't make any sense. You have to send proper intents for start/stop.
ghotik4 years ago

Thanks for the reply. Here is mine:

  1. I like the idea of using traccar as a service provider, pretty much like you do in Android when you integrate services with different implementation (like a custom photo app, or a mail client) via intent messages. The advantage is clear: if either traccar or my application evolves there would be no need to reassemble all stuff and test all functionalities, each all will keep doing its job. And, to be honest, I'm clumsy enough in Android environment to be scared of the integration of all problems. "Dividi et impera", it's a good principle.

  2. I know what I'm doing doesn't make much sense, given the fact that it doesn't work. It was just an example of the troubles where I put myself. Actually, the first part works: startActivity makes traccar client starting and running, the problem is that stopping it by killing is not fair and it doesn't work. After I wrote my post, I found some reference here https://www.traccar.org/forums/topic/tasker/page/3/ to the possibility to start and stop the tracing activity programmatically via Intents, but my attempts of coding a start/stop procedure have failed so far. I have some difficulty to map the Tasker field values to the proper programmatic actions and just hoped someone had some source code sample.

Anton Tananaev4 years ago

The reference you found is the right direction. I doubt anyone would actually write code for you. I would recommend to check StackOverflow for some example on how to execute intents.

ghotik4 years ago

Certainly I don't expect anyone to write code for me, but as far as I can understand you can start and stop the traccar service either by tapping the traccar shortcuts on the screen or by sending intent messages, and we should have a protocol here. But the content of the intent text message (that is, the string value used in a statement like intent.putExtra(Intent.EXTRA_TEXT, stringValue) ) doesn't seem clearly defined. I wrote code that sends some text to traccar (like "action:stop"), but the reaction is either null or the launch of the user main activity. The shortcuts I would need to activate programmatically are "Stop service" and "Start service", but I still wonder how ...

Anton Tananaev4 years ago

All you need is an action. Read intent documentation.

ghotik4 years ago

Thanks, I've followed your advice and I've done it. If someone could be interested, this is a java function that starts (when action is true) or stops (when action is false) the traccar client on Android.

private void traccarAction(boolean action) {
    Intent intentTraccar = new Intent(Intent.ACTION_MAIN).addCategory(
            Intent.CATEGORY_LAUNCHER).setClassName("org.traccar.client",
            "org.traccar.client.ShortcutActivity").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            .addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("org.traccar.client",
                    "org.traccar.client.ShortcutActivity"));
    Bundle bundle = new Bundle();
    bundle.putBoolean("shortcutAction", action);
    intentTraccar.putExtras(bundle);
    getApplicationContext().startActivity(intentTraccar);
}