Android

Web OS (Pre / Pixi)

Powered by Google App Engine
Source Code‎ > ‎

Android Service

  

   
 








ServiceDemoService.java
package infinityball.servicedemo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class ServiceDemoService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}


    public void onCreate() {
        super.onCreate();    
    }
    
    public void onDestroy() {
        super.onDestroy();        
    }


private Timer timer = new Timer();


package infinityball.servicedemo;

import java.util.Timer;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class ServiceDemoService extends Service{
	private Timer timer = new Timer();
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void onCreate() {
		super.onCreate();	
	}
	
	public void onDestroy() {
		super.onDestroy();		
	}

}


	public void onCreate() {
		super.onCreate();	
		timer.scheduleAtFixedRate( new TimerTask() {
			public void run() {			
				Log.v("ServiceDemo", "Hello World");				
			}
		}, 0, 3000);
	}


import android.util.Log;


	public void onDestroy() {
		super.onDestroy();	
		if(null != timer)
			timer.cancel();
	}

ServiceDemoGui.java
package infinityball.servicedemo;

import android.app.Activity;
import android.os.Bundle;

public class ServiceDemoGui extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


 public static final ViewGroup createLayout(final ServiceDemoGui sdg) {		
		LinearLayout ll = new LinearLayout(sdg);
		
		final Button startButton = new Button(sdg);
		startButton.setText("Start Service");
		ll.addView(startButton);

		return ll;
    }


private static Intent serviceIntent = null;


		startButton.setOnClickListener( new OnClickListener () {
			@Override
			public void onClick(View v) {
				if(serviceIntent == null) {
					ComponentName comp = new ComponentName(
							sdg.getPackageName(),
							ServiceDemoService.class.getName()
							);
					serviceIntent = new Intent();
					serviceIntent.setComponent(comp); 
		        	sdg.startService(serviceIntent);
				}		        		       
			}			
		});

ServiceDemoGui.java
package infinityball.servicedemo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class ServiceDemoGui extends Activity {
	
	private static Intent serviceIntent = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(ServiceDemoGui.createLayout(this));
    }
    
    public static final ViewGroup createLayout(final ServiceDemoGui sdg) {		
		LinearLayout ll = new LinearLayout(sdg);
		
		final Button startButton = new Button(sdg);
		startButton.setText("Start Service");
		startButton.setOnClickListener( new OnClickListener () {
			@Override
			public void onClick(View v) {
				if(serviceIntent == null) {
					ComponentName comp = new ComponentName(
							sdg.getPackageName(),
							ServiceDemoService.class.getName()
							);
					serviceIntent = new Intent();
					serviceIntent.setComponent(comp); 
		        	sdg.startService(serviceIntent);
				}		        		       
			}			
		});

		ll.addView(startButton);
		return ll;
    }
}


		startButton.setOnClickListener( new OnClickListener () {
			@Override
			public void onClick(View v) {
				if(serviceIntent == null) {
					ComponentName comp = new ComponentName(
							sdg.getPackageName(),
							ServiceDemoService.class.getName()
							);
					serviceIntent = new Intent();
					serviceIntent.setComponent(comp); 
		        	sdg.startService(serviceIntent);
		        	startButton.setText("Stop Service");
				} else {
					sdg.stopService(serviceIntent);
					startButton.setText("Start Service");
					serviceIntent = null;
				}		        		       
			}			
		});

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="infinityball.servicedemo"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ServiceDemoGui"
                  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=".ServiceDemoService" android:enabled="true" />
    </application>
    <uses-sdk android:minSdkVersion="2" />
</manifest>