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 2049 Let it Bead(polya模板)
Description Cannery Row percent of the target audience insists that the bracelets be unique. (Just ...
- hdu 5400 Arithmetic Sequence(模拟)
Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...
- 分享一个option样式传递给select当前选中样式
今天遇到一个很是纠结的问题,需求又改了!原生的select给option加样式,结果发现select选中仍是默认样式,如下图:
- Android 如何检测一个服务是否还在运行?
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- Android UI开发详解之ActionBar .
在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果. 一.添加A ...
- javascript---String与Arry
var str = "liuzhanqi"; document.write(str["length"]);//等价str.l ength var str = s ...
- VPS,虚拟主机,云主机,独立服务器区别
作者:张朝权链接:http://www.zhihu.com/question/25507629/answer/105594087来源:知乎著作权归作者所有,转载请联系作者获得授权. 独立服务器独立 ...
- 关于毕设WiFi选型
毕设做基于WiFi的家庭灯光影音控制,现在主要是WiFi模块上的选型,自己选了ESP8266. 一开始是看到了机智云的内容,想尝试机智云.但是目前没有母版,不知道怎么下手. 接下来就先尝试通过通用类型 ...
- w530 在ubuntu 12.04 _x64 背光调节方法
So to get the screen brightness keys working with your Nvidia graphics card, create a file in the xo ...
- Trident内核中取验证码图片的几种方法
程序中用了IE的内核,想取出网站中的验证码图片,单独显示出来,调研了以下几路方法 1.枚举所有缓存文件,进行处理,找到想要的,核心代码 )//这段代码可以枚举所有缓存资源,然后对应做处理 { LPIN ...