设为首页 加入收藏

TOP

一个Demo学完Android中所有的服务(一)
2014-11-24 08:24:44 来源: 作者: 【 】 浏览:0
Tags:一个 Demo 学完 Android 有的 服务


接下来,以源代码的方式分析这个例子


1.MainActivity--主界面


这个类主要是实现用户所看到的这个Activity,其中包含了一系列的按钮,用户点击按钮执行相应的动作,所以在这个类中主要是对按钮的定义和对按钮绑定相应的监听器,下面是实现的代码:


package lovefang.stadyService;


import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.content.Intent;
import android.util.Log;
/**这是使用后台服务的学习例子*/
public class MainStadyServics extends Activity {
/**参数设置*/
Button startServiceButton;// 启动服务按钮
Button shutDownServiceButton;// 关闭服务按钮
Button startBindServiceButton;// 启动绑定服务按钮
Button sendBroadcast;// 使用广播
Button notificationButton;// 使用通知功能
Button alarmButton;// 使用闹钟
Button handlerButton;// 使用handler
Button asyncButton;// 使用异步加载
Button phoneStateButton;// 查看手机状态
Button callphoneButton;// 拨打电话
Button vibratorButton;// 使用震动
CountService countService;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("MainStadyServics", "setContentView");
setContentView(R.layout.main);
getWidget();
regiestListener();
}
/**获得组件*/
public void getWidget(){
startServiceButton = (Button)findViewById(R.id.startServerButton);
startBindServiceButton = (Button)findViewById(R.id.startBindServerButton);
shutDownServiceButton = (Button)findViewById(R.id.sutdownServerButton);
sendBroadcast = (Button)findViewById(R.id.sendBroadcast);
notificationButton = (Button)findViewById(R.id.notification);
alarmButton = (Button)findViewById(R.id.alarm);
handlerButton = (Button)findViewById(R.id.handler);
asyncButton = (Button)findViewById(R.id.async);
phoneStateButton = (Button) findViewById(R.id.phonestate);
callphoneButton = (Button) findViewById(R.id.callphone);
vibratorButton = (Button) findViewById(R.id.vibrator);
}
/**为按钮添加监听*/
public void regiestListener(){
startServiceButton.setOnClickListener(startService);
shutDownServiceButton.setOnClickListener(shutdownService);
startBindServiceButton.setOnClickListener(startBinderService);
sendBroadcast.setOnClickListener(broadcastReceiver);
notificationButton.setOnClickListener(notification);
alarmButton.setOnClickListener(startAlarm);
handlerButton.setOnClickListener(handler);
asyncButton.setOnClickListener(async);
phoneStateButton.setOnClickListener(phonestate);
callphoneButton.setOnClickListener(callphoneEvent);
vibratorButton.setOnClickListener(vibrator);
}
/**启动服务的事件监听*/
public Button.OnClickListener startService = new Button.OnClickListener(){
public void onClick(View view){
/**单击按钮时启动服务*/
Intent intent = new Intent(MainStadyServics.this,CountService.class);
startService(intent);
Log.v("MainStadyServics", "start Service");
}
};
/**关闭服务*/
public Button.OnClickListener shutdownService = new Button.OnClickListener(){
public void onClick(View view){
/**单击按钮时启动服务*/
Intent intent = new Intent(MainStadyServics.this,CountService.class);
/**退出Activity是,停止服务*/
stopService(intent);
Log.v("MainStadyServics", "shutDown serveice");
}
};
/**打开绑定服务的Activity*/
public Button.OnClickListener startBinderService = new Button.OnClickListener(){
public void onClick(View vi

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android中SQLite构造函数参数Cont.. 下一篇100多个Android Demo的整合下载

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)