Android使用bindService启动服务
1.Service
package com.example.ebobo; import java.util.Timer; import java.util.TimerTask; import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.os.Binder; public class timer_push extends Service { private NotificationManager manager;
private Notification myNotify;
private Context context;
private PendingIntent contentIntent;
private CharSequence contentTitle;
private CharSequence contentText; private Timer mTimer = null;
private TimerTask mTimerTask = null; private final IBinder binder = new MyBinder(); @Override
public IBinder onBind(Intent intent) {
return binder;
} public class MyBinder extends Binder {
timer_push getService() {
return timer_push.this;
}
} @Override
public boolean onUnbind(Intent intent) {
// 当调用者退出(即使没有调用unbindService)或者主动停止服务时会调用
System.out.println("调用者退出了");
return super.onUnbind(intent);
} @Override
public void onCreate()
{
super.onCreate();
Log.i("push_service", "push_service onCreate"); manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.button_login; //通知图标
CharSequence tickerText = "Hello"; //状态栏显示的通知文本提示
long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示
myNotify = new Notification(icon,tickerText,when);
myNotify.defaults = Notification.DEFAULT_VIBRATE; context = getApplicationContext(); //上下文
contentTitle = "定时按摩"; //通知栏标题
Intent notificationIntent = new Intent(getApplicationContext(), ScanActivity.class); //点击该通知后要跳转的Activity
contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
} @SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent,int startId)
{
super.onStart(intent, startId);
Log.i("push_service", "push_service start");
} @Override
public void onDestroy()
{
stopMyTimer();
manager.cancel(2);
super.onDestroy();
Log.i("push_service", "push_service destroy");
} @SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 123:
contentText = "请注意大保健!"; //通知栏内容
myNotify.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
manager.notify(2, myNotify);
//如果想要更新一个通知,只需要在设置好notification之后,再次调用 setLatestEventInfo(),然后重新发送一次通知即可,即再次调用notify()。
break; default:
break;
}
}
}; public void startMyTimer(){
if (mTimer == null) {
mTimer = new Timer();
} if (mTimerTask == null) {
mTimerTask = new TimerTask() {
@Override
public void run() {
do {
try {
Log.i("push_service", "222222222222222222222222222222222222222y");
Message message = new Message();
message.what = 123;
handler.sendMessage(message);
}
catch (IllegalStateException e) {
}
} while (false);
}
};
} if(mTimer != null && mTimerTask != null )
mTimer.schedule(mTimerTask, 0, 10000);
} public void stopMyTimer(){ if (mTimer != null) {
mTimer.cancel();
mTimer = null;
} if (mTimerTask != null) {
mTimerTask.cancel();
mTimerTask = null;
}
}
}
2 activity1
public class LoginActivity extends Activity implements OnClickListener, OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Intent intent = new Intent(this, timer_push.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
init();
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
myService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService = ((timer_push.MyBinder) service).getService();
System.out.println("Service连接成功");
// 执行Service内部自己的方法
myService.startMyTimer();
}
};
}
3.actvity2
public class WaterActivity extends Activity implements OnClickListener, OnSeekBarChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mian_water);
init();
model = 0;
Intent sintent = new Intent(this, timer_push.class);
bindService(sintent, connection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
myService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService = ((timer_push.MyBinder) service).getService();
System.out.println("Service连接成功");
}
};
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_wf_start:
index = 3;
if(btnStart.getText().toString().equals(getResources().getString(R.string.start))){
btnStart.setText(getResources().getString(R.string.stop));
startMyTimer();
getflag = 1;
final ProgressDialog proDia=ProgressDialog.show(WaterActivity.this,"",
"正在启动设备,请稍后...",true,false);// 执行Service内部自己的方法
myService.stopMyTimer();
} else {// 执行Service内部自己的方法
myService.startMyTimer();
}
}
}
}
Android使用bindService启动服务的更多相关文章
- android startservice无法启动服务
1.android startservice无法启动服务 之前MainActivity.java中启动service源代码如下: private void startMyService() { //启 ...
- Android应用程序绑定服务(bindService)的过程源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6745181 Android应用程序组件Serv ...
- Android应用程序绑定服务(bindService)的过程源码分析
Android应用程序组件Service与Activity一样,既能够在新的进程中启动,也能够在应用程序进程内部启动:前面我们已经分析了在新的进程中启动Service的过程,本文将要介绍在应用程序内部 ...
- Android四大组件之服务的两种启动方式详解
Service简单概述 Service(服务):是一个没有用户界面.可以在后台长期运行且可以执行操作的应用组件.服务可由其他应用组件启动(如:Activity.另一个service).此外,组件可以绑 ...
- Android使用bindService作为中间人对象开启服务
Android使用bindService作为中间人对象开启服务 项目结构如下: MyService: package com.demo.secondservice; import android.ap ...
- 【android学习1】:安装MySQL启动服务失败解决方法
最近需要用到MySQL,从官网上下载了一个安装文件,但是安装时一直弹出如下提示信息: Configuration of MySQL Server 5.7 is taking longer than e ...
- Android开发学习—— Service 服务
Service运行于后台的一个组件,用来运行适合运行在后台的代码,服务是没有前台界面,可以视为没有界面的activity. 服务可以被手动关闭,不会重启,但是如果被自动关闭,内存充足就会重启. sta ...
- 一个Demo学完Android中所有的服务(转)
说明:这个例子实现了Android中常见的许多服务,下面是实现的截图 接下来,以源代码的方式分析这个例子 1.MainActivity--主界面 这个类主要是实现用户所看到的这个Activity, ...
- Android 学习笔记 Service服务与远程通信...(AIDL)
PS:这一章节看的我有几分迷茫,不是很容易理解...不过还好总算是明白了一大半了...基本的迷惑是解决了... 学习内容: 1.跨应用启动服务... 2.跨应用绑定服务... 3.跨应用实现通信... ...
随机推荐
- 原生javascript 改写的tab选项卡
<!--css部分--> <style> *{ margin: 0; padding: 0; } ul,li{ list-style: none } .tabbox{ widt ...
- Oracle—RMAN备份(三)
一.增量备份的相关概念 1. 在前面说明了RMAN的完整备份,完整备份是备份所用使用过的块,不备份没有使用的过的块:增量备份只备份自上次备份以来更改过的块. 2.即使RMAN的默认操作是在增量备份时扫 ...
- 【转载】cocos2d-x2.2.3和android的平台环境
这两天试图按照教程来学习写游戏移植到的横版过关Android在.在网上找了很多教程,但版本号变化.所使用的工具有细微的差别.所以,现在我们还没有准备好,阅读后,下面的文章.最后能够顺利您的手机上跑起来 ...
- openstack中glance组件images的全部python API 汇总
感谢朋友支持本博客,欢迎共同探讨交流.因为能力和时间有限.错误之处在所难免,欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- CodeSmith使用总结--读取一个表试试
我感觉CodeSmith对于我的最大用途是不用我手动创建每个表的Model.BLL和DAL了,那些繁琐的工作真的让我很无语. CodeSmith要读取数据库中的表就要先连接数据库. 新建一个数据库连接 ...
- asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料
asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料 NET Pet Shop .NET Pet Shop是一个电子商务的实例, ...
- (转)Div+CSS布局入门
在网页制作中,有许多的术语,例如:CSS.HTML.DHTML.XHTML等等.在下面的文章中我们将会用到一些有关于HTML的基本知识,而在你学习这篇入门教程之前,请确定你已经具有了一定的HTML基础 ...
- .NET踩坑记录【不断更新】
NET 4.0 Tasks 使用 ThreadPool 可设置最大并发级别. 多个WebClient多线程下载受System.Net.ServicePointManager.DefaultConnec ...
- ifstream中文路径问题分析
最近维护一个项目,遇到了ifstream在中文路径下打开文件失败的bug,我搜索了一下,最后整理成下文以后日后查阅. 一.问题重现 vs2008下创建一个简单win32工程. 使用ANSI编码方式:项 ...
- c# 中的线程和同步
一.新建线程的3种方法 a)异步委托:b)Thread类:c)线程池: 二.异步委托 1.简单使用,检查委托是否完成其任务 a) 通过 BeginInvoke() 的返回值IAsyncResult ...