通过startService()方法使用Service
2014-05-11 11:27
288 查看
实现步骤:
1、编写类继承 Service 或其子类
2、重写onStartCommand() onBind() onCreate() onDestroy() 方法
3、在mainfest 文件中声明服务 <service android:name = ".Service" /> (Service 替换为你的服务类名)
4、在程序中启动服务,关闭服务
mService.java
package com.lanbokaka; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class mService extends Service { private static final String TAG = "mService"; @Override public void onCreate() { Log.i(TAG, "mService-->onCreate"); super.onCreate(); } @Override public void onDestroy() { Log.i(TAG, "mService-->onDestroy"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "mService-->onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent arg0) { Log.i(TAG, "mService-->onBind"); return null; } }
MainActivity.java
package com.lanbokaka; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTextView; private Button mButton1, mButton2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.textView1); mButton1 = (Button) findViewById(R.id.button1); mButton2 = (Button) findViewById(R.id.button2); /* 开始服务 */ mButton1.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, mService.class); startService(intent); mTextView.setText("服务启动"); } }); /* 结束服务 */ mButton2.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, mService.class); stopService(intent); mTextView.setText("服务结束"); } }); } }
AndroidManifest.xml
<activity android:name="com.lanbokaka.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=".mService" > </service>
相关文章推荐
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- service使用以及startService,stopService,bindService,unbindService方法比较
- 使用service的startservice 和bindservice方法实现音乐的播放、暂停、继续
- Android中startService基本使用方法概述
- Android Context的startService方法如何使用?
- service使用以及startService,stopService,bindService,unbindService方法比较
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- mysql 使用service mysqld start 提示未识别服务 进入/etc/rc.d/init.d 下面未发现有mysqld解决方法
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- service使用以及startService,stopService,bindService,unbindService方法比较
- 混合使用startService,bindService,以及tartForegroud方法使用总结
- [源码]使用startService和boundService两种方法实现播放歌曲DEMO
- service使用以及startService,stopService,bindService,unbindService方法比较
- 混合使用startService,bindService,以及tartForegroud方法使用总结
- 混合使用startService,bindService,以及tartForegroud方法使用总结
- Android中startService基本使用方法概述
- 停止通过startService()方式启动的service的两种方法
- Service的onStartCommand()方法返回值详解
- MySQL 重装不能start service解决方法