package com.example.testservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
/**
* 服务第一次创建的时候调用
*/
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "服务的onCreate方法被调用", Toast.LENGTH_SHORT).show();
}
/**
* 服务每一次启动的时候调用
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "服务的onStartCommand方法被调用", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Toast.makeText(this, "服务的onDestroy方法被调用", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testservice.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=".MyService"></service>
</application>
</manifest>
<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/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动服务" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止服务" />
</LinearLayout>
package com.example.testservice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button startService_Button;
private Button stopService_Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取开启服务按钮
startService_Button = (Button) findViewById(R.id.button1);
//获取停止服务按钮
stopService_Button = (Button) findViewById(R.id.button2);
//调用点击事件
startService_Button.setOnClickListener(this);
stopService_Button.setOnClickListener(this);
}
/**
* 点击事件
*/
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.button1:
//"开启服务"按钮
Intent startIntent = new Intent(this,MyService.class);
//开启服务
startService(startIntent);
break;
case R.id.button2:
//"停止服务"按钮
Intent stopIntent = new Intent(this,MyService.class);
//停止服务
stopService(stopIntent);
break;
default:
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;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有