android - using application service in another application -
i have android application contains service. want access service in application. how can that? found application on net. please find code snippets bellow
1> public class localwordservice extends service { private final ibinder mbinder = new mybinder(); private arraylist<string> list = new arraylist<string>(); @override public int onstartcommand(intent intent, int flags, int startid) { random random = new random(); if (random.nextboolean()) { list.add("linux"); } if (random.nextboolean()) { list.add("android"); } if (random.nextboolean()) { list.add("iphone"); } if (random.nextboolean()) { list.add("windows7"); } if (list.size() >= 20) { list.remove(0); } return service.start_not_sticky; } @override public ibinder onbind(intent arg0) { return mbinder; } public class mybinder extends binder { localwordservice getservice() { return localwordservice.this; } } public list<string> getwordlist() { return list; } } 2> public class myschedulereceiver extends broadcastreceiver { // restart service every 30 seconds private static final long repeat_time = 1000 * 30; @override public void onreceive(context context, intent intent) { alarmmanager service = (alarmmanager) context .getsystemservice(context.alarm_service); intent = new intent(context, mystartservicereceiver.class); pendingintent pending = pendingintent.getbroadcast(context, 0, i, pendingintent.flag_cancel_current); calendar cal = calendar.getinstance(); // start 30 seconds after boot completed cal.add(calendar.second, 30); // // fetch every 30 seconds // inexactrepeating allows android optimize energy consumption service.setinexactrepeating(alarmmanager.rtc_wakeup, cal.gettimeinmillis(), repeat_time, pending); // service.setrepeating(alarmmanager.rtc_wakeup, cal.gettimeinmillis(), // repeat_time, pending); } } 3> public class mystartservicereceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { intent service = new intent(context, localwordservice.class); context.startservice(service); } } 4> public class myservice extends service { @override public void oncreate() { super.oncreate(); } @override public int onstartcommand(intent intent, int flags, int startid) { return super.onstartcommand(intent, flags, startid); } @override public ibinder onbind(intent intent) { return null; } }
and manifest file follows
<application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="de.vogella.android.ownservice.local.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <service android:name="de.vogella.android.ownservice.local.localwordservice" android:icon="@drawable/ic_launcher" android:label="@string/service_name" > </service> <service android:name="de.vogella.android.ownservice.local.myservice" android:process=":meinprocess" android:icon="@drawable/ic_launcher" android:label="@string/service_name" > </service> <receiver android:name="de.vogella.android.ownservice.local.myschedulereceiver" > <intent-filter> <action android:name="android.intent.action.boot_completed" /> </intent-filter> </receiver> <receiver android:name="de.vogella.android.ownservice.local.mystartservicereceiver" > </receiver> </application>
need use intent filter service, 'localwordservice' declare
<service android:enabled="true" android:exported="true" android:name="de.vogella.android.ownservice.local.myservice"> <intent-filter> <action android:name="de.vogella.android.ownservice.local.start_service" /> </intent-filter> </service>
in calling app, need add line getting service
intent intent=new intent("de.vogella.android.ownservice.local.start_service"); startservice(intent);
that's it.
Comments
Post a Comment