Android之Service
2014-04-07 16:24
239 查看
1.自定义Service类
2.启动和关闭Service
3.设置权限
package com.example.mars_2000_service; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class FirstService extends Service { @Override public IBinder onBind(Intent intent) { System.out.println("Service onBind"); return null; } // 当创建一个Servcie对象之后,会首先调用这个函数 @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); System.out.println("Service onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub System.out.println("flags--->" + flags); System.out.println("startId--->" + startId); System.out.println("Service onStartCommand"); return START_NOT_STICKY; } @Override public void onDestroy() { // TODO Auto-generated method stubo System.out.println("Service onDestory"); super.onDestroy(); } }
2.启动和关闭Service
public class MainActivity extends Activity { private Button startServiceButton = null; private Button stopServiceButton = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startServiceButton = (Button) findViewById(R.id.startService); startServiceButton.setOnClickListener(new StartServiceListener()); stopServiceButton = (Button) findViewById(R.id.stopService); stopServiceButton.setOnClickListener(new StopServiceListener()); System.out.println("Activity onCreate"); /*if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); }*/ } class StartServiceListener implements OnClickListener { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, FirstService.class); startService(intent); } } class StopServiceListener implements OnClickListener { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, FirstService.class); stopService(intent); } }
3.设置权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mars_2000_service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.mars_2000_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:name=".FirstService"></service> </application> </manifest>
相关文章推荐
- android中对Looper、HandlerThread、IntentService的理解及使用方法
- 初学Android:服务Service小结
- Android Service示例
- android Service(一)
- Android Service完全解析,关于服务你所需知道的一切(上)
- Android 一个例子带IntentService入门
- Android DropBoxManager Service
- Android Service(三) IntentService详解
- Android中保持Service的存活
- Android开发之如何保证Service不被杀掉(broadcast+system/app)
- 如何让Android Service不被清除或者常驻内存
- 介绍 Android DropBoxManager Service
- Android 5.0之后禁止用隐式Intent启动Service
- android service 本地 远程 总结
- Android 多线程之IntentService 完全详解
- Android中让Service被关闭后再重新启动
- 名词解释:android中四大天王Activity、Service、Broadcast Receiver、Content Provid
- android上app后台运行的相关知识(一)————android的四大组件:Activity、Service、BroadCast Recevicer、Content provider
- android中判断service是否正在运行
- Android 核心分析 之六 -----IPC框架分析 Binder,Service,Service manager