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.跨应用实现通信... ...
随机推荐
- poj 1979 Red and Black(dfs水题)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- 并发情况下synchronized死锁
存在缺陷的代码: public class DataPropertyIdAndNameRepositoryImpl{ /** 发布标志 */ private volatile boolean publ ...
- Mybatis分页插件PageHelper正确的用法(网上有2篇不够科学的文章)
今天下午在Mybatis项目中.实现分页.由于我是后加入项目中的,Leader用的是PageHelper这个组件.可是我在实际使用的过程中遇到了2个大问题. 1.p=2#comments" ...
- CodeManage 源代码管理器v2.0发布
下载地址 欢迎大家提出宝贵的意见和bug
- Visual Studio 命中断点时 打印信息
打印时间: 开始: {DateTime.Now.ToString()} 结束: {DateTime.Now.ToString()} 搜索 复制
- 黑马程序员—— Java SE(3)
----<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训 ...
- windows消息常量值
WM_NULL = 0WM_CREATE = 1应用程序创建一个窗口WM_DESTROY = 2一个窗口被销毁WM_MOVE = 3移动一个窗口WM_SIZE = 5改变一个窗口的大小WM_ACTIV ...
- oc随笔六:字典
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...
- struts2 result的type属性
目前只使用过以下3种,都是直接跳转到另一个action chain: 写法:<result name="success" type="chain"> ...
- JSP 核心 (等待更新)
开篇:JSP的等同于servlet 编译器将其转化为.class,后执行.一旦配置在Tomcat webapps,运行访问后,在Tomcat--->work文件内出现java文件,其内容即为转化 ...