starting an android native service with titanium module -
i started mobile module project following service class:
package com.eliddell.testservice; import java.lang.reflect.method; import java.util.arraylist; import java.util.list; import java.util.timer; import java.util.timertask; import org.appcelerator.titanium.tibaseservice; import org.appcelerator.kroll.common.log; import android.app.activitymanager; import android.app.service; import android.app.activitymanager.runningserviceinfo; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.graphics.pixelformat; import android.net.wifi.wifimanager; import android.os.batterymanager; import android.os.handler; import android.os.ibinder; import android.text.format.time; import android.view.gravity; import android.view.view; import android.view.windowmanager; import android.widget.relativelayout; import android.widget.textview; public class foregroundservice extends service { /* (non-javadoc) * @see android.app.service#onbind(android.content.intent) */ timer timer; windowmanager.layoutparams handleparams; windowmanager.layoutparams indiparams; view disablestatusbar; windowmanager winmgr; int interval = 50; handler mhandler; boolean secdialog = false; arraylist<string> allowedapps; int scale = -1; int level = -1; int charging = 0; broadcastreceiver batteryreceiver; @override public void oncreate() { log.v("nav service: ", "{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{created"); // start thread running service. note create // separate thread because service runs in process's // main thread, don't want block. make // background priority cpu-intensive work not disrupt our ui. allowedapps = new arraylist<string>(); /*allowedapps.add("com.sapientnitro.lcinstore2");//lc allowedapps.add("com.adobe.reader");//acrobat allowedapps.add("com.dynamixsoftware.printershare");//printer share allowedapps.add("my.handrite.prem");//handrite pro */ mhandler = new handler(); //batman = new batterymanager(); winmgr = (windowmanager) getsystemservice(window_service); //ctime = new time(time.getcurrenttimezone()); } @override public int onstartcommand(intent intent, int flags, int startid) { log.v("nav service: ", "{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{started"); disablestatusbar = new relativelayout(getapplicationcontext()); handleparams = new windowmanager.layoutparams(windowmanager.layoutparams.fill_parent, 50, // allows view displayed on status bar // windowmanager.layoutparams.type_system_alert | windowmanager.layoutparams.type_system_overlay, // keep button presses going background window windowmanager.layoutparams.flag_not_focusable | windowmanager.layoutparams.flag_watch_outside_touch | // enable notification recieve touch events windowmanager.layoutparams.flag_not_touch_modal | // draws on status bar windowmanager.layoutparams.flag_layout_no_limits, pixelformat.transparent); handleparams.gravity = gravity.top; handleparams.y = 50; disablestatusbar.setbackgroundcolor(0xffff0000); winmgr.addview(disablestatusbar, handleparams); statuschecker.run(); // if killed, after returning here, restart return start_redeliver_intent; } @override public ibinder onbind(intent intent) { // don't provide binding, return null return null; } @override public void ondestroy() { unregisterreceiver(batteryreceiver); mhandler.removecallbacks(statuschecker); winmgr.removeview(disablestatusbar); //toast.maketext(this, "service done", toast.length_short).show(); } runnable statuschecker = new runnable() { public void run() { mhandler.postdelayed(statuschecker, interval); } }; private void monitoruser() { string cprocess; try { activitymanager = (activitymanager) getapplicationcontext().getsystemservice( context.activity_service); list<activitymanager.runningtaskinfo> processlist = am.getrunningtasks(5); //screen capture tool am.killbackgroundprocesses("com.diotek.screen_capture"); cprocess = processlist.get(0).baseactivity.getpackagename(); //log.v("","--------------------------------------------------------------------------------"+cprocess); if (cprocess.equals("com.android.settings")) { launchsecurity(); } else if(!allowedapps.contains(cprocess)){ gohome(); } object service = getsystemservice("statusbar"); class<?> statusbarmanager = class.forname("android.app.statusbarmanager"); method collapse = statusbarmanager.getmethod("collapse"); method disable = statusbarmanager.getmethod("disable", integer.type); collapse.setaccessible(true); // disable.invoke(service,1); //collapse .invoke(service); } catch (exception ex) { log.v("window change", "exemption:" + ex); } } public void launchsecurity() { log.v("launching", "((((((((((((((((((((security)))))))))))))))))"); intent = new intent("com.eliddell.testservice.activities.authenticate"); i.setflags(intent.flag_activity_new_task); startactivity(i); } private boolean issecurityservicerunning() { activitymanager manager = (activitymanager) getsystemservice(activity_service); (runningserviceinfo service : manager.getrunningservices(integer.max_value)) { if ("com.eliddell.services_test.services.securityservice".equals(service.service.getclassname())) { return true; } } return false; } public void gohome(){ intent startmain = new intent(intent.action_main); startmain.addcategory(intent.category_home); startmain.setflags(intent.flag_activity_new_task); startactivity(startmain); /* intent = new intent(intent.action_view); i.setcomponent(new componentname("com.sapientnitro.lcinstore2", "com.sapientnitro.lcinstore2.activities.mainactivity")); i.setflags(intent.flag_activity_new_task); startactivity(i);*/ } }
my modul loading titanium app fine , kroll method in module, calls service looks this
@kroll.method public void startforegroundservice() { log.v("{{{{{{{{{{{{{{{{{", "attempting start service"); try{ backoverlayservice = new intent(); backoverlayservice.setclassname("com.eliddell.testservice", "foregroundservice.class"); //start //this.getactivity().getapplicationcontext().startservice(backoverlayservice); tiapplication.getinstance().startservice(backoverlayservice); log.v("testservicemodule{{{{{{{", "try success"); }catch(exception ex) { log.v("testservicemodule{{{{{{{", "exception caught:"+ex); } }
the log shows success, actual service's logs in oncreate , onstart never fire..
any idea doing wrong?
the problem in way setting intent..
instead of:
backoverlayservice = new intent(); backoverlayservice.setclassname("com.eliddell.testservice", "foregroundservice.class");
i use:
backoverlayservice = new intent(tiapplication.getappcurrentactivity(), foregroundservice.class);
Comments
Post a Comment