【アンドロイド】サービス

次のようにMyServiceUtil.javaコードは次のとおりです。

package org.lxh.demo;
 
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
 
public class MyServiceUtil extends Service {//继承Service类
 
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
 
	@Override
	public void onCreate() {
		System.out.println("***Service onCreate()");
	}
 
	@Override
	public void onDestroy() {
		System.out.println("***Service onDestory()");
	}
 
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("***Service onStart()-->Intent=" + intent
				+ ",startId=" + startId);
		return Service.START_CONTINUATION_MASK;//继续执行Service
	}
}

次のようにmain.xmlコードは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="启动Service" />
 
    <Button
        android:id="@+id/stop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="停止Service" />
 
</LinearLayout>

次のようにJavaコード:

package org.lxh.demo;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class Hello extends Activity {
	private Button start = null;
	private Button stop = null;
 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		super.setContentView(R.layout.main); // 设置要使用的布局管理器
		this.start = (Button) super.findViewById(R.id.start);
		this.stop = (Button) super.findViewById(R.id.stop);
		this.start.setOnClickListener(new StartOnClickListener());
		this.stop.setOnClickListener(new StopOnClickListener());
 
	}
 
	private class StartOnClickListener implements OnClickListener {
 
		public void onClick(View arg0) {
			Hello.this
					.startService(new Intent(Hello.this, MyServiceUtil.class));
 
		}
 
	}
 
	private class StopOnClickListener implements OnClickListener {
 
		public void onClick(View arg0) {
			Hello.this.stopService(new Intent(Hello.this, MyServiceUtil.class));
		}
 
	}
}

私たちは、configure権限する必要があります。

<?xml version="1.0" encoding="utf-8"?>
<manifest 
	xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.lxh.demo" 
	android:versionCode="1" 
	android:versionName="1.0">
	<uses-sdk android:minSdkVersion="10" />
	<application 
		android:icon="@drawable/icon" 
		android:label="@string/app_name">
		<activity 
			android:name=".Hello" 
			android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<strong><span style="color:#ff0000;"><service android:name=".MyServiceUtil"></service></span></strong>
	</application>
	
</manifest>

例を実行すると、次のされている:
(1)最初に"スタートサービス"ボタンをクリックしてください
3月18日09:58:00.058:I /をSystem.out(692):***サービスのonCreate()
3月18日09:58:00.058:Iを/System.out(692):*** ONSTART(サービス) - >テントテント= {= org.lxh.demo CMP / .MyServiceUtil} = 1 startID
(2)クリックを繰り返した"開始サービス"を
3月18日10 :06:06.227:I /のSystem.out(727):*** ONSTARTサービス() - >、startID 2 =インテント= {= org.lxh.demo CMP / .MyServiceUtil}テント
(3)「エンドサービスクリック「
3月18日10:06:10.647:I /をSystem.out(727):***サービスonDestory()

ここに画像を挿入説明

実行中のプログラムを見ることができます開きます。

公開された73元の記事 ウォン称賛17 ビュー10000 +

おすすめ

転載: blog.csdn.net/qq_40110781/article/details/104956131