android 入门-Service
sdk 1.7
package com.example.hellowrold; import java.util.Random; import com.example.hellowrold.R.id; 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.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.widget.Button; public class EX0314 extends Activity { private Button startButton;
private Button stopbuButton;
private Button bindButton;
private Button unbindButton;
private ServiceConnection sConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex0314); sConnection=new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName name) {
} @Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
};
startButton=(Button)findViewById(id.Ex0314StartButton);
startButton.setOnClickListener(new Button.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sIntent=new Intent(EX0314.this,mService1.class);
sIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(sIntent);
}
}); stopbuButton=(Button)findViewById(id.Ex0314StopButton);
stopbuButton.setOnClickListener(new Button.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sIntent=new Intent(EX0314.this,mService1.class);
stopService(sIntent);
}
}); bindButton=(Button)findViewById(id.Ex0314BindButton);
bindButton.setOnClickListener(new Button.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sIntent=new Intent(EX0314.this,mService1.class);
sIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
bindService(sIntent,sConnection,BIND_AUTO_CREATE);
}
}); unbindButton=(Button)findViewById(id.Ex0314UnbindButton);
unbindButton.setOnClickListener(new Button.OnClickListener() { @Override
public void onClick(View v) {
Intent sIntent=new Intent(EX0314.this,mService1.class);
sIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
unbindService(sConnection);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.ex0314, menu);
return true;
} }
package com.example.hellowrold; import android.R.integer;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log; public class mService1 extends Service{
private Handler objHandler=new Handler();
private int intCounter=0;
private static final String TAG = "MyService";
private Runnable mTasks=new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
intCounter++;
Log.i("hippo","计算器"+Integer.toString(intCounter));
objHandler.postDelayed(mTasks, 1000);
}
}; @Override
public void onCreate() {
Log.e(TAG, "start onCreate~~~");
super.onCreate(); objHandler.postDelayed(mTasks, 1000);
}
@Override
public void onDestroy() {
Log.e(TAG, "start onDestroy~~~");
super.onDestroy(); objHandler.removeCallbacks(mTasks);
}
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "start IBinder~~~");
return null;
}
@Override
public boolean onUnbind(Intent intent) {
Log.e(TAG, "start onUnbind~~~");
return super.onUnbind(intent);
}
}
<activity
android:name="com.example.hellowrold.EX0314"
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=".mService1"
android:exported="false">
</service>




android 入门-Service的更多相关文章
- android 入门-Service实时向Activity通过BroadcastReceiver传递数据
引文: http://www.cnblogs.com/linjiqin/p/3147764.html <RelativeLayout xmlns:android="http://sch ...
- android入门——Service
简单记录一下四大组件之一的Service的简单实用. 先是最简单的用法,服务的开关,onBind方法的使用 package com.example.wkp.service; import androi ...
- android入门 — Service
Service完全在后台运行,没有用户界面.使用的时候先创建Service子类,然后在AndroidManifest.xml中进行注册,同时可以通过<intent-filter.../>进 ...
- Android入门:Service入门介绍
一.Service介绍 Service类似于Windows中的服务,没有界面,只是在后台运行:而服务不能自己运行,而是需要调用Context.startService(Intent intent);或 ...
- Android入门:绑定本地服务
一.绑定服务介绍 前面文章中讲过一般的通过startService开启的服务,当访问者关闭时,服务仍然存在: 但是如果存在这样一种情况:访问者需要与服务进行通信,则我们需要将访问者与服务进行绑定: ...
- Android入门教程之我见
真正的从安卓入门学习到实际工作也差不多一年时间了,也做了几个项目.在这期间经历了一开始学习Android的基本知识后仍旧无从下手,不知道如何开始开发一个app,到现在也开始学会注意Android架构的 ...
- 小猪的Android入门之路 day 1
小猪的Android入门之路 Day 1 Android相关背景与开发环境的搭建 ------转载请注明出处:coder-pig 本节引言: 随着社会经济的发展,移动互联网的越来越热,手机APP开发显 ...
- 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity
问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...
- Android精通教程-第一节Android入门简介
前言 大家好,给大家带来Android精通教程-第一节Android入门简介的概述,希望你们喜欢 每日一句 If life were predictable it would cease to be ...
随机推荐
- 史上最详细的CocoaPods安装教程
虽然网上关于CocoaPods安装教程多不胜数,但是我在安装的过程中还是出现了很多错误,所以大家可以照下来步骤装一下,我相信会很好用. 前言 在iOS项目中使用第三方类库可以说是非常常见的事,但是要正 ...
- espcms自定义表单邮件字段
/include/inc_replace_mailtemplates.php中增加一行就可以了. 如:$replacemail['mailform'][] = array(name => '职位 ...
- 【GoLang】golang TCP 粘包处理 示例
参考资料: http://www.01happy.com/golang-tcp-socket-adhere/
- 图形化的Git
原文:http://gitbook.liuhui998.com/6_5.html Git有不少图形化界面工具用于读取和维护仓库. 捆绑的GUI Git自带了两个使用Tcl/Tk写成的GUI程序. Gi ...
- An exception occurred during a WebClient request
System.Net.WebException was caught HResult=-2146233079 Message=An exception occurred during a WebCli ...
- 使用Java的多线程和IO流写一个文件复制功能类
创建一个复制功能类,继承Thread类,重写run()方法,把FileInputStream和FileOutputStream输入输出流写在run()方法内.示例代码如下: import java.i ...
- [Linux]非外网环境下配置lnmp心得
1.安装gcc编译器 基本所有编译安装包都需要gcc编译器,而且编译安装gcc也需要一个编译器,这是一个悖论.所以,这里只能寻求rpm包安装.但是rpm包需要匹配特定的操作系统内核,所以没有一个通用的 ...
- Application.AddMessageFilter(this);
开发环境:windows 8(x64), vs2013 只要“项目属性-调试”中选中“启用Visual Studio承载进程“,在VS2013中用F5调试,调用Application.AddMessa ...
- ABAP 销售范围
*&---------------------------------------------------------------------* *& Report ZSDR008 ...
- 深入浅出JMS(一)——JMS简介
如果手机只能进行实时通话,没有留言和短信功能会怎么样?一个电话打过来,正好没有来得及接上,那么这个电话要传递的信息肯定就收不到了.为什么不能先将信息存下来,当用户需要查看信息的时候再去获得信息呢?伴随 ...