package com.example.servicetest;
import com.example.servicetest.service.MyService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
/** 标志位 */
private static String TAG = "com.example.servicetest.MainActivity";
/** 启动服务 */
private Button mBtnStart;
/** 绑定服务 */
private Button mBtnBind;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* init the View
*/
private void initView() {
mBtnStart = (Button) findViewById(R.id.startservice);
mBtnBind = (Button) findViewById(R.id.bindservice);
mBtnStart.setOnClickListener(this);
mBtnBind.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
// 启动服务的方式
case R.id.startservice:
startService(new Intent(MyService.ACTION));
break;
// 绑定服务的方式
case R.id.bindservice:
bindService(new Intent(MyService.ACTION), conn, BIND_AUTO_CREATE);
break;
default:
break;
}
}
ServiceConnection conn = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
Log.v(TAG, "onServiceConnected");
}
public void onServiceDisconnected(ComponentName name) {
Log.v(TAG, "onServiceDisconnected");
}
};
@Override
protected void onDestroy() {
super.onDestroy();
System.out.println("-------onDestroy()--");
stopService(new Intent(MyService.ACTION));
unbindService(conn);
}
}
package com.example.servicetest.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
/** 标志位 */
private static String TAG = "com.example.servicetest.service.MyService";
/** 行为 */
public static final String ACTION = "com.example.servicetest.service.MyService";
@Override
public void onCreate() {
super.onCreate();
System.out.println("----- onCreate() ---");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
System.out.println("----- onStart() ---");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("----- onStartCommand() ---");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
System.out.println("----- onBind() ---");
return null;
}
@Override
public void onRebind(Intent intent) {
System.out.println("----- onRebind() ---");
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("----- onUnbind() ---");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
System.out.println("----- onDestroy() ---");
super.onDestroy();
}
}
<!-- 注册 -->
<service android:name="com.example.servicetest.service.MyService" >
<intent-filter>
<!-- 用来启动服务的Intent -->
<action android:name="com.example.servicetest.service.MyService" />
<category android:name="android.intent.category.default" />
</intent-filter>
</service>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有