Click here to Skip to main content
15,891,951 members
Articles / Mobile Apps / Android

Development and remote installation of Java service for Android Devices

,
Rate me:
Please Sign up or sign in to vote.
4.68/5 (17 votes)
23 Sep 2009CPOL9 min read 50.5K   1.9K   45  
In this article I described how to develop and install remotely Java service for the Android devices
package com.mycompany;

import java.util.Calendar;
import java.util.Timer;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class NotifyService extends Service 
{
	private final static String LOG_TAG = "NotifyService";
	
	private Handler handler = new Handler();
	private NotificationRunnable notificator = new NotificationRunnable();
	private NotifyServiceBinder binder = null;
	private NotifyTask task = null;
	private Timer timer = null;
	
    @Override
    public void onCreate() 
    {
        super.onCreate();
        Log.d(LOG_TAG, "Creating service");
        showNotification("Creating NotifyService");
        
        binder = new NotifyServiceBinder(handler, notificator);
        task = new NotifyTask(handler, notificator);
        timer = new Timer();
    }
    
    @Override
    public void onStart(Intent intent, int startId)
    {
    	super.onStart(intent, startId);
    	Log.d(LOG_TAG, "Starting service");
    	showNotification("Starting NotifyService");
    	
        timer.scheduleAtFixedRate(task, Calendar.getInstance().getTime(), 30000);
    }
    
    @Override
    public void onDestroy()
    {
    	super.onDestroy();
    	Log.d(LOG_TAG, "Stopping service");
    	showNotification("Stopping NotifyService");
    	
    	timer.cancel();
    }
	
	@Override
	public IBinder onBind(Intent intent) 
	{
		Log.d(LOG_TAG, "Binding service");
		return binder;
	}
	
    public void showNotification(String message) 
    {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }
    
    public class NotificationRunnable implements Runnable 
    {
        private String message = null;
        
        public void run() 
        {
            if (null != message) 
            {
                showNotification(message);
            }
        }
        
        public void setMessage(String message) 
        {
            this.message = message;
        }
    }  
    
    public class NotifyServiceBinder extends Binder implements INotifyService 
    {
    	private Handler handler = null;
    	private NotificationRunnable notificator = null;    	
    	
    	public NotifyServiceBinder(Handler handler, NotificationRunnable notificator)
    	{
    		this.handler = handler;
    		this.notificator = notificator;
    	}
    	
    	public void sendNotification(String message)
    	{
    		if (null != notificator)
    		{
        		notificator.setMessage(message);
                handler.post(notificator);
    		}
    	}

		@Override
		public IBinder asBinder() 
		{
			return this;
		}
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Apriorit Inc.
United States United States
ApriorIT is a software research and development company specializing in cybersecurity and data management technology engineering. We work for a broad range of clients from Fortune 500 technology leaders to small innovative startups building unique solutions.

As Apriorit offers integrated research&development services for the software projects in such areas as endpoint security, network security, data security, embedded Systems, and virtualization, we have strong kernel and driver development skills, huge system programming expertise, and are reals fans of research projects.

Our specialty is reverse engineering, we apply it for security testing and security-related projects.

A separate department of Apriorit works on large-scale business SaaS solutions, handling tasks from business analysis, data architecture design, and web development to performance optimization and DevOps.

Official site: https://www.apriorit.com
Clutch profile: https://clutch.co/profile/apriorit
This is a Organisation

33 members

Written By
Software Developer ApriorIT
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions