您的位置:首页 > 其它

启动Service--onstartService实例

2015-11-18 16:38 363 查看
package com.example.services;

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

/**
* @author HD
* @date 2015-11-18
* @package_name com.example.services
* @file_name MainActivity.java
*/
public class MainActivity extends Activity implements OnClickListener{
private Button btnOpen;
private Button btnCancel;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOpen = (Button) findViewById(R.id.btnOpen);
btnCancel = (Button) findViewById(R.id.btnCancel);
btnOpen.setOnClickListener(this);
btnCancel.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
switch (v.getId()) {
case R.id.btnOpen:
Intent intent = new Intent(MainActivity.this, MyServices.class);
intent.putExtra("name", "HD");
startService(intent);
break;
case R.id.btnCancel:
Intent intent2 = new Intent(MainActivity.this, MyServices.class);
stopService(intent2);
break;

default:
break;
}
}

}


package com.example.services;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

/**
* @author HD
* @date 2015-11-18
* @package_name com.example.services
* @file_name MyServices.java
*/
public class MyServices extends Service {
private final static String TAG = "MyServices";

public MyServices() {
// TODO 自动生成的构造函数存根
}

@Override
public void onCreate() {
// TODO 自动生成的方法存根
super.onCreate();
Log.i(TAG, "------>>onCreate");
}

@Override
public IBinder onBind(Intent intent) {
// TODO 自动生成的方法存根
Log.i(TAG, "------>>IBinder");
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO 自动生成的方法存根
Log.i(TAG, "------>>onStartCommand");
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
// TODO 自动生成的方法存根
super.onDestroy();
Log.i(TAG, "------>>onDestroy");
}

}


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

<uses-sdk
android:minSdkVersion="17"
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=".MyServices"></service>
</application>

</manifest>


<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.services.MainActivity" >

<Button
android:id="@+id/btnOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="启动服务" />

<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="42dp"
android:text="结束服务" />

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: