您的位置:首页 > 移动开发 > Android开发

android初接触之service1

2016-01-14 21:06 441 查看
初学参考http://blog.csdn.net/wtao158/article/details/5149721http://www.cnblogs.com/gzgg/archive/2012/02/20/2359485.html的解释说明

1.对于android的service的理解

android中的sevice是android用于做后台服务的便于进行多任务同时运行

2.实现方式有两种

1>启动某个服务使用startService();启动某个服务调用了服务中onCreate()方法进行启动

2>绑定某个服务到调用的客户类绑定如果调用这个客户类被销毁,Service 也会被销毁。用这个方法的一个好处是,bindService() 方法执行后 Service 会回调上边提到的 onBind() 方发,你可以从这里返回一个实现了 IBind 接口的类,在客户端操作这个类就能和这个服务通信了,比如得到 Service 运行的状态或其他操作。如果 Service 还没有运行,使用这个方法启动 Service 就会 onCreate() 方法而不会调用 onStart()。

3.代码实现:

方式1:创建一个activity类让他继承service这个抽象类重写onCreate()和onDestroy()这两个方法

package com.example.test;

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

public class ServiceTest extends Service {

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("服务启动");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("服务注销");
}
}


在activity_main.xml中加入连个button用于启动和停止服务

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity" >

<Button
android:id="@+id/startService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="启动服务" />

<Button
android:id="@+id/destoryService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/startService"
android:layout_alignRight="@+id/startService"
android:layout_below="@+id/startService"
android:text="注销服务" />

</RelativeLayout>


在MainActivity.java中给button加入监听和调用service

package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity implements OnClickListener {
private Button startService,destroyService;
private Intent IntentService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建调用服务的Intent
IntentService = new Intent(this,ServiceTest.class);
//获取button按钮
startService = (Button) findViewById(R.id.startService);
destroyService = (Button) findViewById(R.id.destoryService);
startService.setOnClickListener(this);
destroyService.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.startService:
//启动服务
startService(IntentService);
break;
case R.id.destoryService:
//停止服务
stopService(IntentService);
break;
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


在AndroidMainfest.xml中加入调用服务的seriver标签

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.
9cf3
test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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="ServiceTest"></service>

</application>

</manifest>


这样点击按钮就可以在logcat中显示了注意要在allmessages中才能看到
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: