Thanks for the reply. Here is mine:
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.
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.
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.
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 ...
All you need is an action. Read intent documentation.
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);
}
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?