今天在网站看了一系列例子。太棒了。。。
我收藏了哦。
实现了Android中常见的许多服务,下面是实现的截图
接下来,以源代码的方式分析这个例子
1.MainActivity--主界面
这个类主要是实现用户所看到的这个Activity,其中包含了一系列的按钮,用户点击按钮执行相应的动作,所以在这个类中主要是对按钮的定义和对按钮绑定相应的监听器,下面是实现的代码:
- packagelovefang.stadyService;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.widget.Button;
- importandroid.view.View;
- importandroid.content.Intent;
- importandroid.util.Log;
- /**这是使用后台服务的学习例子*/
- publicclassMainStadyServicsextendsActivity{
- /**参数设置*/
- ButtonstartServiceButton;//启动服务按钮
- ButtonshutDownServiceButton;//关闭服务按钮
- ButtonstartBindServiceButton;//启动绑定服务按钮
- ButtonsendBroadcast;//使用广播
- ButtonnotificationButton;//使用通知功能
- ButtonalarmButton;//使用闹钟
- ButtonhandlerButton;//使用handler
- ButtonasyncButton;//使用异步加载
- ButtonphoneStateButton;//查看手机状态
- ButtoncallphoneButton;//拨打电话
- ButtonvibratorButton;//使用震动
- CountServicecountService;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- Log.v("MainStadyServics","setContentView");
- setContentView(R.layout.main);
- getWidget();
- regiestListener();
- }
- /**获得组件*/
- publicvoidgetWidget(){
- 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);
- }
- /**为按钮添加监听*/
- publicvoidregiestListener(){
- 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);
- }
- /**启动服务的事件监听*/
- publicButton.OnClickListenerstartService=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- /**单击按钮时启动服务*/
- Intentintent=newIntent(MainStadyServics.this,CountService.class);
- startService(intent);
- Log.v("MainStadyServics","startService");
- }
- };
- /**关闭服务*/
- publicButton.OnClickListenershutdownService=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- /**单击按钮时启动服务*/
- Intentintent=newIntent(MainStadyServics.this,CountService.class);
- /**退出Activity是,停止服务*/
- stopService(intent);
- Log.v("MainStadyServics","shutDownserveice");
- }
- };
- /**打开绑定服务的Activity*/
- publicButton.OnClickListenerstartBinderService=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- /**单击按钮时启动服务*/
- Intentintent=newIntent(MainStadyServics.this,UseBrider.class);
- startActivity(intent);
- Log.v("MainStadyServics","startBinderService");
- }
- };
- /**打开广播学习的按钮*/
- publicButton.OnClickListenerbroadcastReceiver=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseBroadcast.class);
- startActivity(intent);
- Log.v("MainStadyServics","startbroadcast");
- }
- };
- /**打开通知*/
- publicButton.OnClickListenernotification=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseNotification.class);
- startActivity(intent);
- Log.v("MainStadyService","startNotification");
- }
- };
- /**使用闹钟*/
- publicButton.OnClickListenerstartAlarm=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseAlarmManager.class);
- startActivity(intent);
- Log.v("MainStadyService","startalarm");
- }
- };
- publicButton.OnClickListenerhandler=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseHandleMessage.class);
- startActivity(intent);
- Log.v("MainStadyService","starthandle");
- }
- };
- publicButton.OnClickListenerasync=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseAsyncTask.class);
- startActivity(intent);
- Log.v("MainStadyService","starthandle");
- }
- };
- publicButton.OnClickListenerphonestate=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UsePhoneState.class);
- startActivity(intent);
- Log.v("MainStadyService","startphonestate");
- }
- };
- publicButton.OnClickListenercallphoneEvent=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseActionCall.class);
- startActivity(intent);
- Log.v("MainStadyService","startcallphone");
- }
- };
- publicButton.OnClickListenervibrator=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent(MainStadyServics.this,UseVibrator.class);
- startActivity(intent);
- Log.v("MainStadyService","startcallphone");
- }
- };
- /***/
- protectedvoidonDestroy(){
- super.onDestroy();
- Intentintent=newIntent(MainStadyServics.this,CountService.class);
- /**退出Activity是,停止服务*/
- stopService(intent);
- }
- }
2.启动服务按钮
这个类实现的是第一个按钮的功能,在这个类中新开了一个线程,并每隔一秒打印出一行日志
代码如下:
- packagelovefang.stadyService;
- /**引入包*/
- importandroid.app.Service;//服务的类
- importandroid.os.IBinder;
- importandroid.os.Binder;
- importandroid.content.Intent;
- importandroid.util.Log;
- /**计数的服务*/
- publicclassCountServiceextendsService{
- /**创建参数*/
- booleanthreadDisable;
- intcount;
- publicIBinderonBind(Intentintent){
- returnnull;
- }
- publicvoidonCreate(){
- super.onCreate();
- /**创建一个线程,每秒计数器加一,并在控制台进行Log输出*/
- newThread(newRunnable(){
- publicvoidrun(){
- while(!threadDisable){
- try{
- Thread.sleep(1000);
- }catch(InterruptedExceptione){
- }
- count++;
- Log.v("CountService","Countis"+count);
- }
- }
- }).start();
- }
- publicvoidonDestroy(){
- super.onDestroy();
- /**服务停止时,终止计数进程*/
- this.threadDisable=true;
- }
- publicintgetConunt(){
- returncount;
- }
- classServiceBinderextendsBinder{
- publicCountServicegetService(){
- returnCountService.this;
- }
- }
- }
3.绑定服务
服务有两种实现的方法:
1.startService,启动服务,这时需要程序员管理服务的生命周期
2.bindService,绑定服务,此时Service与Activity绑定在一起
下面是实现的代码:
- packagelovefang.stadyService;
- /**引入包*/
- importandroid.app.Activity;
- importandroid.content.ComponentName;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.content.ServiceConnection;
- importandroid.os.Bundle;
- importandroid.os.IBinder;
- importandroid.util.Log;
- /**通过bindService和unBindSerivce的方式启动和结束服务*/
- publicclassUseBriderextendsActivity{
- /**参数设置*/
- CountServicecountService;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(newUseBriderFace(this));
- Intentintent=newIntent(UseBrider.this,CountService.class);
- /**进入Activity开始服务*/
- bindService(intent,conn,Context.BIND_AUTO_CREATE);
- }
- privateServiceConnectionconn=newServiceConnection(){
- /**获取服务对象时的操作*/
- publicvoidonServiceConnected(ComponentNamename,IBinderservice){
- //TODOAuto-generatedmethodstub
- countService=((CountService.ServiceBinder)service).getService();
- }
- /**无法获取到服务对象时的操作*/
- publicvoidonServiceDisconnected(ComponentNamename){
- //TODOAuto-generatedmethodstub
- countService=null;
- }
- };
- protectedvoidonDestroy(){
- super.onDestroy();
- this.unbindService(conn);
- Log.v("MainStadyServics","out");
- }
- }
4.发送广播
使用sendBroadcast,向一个Action发送广播,并由相应的广播接收器接收并执行相应的动作
实现的代码如下:
4.1 打开广播服务
- packagelovefang.stadyService;
- /**引入包*/
- importandroid.view.View;
- importandroid.os.Bundle;
- importandroid.app.Activity;
- importandroid.content.Intent;
- importandroid.widget.Button;
- /**使用Broadcast,这是一个发送广播的类*/
- publicclassUseBroadcastextendsActivity{
- /**创建参数*/
- privateButtonsendBroadcast;
- /**创建Activity*/
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.broadcast);//使用布局文件
- getView();
- sendBroadcast.setOnClickListener(sendBroadcastClick);//添加事件监听
- }
- publicvoidgetView(){
- sendBroadcast=(Button)findViewById(R.id.sendBroadcast);
- }
- /**创建事件监听*/
- publicButton.OnClickListenersendBroadcastClick=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Intentintent=newIntent();//创建意图
- intent.putExtra("CONTENT","ThisisaBraodcastdemo");//设置广播的内容
- intent.setAction("lovefang.stadyService");//设置广播的Action
- sendBroadcast(intent);
- }
- };
- }
4.2 处理广播消息
- packagelovefang.stadyService;
- /***/
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.util.Log;
- /**这是一个接收广播的类*/
- publicclassUseBroadcastReceiverextendsBroadcastReceiver{
- publicvoidonReceive(Contextcontext,Intentintent){
- Log.v("UseBroadcastReceiver","Igetamessage");
- }
- }
5.Notification
这个称之为通知,显示在手机的通知栏,用户可以清除,可以点击
实现的代码如下:
- packagelovefang.stadyService;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- importandroid.app.Activity;
- importandroid.app.Notification;
- importandroid.app.NotificationManager;
- importandroid.app.PendingIntent;
- importandroid.net.Uri;
- importandroid.media.RingtoneManager;
- importandroid.widget.Button;
- importandroid.view.View;
- /**使用notification*/
- publicclassUseNotificationextendsActivity{
- /**创建组件*/
- privateButtontextButton;
- privateButtonsoundButton;//声音通知
- privateButtonvibrateButton;//震动通知
- privateButtonledButton;//led通知
- privateButtonoffButton;//关闭通知
- NotificationManagernotificationManager;
- /**创建Activity*/
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.notification);
- getComment();
- registerComment();
- }
- /**获取对象*/
- publicvoidgetComment(){
- /**获取Notification对象*/
- notificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
- textButton=(Button)findViewById(R.id.notificationMessage);
- soundButton=(Button)findViewById(R.id.notificationSound);
- vibrateButton=(Button)findViewById(R.id.notificationVibrate);
- ledButton=(Button)findViewById(R.id.notificationLED);
- offButton=(Button)findViewById(R.id.offnotification);
- }
- /**注册对象*/
- publicvoidregisterComment(){
- textButton.setOnClickListener(notificationMessage);
- soundButton.setOnClickListener(notificationSound);
- vibrateButton.setOnClickListener(notificationVibrate);
- ledButton.setOnClickListener(notificationLed);
- offButton.setOnClickListener(notificationOff);
- }
- publicButton.OnClickListenernotificationMessage=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Notificationnotification=newNotification();//创建Notification对象
- notification.icon=R.drawable.icon;
- notification.tickerText="Thisistextnotication";//设置通知消息
- /**单击通知后的Intent,此例子单击后还是在当前页面*/
- PendingIntentintent=PendingIntent
- .getActivity(UseNotification.this,
- 0,newIntent(UseNotification.this,UseNotification.class)
- ,0);
- /**设置通知消息*/
- notification.setLatestEventInfo(UseNotification.this
- ,"Notification","ContentofNotificationDemo",intent);
- /**执行通知*/
- notificationManager.notify(0,notification);
- }
- };
- publicButton.OnClickListenernotificationSound=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- /**创建通知对象*/
- Notificationnotification=newNotification();
- /**获取系统当前声音*/
- StringringName=RingtoneManager.getActualDefaultRingtoneUri(
- UseNotification.this,RingtoneManager.TYPE_RINGTONE)
- .toString();
- /**设置系统当前铃声为此通知的铃声*/
- notification.sound=Uri.parse(ringName);
- /**执行通知*/
- notificationManager.notify(0,notification);
- }
- };
- /**震动通知-七七八八网*/
- publicButton.OnClickListenernotificationVibrate=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Notificationnotification=newNotification();//创建Notification对象
- notification.vibrate=newlong[]{0,100,200,300};//设置通知震动模式
- notificationManager.notify(0,notification);//执行通知
- }
- };
- /**LED通知*/
- publicButton.OnClickListenernotificationLed=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- Notificationnotification=newNotification();//创建Notification对象
- notification.ledOnMS=300;//设置led开始闪光的时间
- notification.ledOffMS=1000;//设置关闭时的闪光时间
- notificationManager.notify(0,notification);//执行通知
- }
- };
- /**关闭通知*/
- publicButton.OnClickListenernotificationOff=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- notificationManager.cancel(0);//关闭通知
- }
- };
- }
6.Alarm
闹钟服务
- packagelovefang.stadyService;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.widget.Button;
- importandroid.view.View;
- importandroid.app.AlarmManager;
- importjava.util.Calendar;
- publicclassUseAlarmManagerextendsActivity{
- /**创建参数*/
- privateButtonstartAlarm;
- privateButtonshutdownAlarm;
- privateAlarmManageralarm;
- /**创建Activity*/
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.usealarmmanager);
- getWidget();
- }
- publicvoidgetWidget(){
- startAlarm=(Button)findViewById(R.id.startAlarm);
- shutdownAlarm=(Button)findViewById(R.id.shutDowntAlarm);
- alarm=(AlarmManager)getSystemService(ALARM_SERVICE);//获取AlarmManager
- }
- publicvoidregisterWidget(){
- startAlarm.setOnClickListener(startAlarms);
- shutdownAlarm.setOnClickListener(shutdownAlarms);
- }
- /**启动闹钟*/
- publicButton.OnClickListenerstartAlarms=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- //设置10秒后出发闹钟
- Calendarcalendar=Calendar.getInstance();
- calendar.setTimeInMillis(System.currentTimeMillis());//设置calendar的时间
- calendar.add(Calendar.SECOND,10);
- alarm.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),null);
- }
- };
- publicButton.OnClickListenershutdownAlarms=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- alarm.cancel(null);
- }
- };
- }
7.获取手机的状态
这个功能实现的是获取用户手机的一些定义的信息
- packagelovefang.stadyService;
- /**引入包*/
- importandroid.os.Bundle;
- importandroid.app.Activity;
- importandroid.app.Service;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.TextView;
- importandroid.content.ContentResolver;//Thisclassprovidesapplicationsaccesstothecontentmodel.
- importandroid.telephony.TelephonyManager;
- importandroid.util.Log;
- /**尖锋网-获取手机的状态*/
- publicclassUsePhoneStateextendsActivity{
- /**创建参数*/
- privateContentResolvercr;
- privateButtongetStateButton;//用来获取用户的手机状态
- /**创建Activity*/
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.usephonestate);
- cr=getContentResolver();
- Log.v("UsePhonestate","cr=getContentResolver()");
- Log.v("UsePhonestate","setContentView");
- getStateButton=(Button)findViewById(R.id.button_getphonestate);
- Log.v("UsePhonestate","getStateButton");
- getStateButton.setOnClickListener(getState);
- Log.v("UsePhonestate","getStateButton.setOnClickListener");
- }
- privateButton.OnClickListenergetState=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- /**获得TelephonyManager对象*/
- TelephonyManagertelephonyManager=(TelephonyManager)getSystemService(Service.TELEPHONY_SERVICE);
- /**获取电信网络级别*/
- StringteleCode=telephonyManager.getNetworkCountryIso();
- /**获取电信网络公司代码*/
- StringteleComCode=telephonyManager.getNetworkOperator();
- /**获取电信网络公司名称*/
- StringteleComName=telephonyManager.getNetworkOperatorName();
- /**获取行动通信类型*/
- intTypeCode=telephonyManager.getPhoneType();
- Stringtype="";
- switch(TypeCode){
- caseTelephonyManager.PHONE_TYPE_NONE:
- type="PHONE_TYPE_NONE";
- break;
- caseTelephonyManager.PHONE_TYPE_GSM:
- type="PHONE_TYPE_GSM";
- break;
- caseTelephonyManager.PHONE_TYPE_CDMA:
- type="PHONE_TYPE_CDMA";
- break;
- }
- /**获取网络类型*/
- intnetTypeCode=telephonyManager.getNetworkType();
- StringnetType="NETWORK_TYPE_UNKNOW";
- switch(netTypeCode){
- caseTelephonyManager.NETWORK_TYPE_1xRTT:
- netType="NETWORK_TYPE_1xRTT";
- break;
- caseTelephonyManager.NETWORK_TYPE_CDMA:
- netType="NETWORK_TYPE_CDMA";
- break;
- caseTelephonyManager.NETWORK_TYPE_EDGE:
- netType="NETWORK_TYPE_EDGE";
- break;
- caseTelephonyManager.NETWORK_TYPE_EVDO_0:
- netType="NETWORK_TYPE_EVDO_0";
- break;
- caseTelephonyManager.NETWORK_TYPE_EVDO_A:
- netType="NETWORK_TYPE_EVDO_A";
- break;
- caseTelephonyManager.NETWORK_TYPE_GPRS:
- netType="NETWORK_TYPE_GPRS";
- break;
- caseTelephonyManager.NETWORK_TYPE_HSDPA:
- netType="NETWORK_TYPE_HSDPA";
- break;
- caseTelephonyManager.NETWORK_TYPE_HSPA:
- netType="NETWORK_TYPE_HSPA";
- break;
- caseTelephonyManager.NETWORK_TYPE_HSUPA:
- netType="NETWORK_TYPE_HSUPA";
- break;
- caseTelephonyManager.NETWORK_TYPE_IDEN:
- netType="NETWORK_TYPE_IDEN";
- break;
- caseTelephonyManager.NETWORK_TYPE_UMTS:
- netType="NETWORK_TYPE_UMTS";
- break;
- default:
- break;
- }
- /**获取漫游状态*/
- booleanroamStatusCode=telephonyManager.isNetworkRoaming();
- StringroamStatus="NOTROAMINF";
- if(roamStatusCode){
- roamStatus="ROAMING";
- }
- /**http://www.jfong.cn/ -获取手机唯一标识*/
- Stringimei=telephonyManager.getDeviceId();
- /**获取手机IMEISV*/
- StringimeiSV=telephonyManager.getDeviceSoftwareVersion();
- /**获取手机IMSI*/
- Stringimsi=telephonyManager.getSubscriberId();
- /**http://www.qi788.com/ -蓝牙服务*/
- StringstatusCode=android.provider.Settings.System.getString(cr,
- android.provider.Settings.System.BLUETOOTH_ON);
- StringbulettothStatus="";
- if(statusCode.equals("1")){
- bulettothStatus="ENABLE";
- }else{
- bulettothStatus="DISABLE";
- }
- /**飞行模式是否打开*/
- statusCode=android.provider.Settings.System.getString(cr,
- android.provider.Settings.System.AIRPLANE_MODE_ON);
- StringAirplaneStatus="";
- if(statusCode.equals("1")){
- AirplaneStatus="ENABLE";
- }else{
- AirplaneStatus="DISABLE";
- }
- /**数据漫游模式是否打开*/
- statusCode=android.provider.Settings.System.getString(cr,
- android.provider.Settings.System.DATA_ROAMING);
- StringdataRoamStatus="";
- if(statusCode.equals("1")){
- dataRoamStatus="ENABLE";
- }else{
- dataRoamStatus="DISABLE";
- }
- TextViewtxt=(TextView)findViewById(R.id.text_showphonestate);
- StringBuildersb=newStringBuilder();
- sb.append("teleCode:"+teleCode+"\n");
- sb.append("teleComCode:"+teleComCode+"\n");
- sb.append("teleComName:"+teleComName+"\n");
- sb.append("type:"+type+"\n");
- sb.append("netType:"+netType+"\n");
- sb.append("roamStatus:"+roamStatus+"\n");
- sb.append("imei:"+imei+"\n");
- sb.append("imeiSV:"+imeiSV+"\n");
- sb.append("imsi:"+imsi+"\n");
- sb.append("bulettothStatus:"+bulettothStatus+"\n");
- sb.append("AirplaneStatus:"+AirplaneStatus+"\n");
- sb.append("dataRoamStatus:"+dataRoamStatus+"\n");
- txt.setText(sb.toString());
- }
- };
- }
8.Vibrator
震动功能,实现对手机震动的管理
- packagelovefang.stadyService;
- /***/
- importandroid.os.Bundle;
- importandroid.os.Vibrator;
- importandroid.app.Activity;
- importandroid.view.View;
- importandroid.content.Context;
- importandroid.widget.Button;
- /**如何实现手机的震动提示Vibrator*/
- publicclassUseVibratorextendsActivity{
- /***/
- privateButtonvibrator_1_Button;
- privateButtonvibrator_2_Button;
- privateButtonvibrator_3_Button;
- privateVibratorvibrator;
- /***/
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.use_vibrator);
- vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
- getWidget();
- registerWidget();
- }
- publicvoidgetWidget(){
- vibrator_1_Button=(Button)findViewById(R.id.button_vibrator_1);
- vibrator_2_Button=(Button)findViewById(R.id.button_vibrator_2);
- vibrator_3_Button=(Button)findViewById(R.id.button_vibrator_3);
- }
- publicvoidregisterWidget(){
- vibrator_1_Button.setOnClickListener(vibrator_1);
- vibrator_2_Button.setOnClickListener(vibrator_2);
- vibrator_3_Button.setOnClickListener(vibrator_3);
- }
- /**震动一次*/
- publicButton.OnClickListenervibrator_1=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- /**long参数数组里大参数的含义*/
- /**第一个参数表示等待100毫秒后开始震动*/
- /**第二个参数表示震动100毫秒后停止震动*/
- vibrator.vibrate(newlong[]{100,100},0);
- }
- };
- /**震动两次*/
- publicButton.OnClickListenervibrator_2=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- vibrator.vibrate(newlong[]{1000,3000,1000,3000},0);
- }
- };
- /**震动三次*/
- publicButton.OnClickListenervibrator_3=newButton.OnClickListener(){
- publicvoidonClick(Viewview){
- vibrator.vibrate(newlong[]{1000,1000,1000,2000,1000,300},0);
- }
- };
- }
相关推荐
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 消息是放在队列里的,当有消息后,...