有问题请加:Q群: 241359063 共同走向创业学习之旅。
原创:kylin_zeng http://blog.chinaunix.net/uid/23795897.html在此感谢mars 老师的帮助。
转载请注明原创出处,尊重他人的劳动成果。
1、service,是一个应用程序组件,跟activity比,它是一个不可见的,没有图形化界面。它常用来处理一些比较耗时的操作,比如下载,播放音乐等。也可以使用service 更新ContenProvider,发送Intent以及启动系统的通知,它是一直运行的,知道你关闭它。
2、service 不是一个单独的进程,也不是一个线程。
3、在AndroidManifest.xml 设置:
4、做一个activity ,两个按钮,启动和停止服务。
5、服务:
mars视频教程ppt和代码01_25_src_ppt.zip
*****************************************************************
二、Bound service:
1、service 是一种组件,在后台处理一些比较耗时的操作,它没有用户界面,用户只能通过activity向service发送命令。
2、Bound service 和Start Service区别。Start service 无法向 activity返回数据。而Bound service则可以解决这个。
以下转载:http://blog.youkuaiyun.com/zoneyoung/article/details/13000789
3、在AndroidManifest.xml 注册
4、布局:
5、新建一个 MainActivity.java 。该类其实是新建了一个Button,给该Button添加了一个监听器。要实现Activity和Service中间的通信,必须使用bindService方法,而其中需要三个参数intent(新建Intent对象,其中Intent对象连接MainActivity.this和SecondService.class,把该intent传进去即可),第二个参数ServiceConnection对象(新建一个ServiceConnection内部类,需要重写两个方法,其中在onServiceConnected方法中创建在SecondService(要绑定的Service)中的Binder对象FirstBinder,调用其中的getData方法,得到返回的数据,最后打印出来,这样其实已经完成了Activity和Service的绑定大半工作),最后一个参数flags(这里直接用BIND_AUTO_CREATE即可),最后就完成了Activity和Service的绑定。效果是在对Button进行点击,就能从Service的数据传到Activity中,在控制台打印出在Service中传过来的数据。
6、 新建一个SecondService.java 的类,该类继承Service。onBind方法是继承Service方法必须复写的方法。新建一个内部类FirstBinder继承于Binder,当中的getData方法返回一个字符串。在onBind方法中新建一个IBinder对象,利用向下转型新建一个FirstBinder对象,然后返回这个IBinder对象。
阅读(4) | 评论(0) | 转发(0) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
原创:kylin_zeng http://blog.chinaunix.net/uid/23795897.html在此感谢mars 老师的帮助。
转载请注明原创出处,尊重他人的劳动成果。
1、service,是一个应用程序组件,跟activity比,它是一个不可见的,没有图形化界面。它常用来处理一些比较耗时的操作,比如下载,播放音乐等。也可以使用service 更新ContenProvider,发送Intent以及启动系统的通知,它是一直运行的,知道你关闭它。
2、service 不是一个单独的进程,也不是一个线程。
3、在AndroidManifest.xml 设置:
点击(此处)折叠或打开
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".TestActivity"
- 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>
4、做一个activity ,两个按钮,启动和停止服务。
点击(此处)折叠或打开
- package mars.service1;
-
- 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 TestActivity extends Activity {
- /** Called when the activity is first created. */
- private Button startServiceButton = null;
- private Button stopServiceButton = null;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.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");
- }
-
- class StartServiceListener implements OnClickListener {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setClass(TestActivity.this, FirstService.class);
- startService(intent); //开启服务。
- }
- }
-
- class StopServiceListener implements OnClickListener {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setClass(TestActivity.this, FirstService.class);
- stopService(intent); //停止服务。
- }
- }
- }
5、服务:
点击(此处)折叠或打开
- package mars.service1;
-
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
-
- public class FirstService extends Service {
-
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- System.out.println("Service onBind");
- return null;
- }
-
- //当创建一个Servcie对象之后,会首先调用这个函数,如果没有关闭服务,那么再次点击开启服务按钮时,不会再进入这个onCreate,因为之前的服务孩子
- @Override
- public void onCreate() {
- // TODO Auto-generated method stub
- super.onCreate();
- System.out.println("Service onCreate");
- }
- //onCreate后,就运行下面的工作。
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // TODO Auto-generated method stub
- System.out.println("flags--->" + flags); //flags =0
- System.out.println("startId--->" + startId); //第二次点击开启服务时,startId 就加1
- 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();
- }
- }

*****************************************************************
二、Bound service:
1、service 是一种组件,在后台处理一些比较耗时的操作,它没有用户界面,用户只能通过activity向service发送命令。
2、Bound service 和Start Service区别。Start service 无法向 activity返回数据。而Bound service则可以解决这个。
以下转载:http://blog.youkuaiyun.com/zoneyoung/article/details/13000789
3、在AndroidManifest.xml 注册
点击(此处)折叠或打开
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.marschen.s01e26_service01"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="16" />
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.marschen.s01e26_service01.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="com.marschen.s01e26_service01.SecondService"> //注册服务。
-
- </service>
- </application>
-
- </manifest>
4、布局:
点击(此处)折叠或打开
- <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"
- tools:context=".MainActivity" >
-
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:text="完成绑定操作" />
-
- </RelativeLayout>
5、新建一个 MainActivity.java 。该类其实是新建了一个Button,给该Button添加了一个监听器。要实现Activity和Service中间的通信,必须使用bindService方法,而其中需要三个参数intent(新建Intent对象,其中Intent对象连接MainActivity.this和SecondService.class,把该intent传进去即可),第二个参数ServiceConnection对象(新建一个ServiceConnection内部类,需要重写两个方法,其中在onServiceConnected方法中创建在SecondService(要绑定的Service)中的Binder对象FirstBinder,调用其中的getData方法,得到返回的数据,最后打印出来,这样其实已经完成了Activity和Service的绑定大半工作),最后一个参数flags(这里直接用BIND_AUTO_CREATE即可),最后就完成了Activity和Service的绑定。效果是在对Button进行点击,就能从Service的数据传到Activity中,在控制台打印出在Service中传过来的数据。
点击(此处)折叠或打开
- package com.marschen.s01e26_service01;
-
- import com.marschen.s01e26_service01.SecondService.FirstBinder;
-
- import android.os.Bundle;
- import android.os.IBinder;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class MainActivity extends Activity {
-
- Button button=null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- button=(Button)findViewById(R.id.button);
- button.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- // TODO Auto-generated method stub
- Intent intent=new Intent();
- intent.setClass(MainActivity.this, SecondService.class);
-
- bindService(intent, conn, BIND_AUTO_CREATE); //绑定服务。
- }
- });
- }
-
- ServiceConnection conn = new ServiceConnection() {
-
- @Override
- public void onServiceDisconnected(ComponentName name) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- // TODO Auto-generated method stub
- FirstBinder binder = (FirstBinder) service;
- String data = binder.getData();
- System.out.println("data----->"+ data);
- }
- };
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
-
- }
6、 新建一个SecondService.java 的类,该类继承Service。onBind方法是继承Service方法必须复写的方法。新建一个内部类FirstBinder继承于Binder,当中的getData方法返回一个字符串。在onBind方法中新建一个IBinder对象,利用向下转型新建一个FirstBinder对象,然后返回这个IBinder对象。
点击(此处)折叠或打开
- package com.marschen.s01e26_service01;
-
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
-
- public class SecondService extends Service{
-
-
- //其他应用程序组件(Activity)绑定至当前的Service对象时,会调用该方法
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- IBinder binder = new FirstBinder();
- return binder;
- }
-
- class FirstBinder extends Binder{
- public String getData() {
- return "test data";
- }
- }
-
- }
相关热门文章
给主人留下些什么吧!~~
评论热议