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.跨应用实现通信... ...
随机推荐
- jquery.tochart.js
var _jq, _hc; var jqsrc = "http://code.jquery.com/jquery-1.7.min.js"; var hcsrc = "ht ...
- UIScrollView使用autolayout 垂直滚动
转自:http://dadage456.blog.163.com/blog/static/30310744201491141752716 1.创建一个空白的UIViewController .将UIS ...
- 算法导论——lec 11 动态规划及应用
和分治法一样,动态规划也是通过组合子问题的解而解决整个问题的.分治法是指将问题划分为一个一个独立的子问题,递归地求解各个子问题然后合并子问题的解而得到原问题的解.与此不同,动态规划适用于子问题不是相互 ...
- Sftp和ftp 差别、工作原理等(汇总ing)
Sftp和ftp over ssh2的差别 近期使用SecureFx,涉及了两个不同的安全文件传输协议: -sftp -ftp over SSH2 这两种协议是不同的.sftp是ssh内含的协议,仅仅 ...
- DEV GridControl 根据单元格值改变背景色
GridControl 根据单元格值改变背景色(需要用到CustomDrawCell事件) 方法1: private void gdvClient_CustomDrawCell(object send ...
- SQL中@@ROWCOUNT函数
返回受上一语句影响的行数.如果行数大于 20 亿,请使用 ROWCOUNT_BIG. 语法 @@ROWCOUNT 返回类型 int 注释 Transact-SQL 语句可以通过下列方 ...
- 未能的导入项目,请确认<Import>声明中的路径正确
对于这样的错误,根据提示应该是项目的管理文件(.csproj)中有问题.找到对应的位置修改即可
- unison实时双向数据同步
软件下载 ocamlopt下载地址:http://caml.inria.fr Unison下载地址:http://www.seas.upenn.edu/~bcpierce/unison 1.安装uni ...
- 删除作业计划出错(DELETE语句与 REFERENCE约束"FK_subplan_job_id"冲突。)
删除作业计划出错(DELETE语句与 REFERENCE约束"FK_subplan_job_id"冲突.) use msdb select * from sysmaintplan_plans --查看 ...
- Apache URL转发
httpd.conf 尾巴加 Alias /web "F:\xampp\htdocs\test/" <Directory "F:\xampp\htdocs\test ...