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.跨应用实现通信... ...
随机推荐
- 第19讲- UI组件之_Button、checkbox、radio
第19讲 UI组件之_Button.checkbox.radio 四.按钮Button Button继承自TextView,间接继承自View.当用户对按钮进行操作的时候,触发相应事件,如点击,触摸. ...
- 【转】打包AAC码流到FLV文件
AAC编码后数据打包到FLV很简单.1. FLV音频Tag格式 字节位置 意义0x08, ...
- mavne install 报错org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException
maven install 报错 org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.Invoc ...
- log4j 突然不打印记录,提示:No appenders could be found for logge,处理方法
log4j 一直都在使用正常,log4j.xml配置.代码都没有修改,突然不打印记录,出现下面提示: log4j:WARN No appenders could be found for logger ...
- 无Root抓包:Packet Capture 1.2.3
无Root抓包:Packet Capture 1.2.3 http://www.coolapk.com/apk/app.greyshirts.sslcapture
- 详细介绍如何使用kindEditor编辑器
今天群里的朋友问我能不能写个kindEditor编辑器的使用教程,说是弄了半天没有搞定.由于PHP啦后台正好用了这个编辑器,我有写经验,正好教他的同时写出来分享给大家. kindEditor编辑器是一 ...
- Qt 界面使用自己定义控件 "提升为"
1.效果图 我做了一个很easy的样例,一个能够显示颜色的QLabel,边上有个button,点击,跳出颜色选取的Dialog,然后选择一个颜色.这个QLabel会变成什么颜色. 2.ColorLab ...
- HDU 多校联合练习赛2 Warm up 2 二分图匹配
Warm up 2 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total ...
- IOS6和IOS7的屏幕适配问题
自从IOS7出来以后,以前写在IOS6上或者更低版本的程序,跑在IOS7的模拟器上就会出现一些问题.最大的问题就是,所有的UI空间都会统一向上移动20个点(如果空间的y值为0,就会被StatusBar ...
- Javascript进阶篇——(DOM—getAttribute()、setAttribute()方法)—笔记整理
getAttribute()方法通过元素节点的属性名称获取属性的值.语法: elementNode.getAttribute(name) 1. elementNode:使用getElementById ...