- 浏览: 394155 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (397)
- 随笔 (2)
- java (44)
- android (129)
- html + css (18)
- php (35)
- linux (2)
- html5技术/游戏 (3)
- 其他 (30)
- MAC (2)
- IOS (11)
- mongoDB (0)
- js + jquery (29)
- SQL (5)
- ecshop (2)
- jsp (1)
- mybatis (1)
- 推荐 (0)
- 网络推广 (1)
- jquery mobiles (7)
- Sencha Tcouch (11)
- case (2)
- joomla (1)
- cocos2d (1)
- AndEngine (1)
- 错误异常报错 (1)
- 第三方&二次开发 (5)
- cordova (5)
- swift (81)
- mysql (1)
- zepto (2)
- 常用代码 (1)
最新评论
-
wu1238:
2c2ab98030fc767cf89051f369a97c5 ...
Swift -> UILable 设置内边距 UIEdgeInsets , UITextField 设置内边距
**什么是 Service,
Service 是 android 四大组件之一, 即android 系统的服务(不是一个线程而是 主程序的一部分) ,于 Activity 不同,它是不能与用户交互的,不能自己启动的, 需要调用 Context.startServece() 来启动 , (Context 类似 ThiActive.this) ,运行后台,如果我们推出应用 service 进程并没有结束, 它任然 在 后台 运行, 比如 我们播放音乐的时候, 可能想边听歌边干点其他的 就 需要这个东西 ,
** 启动和停止 Service
启动: startService(Intent intent) 这个时候 Service 会调用 自身的 onCreate 方法
停止: staopService(Intent intent) 来停止 servece, 这个时候会调用 onDestory() 方法
其中 intent = (ThisActivity.this, ServiceActive.class);
下面是一个例子, 先 是 Servers的 class ,这个的功能新建一个线程 然后 是每隔1秒钟执行一次 加 1 的操作
package udpreceive.com; import android.app.Activity; import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.os.IBinder; import android.util.Log; public class UdpReceive extends Service{ private boolean threadDisable; private int count; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); System.out.println( "dd"); new Thread(new Runnable() { public void run() { // TODO Auto-generated method stub while (!threadDisable) { try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } count++; System.out.println( "Count is " + count); } } }).start(); } @Override public void onDestroy() { super.onDestroy(); this.threadDisable = true; Log.v("CountService", "on destroy"); } public int getCount() { return count; } }
然后是 主 activity 启动后 调用 servers
package remote.com; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.nio.Buffer; import java.util.ArrayList; import java.util.List; import udpreceive.com.UdpReceive; import android.app.Activity; import android.content.Intent; import android.hardware.Camera.Size; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class RemoteActivity extends Activity { /** Called when the activity is first created. */ private Button start_button; private Button end_bu; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); start_button = (Button)findViewById(R.id.start_bu); end_bu = (Button)findViewById(R.id.end_bu); start_button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(RemoteActivity.this, UdpReceive.class); startService(intent); System.out.println("a"); } }); end_bu.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub RemoteActivity.this.stopService(new Intent(RemoteActivity.this,UdpReceive.class)); System.out.println("b"); } }); // } ////// }
最后是 manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="remote.com" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".RemoteActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="udpreceive.com.UdpReceive" android:label="@string/app_name" > </activity> <service android:name="udpreceive.com.UdpReceive"></service> </application> </manifest>
其中 <service android:name="udpreceive.com.UdpReceive"></service> 这样定义因为 我的UdpReceive 在udpreceive.com 包里面 所以定义全包名,如果 和 主 activity 在一个包里 就写
<service android:name=".UdpReceive"></service> 就ok
发表评论
-
android -> ImageButton 内部图片 padding
2020-04-09 12:00 369先给 ImageButton 设置属性 andr ... -
android -> 设置 全局 字体样式
2017-05-05 11:11 726首页在AndroidManifest.xml中 ap ... -
android -> 背景background - shape的使用
2017-05-04 12:09 859shape用于设定形状,可以在selector, ... -
android -> 只保留一边 的 边框 border
2017-05-03 09:05 1032设置background <layer-li ... -
android -> 在布局中动态添加view的两种方法
2017-04-29 19:48 965转自 : http://b ... -
android -> 修改 version 版本
2017-03-28 15:33 403除了在 manifest.xml 里面 <?xml ... -
android -> viewPaer 页面滑动
2017-03-27 17:23 510MainActivity.java public ... -
android -> 获取系统输入法列表/已勾选/默认
2017-03-26 13:02 1173参考: http://www.aichengxu. ... -
android -> android 一个 textview 设置不同的字体大小和颜色
2017-03-24 00:05 861参考: http://blog.csdn.net/fengy ... -
android -> 查看 签名 的 信息 包括 有效期
2017-03-23 11:27 1017C:\Windows\system32>D ... -
android -> 父控件 遍历 子 控件
2017-03-21 21:03 692布局文件: <LinearLayout an ... -
android -> 动画类( Animation)
2017-03-19 11:01 504参考: https://my.oschina.net/u/ ... -
android -> TextView setTextSize(int unit, float size) -- px,sp,dip
2017-03-18 23:35 1027TextView setTextSize(int unit, ... -
android -> canvas学习3 (跟随手势画直线)
2017-03-10 17:17 925DiyView.java package com ... -
android -> canvas学习2(动态画图)
2017-03-10 16:31 589关键字 invalidate() 重新执行onDra ... -
android -> canvas学习1
2017-03-10 14:39 462参考: http://blog.csdn.net/lee5 ... -
android -> 九宫格 划直线解锁
2017-03-09 22:53 414来自 : http://blog.csdn.net/ ... -
android -> 按钮 圆角 & 边框 & 渐变
2017-02-14 16:32 877参考: http://blog.csdn.net/l ... -
android -> 资源文件 反编译
2017-02-14 15:30 348官方网站 : https://ibotpeaches ... -
android -> 按钮双击
2017-02-12 11:48 447long space_last_click_time; ...
相关推荐
android,启动service,并且service把值传给Activity显示
Android防止service多次执行startCommand 博客地址:http://blog.csdn.net/yuzhiqiang_1993/article/details/78211385
android aidl service
android service 简单实例代码 . binder继承一个接口,实现方法。
Android Studio Service安卓在服务中如何弹出对话框Dialog
Android之Service实现比较大小
Android Upload Service Easily upload files in the background with automatic Android Notification Center progress indication. Download the latest demo app APK which uses the library and try it ...
关于Android Service的简单实例:属于start service类型,而不是bound service。
Android Native Service
Android 用Service+OKHttp实现应用后台检测更新示例代码; Android 用Service+OKHttp实现应用后台检测更新示例代码 Android 用Service+OKHttp实现应用后台检测更新示例代码 Android 用Service+OKHttp实现应用后台...
android intent service 实例android intent service 实例android intent service 实例
Android Service下载文件
android学习之Service启动1
android startService小demo,适合初学者。
Android 采用service与定时器每隔3秒打印一次log,希望大家能自己扩展,只是简单用法,具体扩展还得靠自己哈。
一个Android停止系统Service的例程
Android Service Demo
Android 多个service的action 相同冲突 验证demo,对应博客地址是http://blog.csdn.net/czlove110/article/details/50791390
Android在Service里的播放器,http://blog.csdn.net/feifei454498130/article/details/9306631
Android中Service组件的一些高级应用,具体查看博客:http://www.cnblogs.com/plokmju/p/Android_Service2.html