android中启动服务
16lz
2022-01-27
在Activity中
package cn.edu.servicetest;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;public class ServiceTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn=(Button)this.findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent=new Intent();intent.setClass(ServiceTestActivity.this, MyService.class);ServiceTestActivity.this.startService(intent);} }); }}
二、继承Service
package cn.edu.servicetest;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService extends Service{@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubSystem.out.println("service Begin");return super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}}
在AndroidManifest.xml中增加如下配置信息
<service android:name="cn.edu.servicetest.MyService"></service>
更多相关文章
- Android(安卓)Seek自定义样式
- Android增加自定义监听事件
- Android(安卓)Intent列表
- Android的版本(Version)和API-level的对应关系
- Android入门:Button
- Android(安卓)AsyncTask 异步任务取消
- android 使用randerScript实现图片模糊效果
- Android简单的Fragment嵌套Fragment(Viewpager+Fragement)
- android 设置屏幕亮度