Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using application service in another application

I have an android application which contains a service. Now I want to access that service in another application. How can I do that? I found this application over 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 i = 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 to optimize the 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 is as 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>
like image 730
Robert Smith Avatar asked May 02 '13 11:05

Robert Smith


People also ask

Is service a separate process in Android?

If the name assigned to this attribute begins with a colon (':'), the service will run in its own separate process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so.

Where do I put manifest services?

You declare a service in your app's Manifest, by adding a <service> element as a child of your <application> element. There's a list of attributes that you can use to control a service's behavior, but as a minimum you'll need to provide the service's name (android:name) and a description (android:description).

What is service differentiate between activity and service?

Services are a unique component in Android that allows an application to run in the background to execute long-running operation activities, on the other hand, an activity, like a window or a frame in Java, represents a single screen with a user interface.


2 Answers

need to use an intent filter for service, say for 'LocalWordService' we 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, we just need to add the line for getting service

Intent intent=new Intent("de.vogella.android.ownservice.local.START_SERVICE");  
                startService(intent);

that's it.

like image 180
Robert Smith Avatar answered Oct 15 '22 15:10

Robert Smith


You need to make your service publicly available so that another application can bind to it. To do that add

android:export="true"

to the manifest entry for the service that you want to share.

You don't need to put that service in a separate process, so you can remove the

android:process=":meinprocess"

unless you want to do that for other reasons.

like image 21
David Wasser Avatar answered Oct 15 '22 16:10

David Wasser