`
- 浏览:
550648 次
-
今天在网站看了一系列例子。太棒了。。。我收藏了哦。实现了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 view){
-
/**单击按钮时启动服务*/
-
Intent intent = new Intent(MainStadyServics.this,UseBrider.class);
-
startActivity(intent);
-
Log.v("MainStadyServics", "start Binder Service");
-
}
-
};
-
/**打开广播学习的按钮*/
-
public Button.OnClickListener broadcastReceiver = new Button.OnClickListener(){
-
public void onClick(View view){
-
Intent intent = new Intent(MainStadyServics.this,UseBroadcast.class);
-
startActivity(intent);
-
Log.v("MainStadyServics","start broadcast");
-
}
-
};
-
/**打开通知*/
-
public Button.OnClickListener notification = new Button.OnClickListener(){
-
public void onClick(View view){
-
Intent intent = new Intent(MainStadyServics.this, UseNotification.class);
-
startActivity(intent);
-
Log.v("MainStadyService ","start Notification");
-
-
}
-
};
-
/**使用闹钟*/
-
public Button.OnClickListener startAlarm = new Button.OnClickListener(){
-
public void onClick(View view){
-
Intent intent = new Intent(MainStadyServics.this, UseAlarmManager.class);
-
startActivity(intent);
-
Log.v("MainStadyService ","start alarm");
-
-
}
-
};
-
public Button.OnClickListener handler= new Button.OnClickListener(){
-
public void onClick(View view){
-
Intent intent = new Intent(MainStadyServics.this, UseHandleMessage.class);
-
startActivity(intent);
-
Log.v("MainStadyService ","start handle");
-
}
-
};
-
public Button.OnClickListener async= new Button.OnClickListener(){
-
public void onClick(View view){
-
Intent intent = new Intent(MainStadyServics.this, UseAsyncTask.class);
-
startActivity(intent);
-
Log.v("MainStadyService ","start handle");
-
}
-
};
-
public Button.OnClickListener phonestate= new Button.OnClickListener(){
-
public void onClick(View view){
-
Intent intent = new Intent(MainStadyServics.this, UsePhoneState.class);
-
startActivity(intent);
-
Log.v("MainStadyService ","start phonestate");
-
}
-
};
-
public Button.OnClickListener callphoneEvent= new Button.OnClickListener(){
-
public void onClick(View view){
-
Intent intent = new Intent(MainStadyServics.this, UseActionCall.class);
-
startActivity(intent);
-
Log.v("MainStadyService ","start callphone");
-
}
-
};
-
public Button.OnClickListener vibrator= new Button.OnClickListener(){
-
public void onClick(View view){
-
Intent intent = new Intent(MainStadyServics.this, UseVibrator.class);
-
startActivity(intent);
-
Log.v("MainStadyService ","start callphone");
-
}
-
};
-
/***/
-
protected void onDestroy(){
-
super.onDestroy();
-
Intent intent = new Intent(MainStadyServics.this,CountService.class);
-
/**退出Activity是,停止服务*/
-
stopService(intent);
-
}
-
-
-
}
复制代码
2.启动服务按钮这个类实现的是第一个按钮的功能,在这个类中新开了一个线程,并每隔一秒打印出一行日志代码如下:
-
package lovefang.stadyService;
-
/**引入包*/
-
import android.app.Service;// 服务的类
-
import android.os.IBinder;
-
import android.os.Binder;
-
import android.content.Intent;
-
import android.util.Log;
-
/**计数的服务*/
-
public class CountService extends Service{
-
/**创建参数*/
-
boolean threadDisable ;
-
int count;
-
-
public IBinder onBind(Intent intent){
-
return null;
-
}
-
public void onCreate(){
-
super.onCreate();
-
/**创建一个线程,每秒计数器加一,并在控制台进行Log输出*/
-
new Thread(new Runnable(){
-
public void run(){
-
while(!threadDisable){
-
try{
-
Thread.sleep(1000);
-
}catch(InterruptedException e){
-
-
}
-
count++;
-
Log.v("CountService","Count is"+count);
-
}
-
}
-
}).start();
-
}
-
public void onDestroy(){
-
super.onDestroy();
-
/**服务停止时,终止计数进程*/
-
this.threadDisable = true;
-
}
-
public int getConunt(){
-
return count;
-
}
-
class ServiceBinder extends Binder{
-
public CountService getService(){
-
return CountService.this;
-
}
-
}
-
}
复制代码
3.绑定服务服务有两种实现的方法:1.startService,启动服务,这时需要程序员管理服务的生命周期2.bindService,绑定服务,此时Service与Activity绑定在一起下面是实现的代码:
-
package lovefang.stadyService;
-
/**引入包*/
-
import android.app.Activity;
-
import android.content.ComponentName;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.content.ServiceConnection;
-
import android.os.Bundle;
-
import android.os.IBinder;
-
import android.util.Log;
-
-
/**通过bindService和unBindSerivce的方式启动和结束服务*/
-
public class UseBrider extends Activity {
-
/**参数设置*/
-
CountService countService;
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(new UseBriderFace(this));
-
Intent intent = new Intent(UseBrider.this,CountService.class);
-
/**进入Activity开始服务*/
-
bindService(intent, conn, Context.BIND_AUTO_CREATE);
-
-
}
-
private ServiceConnection conn = new ServiceConnection(){
-
/**获取服务对象时的操作*/
-
public void onServiceConnected(ComponentName name, IBinder service) {
-
// TODO Auto-generated method stub
-
countService = ((CountService.ServiceBinder)service).getService();
-
-
}
-
/**无法获取到服务对象时的操作*/
-
public void onServiceDisconnected(ComponentName name) {
-
// TODO Auto-generated method stub
-
countService =null;
-
}
-
-
-
};
-
protected void onDestroy(){
-
super.onDestroy();
-
this.unbindService(conn);
-
Log.v("MainStadyServics", "out");
-
}
-
}
复制代码
4.发送广播使用sendBroadcast,向一个Action发送广播,并由相应的广播接收器接收并执行相应的动作实现的代码如下:4.1 打开广播服务
-
package lovefang.stadyService;
-
/**引入包*/
-
import android.view.View;
-
import android.os.Bundle;
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.widget.Button;
-
/**使用Broadcast,这是一个发送广播的类*/
-
public class UseBroadcast extends Activity{
-
/**创建参数*/
-
private Button sendBroadcast;
-
/**创建Activity*/
-
public void onCreate(Bundle savedInstanceState){
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.broadcast);// 使用布局文件
-
getView();
-
sendBroadcast.setOnClickListener(sendBroadcastClick);// 添加事件监听
-
}
-
public void getView(){
-
sendBroadcast = (Button)findViewById(R.id.sendBroadcast);
-
}
-
/**创建事件监听*/
-
public Button.OnClickListener sendBroadcastClick = new Button.OnClickListener(){
-
public void onClick(View view){
-
Intent intent = new Intent();// 创建意图
-
intent.putExtra("CONTENT","This is a Braodcast demo");// 设置广播的内容
-
intent.setAction("lovefang.stadyService");// 设置广播的Action
-
sendBroadcast(intent);
-
}
-
};
-
-
}
复制代码
未完,待续。。。。。。
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Android 控件使用Demo集,主要包含常用控件的运用,适合入门学习参考。
android常用图片特效处理.zip android超炫的图片浏览器.zip Android——仿美图秀秀和IOS系统的相机胶卷.zip Android创建UI的新思路:用javascript与Activity进行交互.zip Android模仿易网新闻页面源码(异步加载).zip...
源码实现了三种比较常用的悬浮框效果 1.悬浮球转盘效果 2.悬浮加速小火箭效果 3.悬浮播放视频图片效果 具体实现思路和效果图可以参考博文 https://blog.csdn.net/daokedream/article/details/132740710
一些经常用到的比如:Async Task异步任务,蓝牙,Handler,Framework源码
前几年的Android应用源码Demo,主要面向的是学生毕业设计学习。
Android引常用的导页小demo。包含源码。
封装了自己平时快速开发的常用工具类和轻量级框架 有仿iOS的对话框,底部弹出框, svprogress提示框 还有一些常用的util工具类 具体看源码就可以 使用前下载lirbary导入 项目引用这个
一个Demo让你掌握所有的android常用控件!
android中常用动画demo搜集集合(包含源码),没事可以自己模仿着写一些小demo玩玩
Android应用源码开发Demo,主要用于毕业设计学习。
我们的安卓项目源码还采用了一些常用的技术和工具,以提高开发效率和代码质量。例如,我们使用了Android Studio作为开发工具,它提供了一系列的功能和工具,如代码编辑器、调试器和模拟器,使得开发人员可以快速开发...
android应用中常用的加密方式有三种:MD5,AES,RSA。在进行实际的开发过程中,一般是几种加密方式配合使用,这样加密效果会更好,被破解的概率会越小。里面包含了加密和解密的方法,这是demo1,还有一个demo2
微信小程序地图定位Demo,使用微信内置地图查看API定位,调用应用实例的方法获取全局数据,运行实例后,若手机上打开有GPS功能,则会自动在地图上定位至自己的位置,很多Android中的应用或微信小程序应用,都会默认...
该文件包括了安卓疯狂讲义中全部的demo案例,特别适合新手查看,大家可以参照安卓疯狂讲义电子书配合该demo学习,效果更佳,希望能帮助到大家!
首先这里面有好多android项目源码。都是常用的功能点。比如上拉下拉刷新等。其次忘记是哪里弄的了,在电脑里好久了、学习吧少年。
作者yy1300326388,源码cutPicDemo-for-Android,这个案例实现了可以支持拍照,以及缩放等常用的功能。
微信小程序地图定位Demo,使用微信内置地图查看API定位,调用应用实例的方法获取全局数据,运行实例后,若手机上打开有GPS功能,则会自动在地图上定位至自己的位置,很多Android中的应用或微信小程序应用,都会默认...
关于Android DownManager使用可见DownManager Demo。 3、ShellUtils Android Shell工具类,可用于检查系统root权限,并在shell或root用户下执行shell命令。如: checkRootPermission() 检查root权限 execCommand...
Android超完整的Android Kotlin项目是一款Android原生的开源Github客户端App,提供更丰富的...项目使用Retrofit2 RxJava2 Dagger2 DataBinding LiveData Navigation 等,MVVM 配合 Android JetPack,涉及各种常用控件。
Android 通过Socket 和服务器通讯,是一种比较常用的通讯方式,时间比较紧,说下大致的思路,希望能帮到使用socket 进行通信的人 (1)开启一个线程发送消息 SocketOutputThread 消息是放在队列里的,当有消息后,...