Android_2.2_eclipse_service_simple_demo

service简单应用,Log中打印了启动和结束的调用步骤

mainActivity

package com.gongsi.service18;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Service18Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startButton = (Button)findViewById(R.id.startService);
Button stopButton = (Button)findViewById(R.id.stopService);
startButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
doStart();
}
});
stopButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
doStop();
}
});
}
//启动服务
private void doStart(){
Intent intent = new Intent(this,MService.class);
Bundle bundle = new Bundle();
bundle.putString("MusicName", "hello.mp3");
intent.putExtras(bundle);
this.startService(intent);
}
//停止服务
private void doStop(){
Intent intent = new Intent(this,MService.class);
this.stopService(intent);
}
}

Service

package com.gongsi.service18;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class MService extends Service {

/* 如果onBind返回null表示不能通过bindService调用 */
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.v("Service:","In onCreate");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.v("Service:","In onStartCommand");
//根据intent中得到bundle数据,在数据中根据key去的数据
Bundle bundle = intent.getExtras();
String name = bundle.getString("MusicName");
Log.v("info", name);
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.v("Service:","In onDestroy");
super.onDestroy();
}

}

main.xml

<?xml version="1.0" encoding="utf-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:textSize
="25dp"
android:text
="@string/hello"/>
<Button android:text="StartService" android:id="@+id/startService"
android:layout_width
="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="StopService" android:id="@+id/stopService"
android:layout_width
="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

manifest主要添加service项目

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package
="com.gongsi.service18"
android:versionCode
="1"
android:versionName
="1.0">
<uses-sdk android:minSdkVersion="8"/>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Service18Activity"
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=".MService"
android:enabled
="true"></service>
</application>
</manifest>





原文地址:https://www.cnblogs.com/flyingsir/p/2263765.html