Android Open Source - latrobe-datacapture-dir Main Service






From Project

Back to project page latrobe-datacapture-dir.

License

The source code is released under:

MIT License

If you think the Android project latrobe-datacapture-dir listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.DataCaptureApp.testing;
/*from w  w w .  j  av  a  2 s.co  m*/
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import com.example.DataCaptureApp.R;


/**
 * Created by Tom on 3/09/2014.
 */
public class MainService extends Service implements IServiceListener
{
    public static final String START_ACTION = "com.example.DataCaptureApp.START";
    private static final String TAG = "MainService";
    private static final int NOTIFICATION_ID = 1;

    private IServiceListener mListener;
    private boolean mRunning = false;

    private RandomService mRandomService;
    private ServiceConnection mRandomServiceConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            mRandomService = ((RandomService.LocalBinder)binder).getService();
            mRandomService.setListener(MainService.this);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mRandomService = null;
        }
    };

    private NotificationManager mNotificationMgr;
    private Notification mNotification;

    public class LocalBinder extends Binder
    {
        public MainService getService()
        {
            return MainService.this;
        }
    }

    private LocalBinder mBinder = new LocalBinder();

    @Override
    public IBinder onBind(Intent intent)
    {
        Log.d(TAG, "Bound");
        return mBinder;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        Log.d(TAG, "Created");

        mNotificationMgr = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        int icon = R.drawable.icon;
        CharSequence text = "Random Numbers";
        long when = System.currentTimeMillis();

        // Initialise notification
        mNotification = new Notification(icon, text, when);
        mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
        mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
        mNotification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
    }

    /*
     * Implement this method so service runs indefinitely until stopped!
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        // Start RandomService and bind
        Log.d(TAG, "onStartCommand");
        // Start commands only valid from MainActivity with a bundle
        if(intent != null || intent.getExtras() == null)
        {
            mRunning = true;
            Intent startIntent = new Intent(this, RandomService.class);
            startIntent.setAction(START_ACTION);
            startIntent.putExtras(intent.getExtras());
            bindRandomService(startIntent);
        }
        else
        {
            Log.d(TAG, "Null intent, stopping self");
            stopSelf();
        }
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy()
    {
        Log.d(TAG, "Destroying");
        // Clean up RandomService
        unbindRandomService();
        stopService(new Intent(this, RandomService.class));
        mRandomService = null;
        super.onDestroy();
    }

    @Override
    public boolean onUnbind(Intent intent)
    {
        Log.d(TAG, "Unbinding");
        setListener(this);
        return true;
    }

    public void onServiceData(Service service, String data)
    {
        if(service == this)
        {
            // Update notification
            updateNotification(data);
        }
        else if(mListener != null)
        {
            // Data is from RandomService, pass on to listener
            mListener.onServiceData(this, data);
        }
    }

    private void bindRandomService(Intent intent)
    {
        bindService(intent, mRandomServiceConn, Context.BIND_AUTO_CREATE);
    }

    private void unbindRandomService()
    {
        if(mRandomService != null)
        {
            unbindService(mRandomServiceConn);
        }
    }

    private void updateNotification(String newText)
    {
        Context context = getApplicationContext();
        CharSequence title = "Random Numbers!";
        CharSequence text = newText;

        Intent notifIntent = new Intent(this, ServiceTestActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notifIntent, 0);

        // Initialise notification
        mNotification.setLatestEventInfo(context, title, text, contentIntent);
        mNotificationMgr.notify(NOTIFICATION_ID, mNotification);
    }

    /*
     * API methods
     */

    public void setListener(IServiceListener listener)
    {
        mListener = listener;
        if(listener == this)
        {
            updateNotification("Starting...");
            startForeground(NOTIFICATION_ID, mNotification);
        }
        else
        {
            stopForeground(true);
        }
    }

    public boolean isRunning()
    {
        return mRunning;
    }
}




Java Source Code List

com.example.DataCaptureApp.AdvConfigActivity.java
com.example.DataCaptureApp.ConfigActivity.java
com.example.DataCaptureApp.MasterActivity.java
com.example.DataCaptureApp.MasterService.java
com.example.DataCaptureApp.SlaveActivity.java
com.example.DataCaptureApp.SlaveService.java
com.example.DataCaptureApp.data.DataServiceConnection.java
com.example.DataCaptureApp.data.DataService.java
com.example.DataCaptureApp.data.DataTransform.java
com.example.DataCaptureApp.data.Data.java
com.example.DataCaptureApp.data.Event.java
com.example.DataCaptureApp.data.FailedInitialisationException.java
com.example.DataCaptureApp.data.IDataEventListener.java
com.example.DataCaptureApp.data.IDataListener.java
com.example.DataCaptureApp.data.IDataSource.java
com.example.DataCaptureApp.data.IDataTransform.java
com.example.DataCaptureApp.data.IEventListener.java
com.example.DataCaptureApp.data.IEventSource.java
com.example.DataCaptureApp.services.BluetoothConnectivityService.java
com.example.DataCaptureApp.services.BluetoothThread.java
com.example.DataCaptureApp.services.DataDbContract.java
com.example.DataCaptureApp.services.DataDbHelper.java
com.example.DataCaptureApp.services.DataStoreService.java
com.example.DataCaptureApp.services.HttpThread.java
com.example.DataCaptureApp.services.RemoteConnectivityService.java
com.example.DataCaptureApp.services.SensorSampleService.java
com.example.DataCaptureApp.services.SensorSampler.java
com.example.DataCaptureApp.testing.BluetoothActivity.java
com.example.DataCaptureApp.testing.DataStoreActivity.java
com.example.DataCaptureApp.testing.DataTester.java
com.example.DataCaptureApp.testing.IServiceListener.java
com.example.DataCaptureApp.testing.MainService.java
com.example.DataCaptureApp.testing.MasterTestActivity.java
com.example.DataCaptureApp.testing.RandomService.java
com.example.DataCaptureApp.testing.RemoteActivity.java
com.example.DataCaptureApp.testing.SensorSampleActivity.java
com.example.DataCaptureApp.testing.ServiceTestActivity.java
com.example.DataCaptureApp.testing.SlaveTestActivity.java
com.example.DataCaptureApp.testing.TestActivity.java
com.example.DataCaptureApp.transforms.AggregatorDataTransform.java
com.example.DataCaptureApp.transforms.ArithmeticDataTransform.java
com.example.DataCaptureApp.transforms.ArrayCollectDataTransform.java
com.example.DataCaptureApp.transforms.ArraySplitDataTransform.java
com.example.DataCaptureApp.transforms.DeserialiseDataTransform.java
com.example.DataCaptureApp.transforms.FieldCopyDataTransform.java
com.example.DataCaptureApp.transforms.FieldModifyDataTransform.java
com.example.DataCaptureApp.transforms.FieldRenameDataTransform.java
com.example.DataCaptureApp.transforms.IntervalAggregatorDataTransform.java
com.example.DataCaptureApp.transforms.PackDataTransform.java
com.example.DataCaptureApp.transforms.QuaternionDifferenceDataTransform.java
com.example.DataCaptureApp.transforms.RemoveDataTransform.java
com.example.DataCaptureApp.transforms.SetDataTransform.java
com.example.DataCaptureApp.transforms.UnpackDataTransform.java
com.example.DataCaptureApp.utils.BroadcastDataSource.java
com.example.DataCaptureApp.utils.ByteUtils.java
com.example.DataCaptureApp.utils.DataEventHandler.java
com.example.DataCaptureApp.utils.DataHandlerThread.java
com.example.DataCaptureApp.utils.JSONReader.java
com.example.DataCaptureApp.utils.Quaternion.java
com.example.DataCaptureApp.utils.SerialisationUtils.java