android4.2中的后台服务Service及配置
来源:互联网 发布:远程视频软件 编辑:程序博客网 时间:2024/04/28 05:50
1.创建界面布局activity_main.xml
<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=".MainActivity" > <Button android:id="@+id/button_start" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="启动Service"/> <Button android:id="@+id/button_stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止Service" android:layout_below="@id/button_start"/> <Button android:id="@+id/button_bind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="绑定Service" android:layout_below="@id/button_stop"/> <Button android:id="@+id/button_unbind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="解除绑定" android:layout_below="@id/button_bind"/></RelativeLayout>
2.创建Service服务MyService
public class MyService extends Service {@Overridepublic IBinder onBind(Intent arg0) {Log.i("MyService","onBind....ing......");Toast.makeText(MyService.this, "onBind....ing......", Toast.LENGTH_LONG).show();return null;}@Overridepublic void onCreate() {super.onCreate();Log.i("MyService","onCreate....ing......");Toast.makeText(MyService.this, "onCreate....ing......", Toast.LENGTH_LONG).show();}@Overridepublic void onDestroy() {super.onDestroy();Log.i("MyService","onDestroy....ing......");Toast.makeText(MyService.this, "onDestroy....ing......", Toast.LENGTH_LONG).show();}@Overridepublic void onStart(Intent intent, int startId) {Log.i("MyService","onStart....ing......");Toast.makeText(MyService.this, "onStart....ing......", Toast.LENGTH_LONG).show();}}3.创建Activity
public class MainActivity extends Activity {protected ServiceConnection conn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button startButton=(Button)findViewById(R.id.button_start);Button stopButton=(Button)findViewById(R.id.button_stop);Button bindButton=(Button)findViewById(R.id.button_bind);Button unbindButton=(Button)findViewById(R.id.button_unbind);//连接对象conn=new ServiceConnection(){@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.i("MyService", "连接成功!");Toast.makeText(MainActivity.this, "连接成功!", Toast.LENGTH_LONG).show();}@Overridepublic void onServiceDisconnected(ComponentName name) {Log.i("MyService", "断开连接!");Toast.makeText(MainActivity.this, "断开连接!", Toast.LENGTH_LONG).show();}};startButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Intent intent=new Intent();//intent.setClass(MainActivity.this, MyService.class);intent.setAction("com.dh.service.MY_SERVICE");startService(intent);}});stopButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent();intent.setAction("com.dh.service.MY_SERVICE");stopService(intent);}});bindButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent();intent.setAction("com.dh.service.MY_SERVICE");bindService(intent, conn, Service.BIND_AUTO_CREATE);}});unbindButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent();intent.setAction("com.dh.service.MY_SERVICE");unbindService(conn);}});}}4.配置AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dh.service" android:versionCode="1" android:versionName="1.0" ><permission android:protectionLevel="normal" android:name="com.dh.service.MY_SERVICE"></permission> <uses-sdk android:minSdkVersion="7" 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.dh.service.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:permission="com.dh.service.MY_SERVICE" android:name="com.dh.service.MyService" android:enabled="true" > <intent-filter > <action android:name="com.dh.service.MY_SERVICE"/> </intent-filter> </service> </application></manifest>
- android4.2中的后台服务Service及配置
- 后台服务(Service)简述
- 后台服务(Service)
- Android后台服务-service
- Android 后台服务Service实现
- Android SERVICE后台服务进程
- Android Service 后台服务之本地服务
- AppWeb服务后台登陆及配置的方法解答
- ubuntu 配置服务service
- android中的服务---Service
- angularJs中的service服务
- Service使用 一直运行的后台服务
- Android SERVICE后台服务进程的守护
- Android SERVICE后台服务进程的守护
- 使用 Broadcast 与后台 Service 服务通信
- Android SERVICE后台服务进程的守护
- Android SERVICE后台服务进程的守护
- 【android基础】进程内后台服务 Service
- 关于应用程序启动时,提示空指针问题
- try-catch-finally中return的执行情况
- 云享 企业私有网盘 免费使用
- Ztree ajax关键字检索动态树
- poj 3264 RMQ
- android4.2中的后台服务Service及配置
- c#引用类型与值类型的区别大盘点
- SmartCard API for Android、Open Mobile API
- Windows下Apache+PHP+MySQL环境配置
- 远程监控基础知识和故障排除
- 如何避免图像解压缩的时间开销
- Ubuntu下修改DNS重启也能用的方法
- CentOS rpm 离线安装 gcc
- 手机公司笔试题