I4

HMO-? Service

'JKft*. if^&^jUM^JjP 4 'Hfcfil. i»}ljffl start Service ( JFS&JRfc K stop_Service

(ftVlUM&K bind Service ill unbind Service (»riFIRfc) ^SMW^nT:

<?xml version="1.0" encoding-nutf-8"?> <LinearLayout xmlns : Android="http: //schemas . Android. com/apk/res/Android" Android:orientation—"vertical" Android:layout width="fill_parent" Android:layout_height="fill_parent"> <TextView

Android:layout width-"fill_parent" Android:layout height="wrap_content" Android:text-"^string/hello" />

<Button Android:id + id/start_Service" Android:layout width = "fill parent" Android:layout height="wrap content" Android:text="Start Service" />

<Button Android:id="@+id/stop Service" Android:layouL_width="fi11 parent" Android:layout_height="wrap content" Android:text-"Stop Service" />

<Button Android:id="@*id/bind Service" Android: layout. width="fill_parent" Android:layout height="wrap_content" Android:text="Bind Service" />

<Button Android: id- "@+id/unbind_Service" Android: .1 ayout width=" til 1_ parent" Android:layout_height="wrap content" Android:text ="Unbind Service" />

</LinearLayout>

^immft:

m LinearLayout jmW. i 4 tf&Ul. 4 ttflMfc.

B mimxn- AndroidManifestxml Htf ffifi'Mfa Service M'JDfj. h

<?xml version®"1.0" encoding="utf-8"?>

cmanifest xmlns:Android "http://schemas.Android.com/apk/res/Android" package-"com.iceskysl.TestServiceHolder" Android:versionCode-"1" Android:versionName-"1.0.0">

<application Android:icon="@drawable/icon" Android:label="@string/app name"> <activity Android:name=".TestServiceHolder" Android:label "@string/app_name"> <intent-Ciiter>

<action Android:name="Android.intent.action.MAIN" />

<category Androi d:name="Android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<Service Android:enabled»"true" Android:name=".TestService" />

</appiication> </manifest>

M KIZM ft:

^fVi^f^1 f1 - Hiflpio f ~ff"<Service Android:enabIed="true" Android:namc=".TestService" />" JB^tfBj^flK^-njy^T.

package com.iceskysl.TestServiceHolder;

import Android.app.Notification; import Android. app. Not.i f i cat ionManager ; import Android.app.Pendinglntent; import Android.app.Service; import Android.content.Intent; import Android.os.Binder; import Android.os.IBinder; import Androi d . ut.il. Log ;

public class TestService extends Service {

private static final String TAG = "TestService"; private NotificationManager nm;

Log . e (TAG, =======> TestService.onBi.nd") ;

return null; )

public class LocalBinder extends Binder { TestService getService() {

return TestService.this; }

(¿Override public boolean onUnbind(Intent i) {

Log.e(TAG, "============> TestService.onUnbind");

return false;

{¿Override public void onRebind(Intent 1) {

Log.e(TAG, "============> TestService.onRebind");

^Override public void onCreateO {

Log. e (TAG, ••============> TestService.onCreate");

_nm = (NotificationManager) getSystemService(NOTIFICATION SERVICE);

showNotification(); }

^Override public void onStart (Intent .intent, int startld) { Log . e (TAG, TestService.onStart");

OOverride public void onDastroyO {

nm.cancel (R.st ring.Servicestarted); Log.e(TAG, "============> TestService.onDestroy");

private void showNotification() {

Notification notification = new Notification(R.drawable.face_l, "Service started", Systern.currentTimeMi11 is());

Pendinglntent content I ntent = Pendinglntent.getActivity(this, 0, new Intent(this, TestServiceHolder.class), 0);

// must set this for content view, or will throw a exception notification.setLatestEventInfo(this, "Test Service", "Service started", content Intent);

_nrr.. notify (R. string. Service_started, notification) ; )

H imMM:

t^ftyi'l^» frJii jm.m-1 Android.app.Service ft TestService, T % onStart, onDestroy

^//ft, LOG tfrJjjm&mmm «fflSJ Notification®^ fW

Service YriSWtt^, F Notification MrtStf.WAlM«

80verride public void onCreateO {

Log.e(TAG, "============> TestService.onCreate");

_nm - (NotificationManager) getSystemService(NOTIFICATION SERVICE); showNotification();

hifriWftffl®. 4: Service W^^Wfflatt + Jta it\'C\l log MilH'J. A'®»]ft ffo

Public class LocalBinder extends Binder I TestService getServiceO I return TestService.this;

0 0

Post a comment