當(dāng)前位置:首頁 > 芯聞號 > 充電吧
[導(dǎo)讀]說明:這個例子實(shí)現(xiàn)了Android中常見的許多服務(wù),下面是實(shí)現(xiàn)的詳細(xì)流程接下來,以源代碼的方式分析這個例子1.MainActivity--主界面這個類主要是實(shí)現(xiàn)用戶所看到的這個Activity,其中包

說明:這個例子實(shí)現(xiàn)了Android中常見的許多服務(wù),下面是實(shí)現(xiàn)的詳細(xì)流程

接下來,以源代碼的方式分析這個例子

1.MainActivity--主界面

這個類主要是實(shí)現(xiàn)用戶所看到的這個Activity,其中包含了一系列的按鈕,用戶點(diǎn)擊按鈕執(zhí)行相應(yīng)的動作,所以在這個類中主要是對按鈕的定義和對按鈕綁定相應(yīng)的監(jiān)聽器,下面是實(shí)現(xiàn)的代碼:

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;
/**這是使用后臺服務(wù)的學(xué)習(xí)例子*/
public?class?MainStadyServics?extends?Activity?{
????????/**參數(shù)設(shè)置*/
????Button?startServiceButton;//?啟動服務(wù)按鈕
????Button?shutDownServiceButton;//?關(guān)閉服務(wù)按鈕
????Button?startBindServiceButton;//?啟動綁定服務(wù)按鈕
????Button?sendBroadcast;//?使用廣播
????Button?notificationButton;//?使用通知功能
????Button?alarmButton;//?使用鬧鐘
????Button?handlerButton;//?使用handler
????Button?asyncButton;//?使用異步加載
????Button?phoneStateButton;//?查看手機(jī)狀態(tài)
????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);
????}
????????/**為按鈕添加監(jiān)聽*/
????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);
????}
????????/**啟動服務(wù)的事件監(jiān)聽*/
????public?Button.OnClickListener?startService?=?new?Button.OnClickListener(){
????????public?void?onClick(View?view){
????????????????/**單擊按鈕時啟動服務(wù)*/
????????????Intent?intent?=?new?Intent(MainStadyServics.this,CountService.class);
????????????startService(intent);
????????????Log.v("MainStadyServics",?"start?Service");
????????}
????};
????????/**關(guān)閉服務(wù)*/
????public?Button.OnClickListener?shutdownService?=?new?Button.OnClickListener(){
????????public?void?onClick(View?view){
????????????????/**單擊按鈕時啟動服務(wù)*/
????????????Intent?intent?=?new?Intent(MainStadyServics.this,CountService.class);
????????????????/**退出Activity是,停止服務(wù)*/
????????????stopService(intent);
????????????Log.v("MainStadyServics",?"shutDown?serveice");
????????}
????};
????????/**打開綁定服務(wù)的Activity*/
????public?Button.OnClickListener?startBinderService?=?new?Button.OnClickListener(){
????????public?void?onClick(View?view){
????????????????/**單擊按鈕時啟動服務(wù)*/
????????????Intent?intent?=?new?Intent(MainStadyServics.this,UseBrider.class);
????????????startActivity(intent);
????????????Log.v("MainStadyServics",?"start?Binder?Service");
????????}
????};
????????/**打開廣播學(xué)習(xí)的按鈕*/
????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是,停止服務(wù)*/
????????stopService(intent);
????}

}

?


2.啟動服務(wù)按鈕

?

這個類實(shí)現(xiàn)的是第一個按鈕的功能,在這個類中新開了一個線程,并每隔一秒打印出一行日志

代碼如下:

?

package?lovefang.stadyService;
/**引入包*/
????import?android.app.Service;//?服務(wù)的類
????import?android.os.IBinder;
????import?android.os.Binder;
????import?android.content.Intent;
????import?android.util.Log;
/**計(jì)數(shù)的服務(wù)*/
????public?class?CountService?extends?Service{
????????????/**創(chuàng)建參數(shù)*/
????????boolean?threadDisable?;
????????int?count;
????????
????????public?IBinder?onBind(Intent?intent){
????????????return?null;
????????}
????????public?void?onCreate(){
????????????super.onCreate();
????????????????/**創(chuàng)建一個線程,每秒計(jì)數(shù)器加一,并在控制臺進(jìn)行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();
????????????????/**服務(wù)停止時,終止計(jì)數(shù)進(jìn)程*/
????????????this.threadDisable?=?true;
????????}
????????public?int?getConunt(){
????????????return?count;
????????}
????????class?ServiceBinder?extends?Binder{
????????????public?CountService?getService(){
????????????????return?CountService.this;
????????????}
????????}
????}

?


3.綁定服務(wù)

?

服務(wù)有兩種實(shí)現(xiàn)的方法:

1.startService,啟動服務(wù),這時需要程序員管理服務(wù)的生命周期

2.bindService,綁定服務(wù),此時Service與Activity綁定在一起

下面是實(shí)現(xiàn)的代碼:

?

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的方式啟動和結(jié)束服務(wù)*/
????public?class?UseBrider?extends?Activity?{
????????????/**參數(shù)設(shè)置*/
????????CountService?countService;
????
????????@Override
????????public?void?onCreate(Bundle?savedInstanceState)?{
????????????super.onCreate(savedInstanceState);
????????????setContentView(new?UseBriderFace(this));
????????????Intent?intent?=?new?Intent(UseBrider.this,CountService.class);
????????????????/**進(jìn)入Activity開始服務(wù)*/
????????????bindService(intent,?conn,?Context.BIND_AUTO_CREATE);
????????????
????????}
????????private?ServiceConnection?conn?=?new?ServiceConnection(){
????????????????/**獲取服務(wù)對象時的操作*/?
????????????public?void?onServiceConnected(ComponentName?name,?IBinder?service)?{
????????????????//?TODO?Auto-generated?method?stub
????????????????countService?=?((CountService.ServiceBinder)service).getService();
????????????????
????????????}
????????????????/**無法獲取到服務(wù)對象時的操作*/
????????????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.發(fā)送廣播

?

使用sendBroadcast,向一個Action發(fā)送廣播,并由相應(yīng)的廣播接收器接收并執(zhí)行相應(yīng)的動作

實(shí)現(xiàn)的代碼如下:

4.1 打開廣播服務(wù)

?

package?lovefang.stadyService;
/**引入包*/
????import?android.view.View;
????import?android.os.Bundle;
????import?android.app.Activity;
????import?android.content.Intent;
????import?android.widget.Button;
/**使用Broadcast,這是一個發(fā)送廣播的類*/
????public?class?UseBroadcast?extends?Activity{
????????????/**創(chuàng)建參數(shù)*/
????????private?Button?sendBroadcast;
????????????/**創(chuàng)建Activity*/
????????public?void?onCreate(Bundle?savedInstanceState){
????????????super.onCreate(savedInstanceState);
????????????setContentView(R.layout.broadcast);//?使用布局文件
????????????getView();
????????????sendBroadcast.setOnClickListener(sendBroadcastClick);//?添加事件監(jiān)聽
????????}
????????public?void?getView(){
????????????sendBroadcast?=?(Button)findViewById(R.id.sendBroadcast);
????????}
????????????/**創(chuàng)建事件監(jiān)聽*/
????????public?Button.OnClickListener?sendBroadcastClick?=?new?Button.OnClickListener(){
????????????public?void?onClick(View?view){
????????????????Intent?intent?=?new?Intent();//?創(chuàng)建意圖
????????????????intent.putExtra("CONTENT",??"This?is?a?Braodcast?demo");//?設(shè)置廣播的內(nèi)容
????????????????intent.setAction("lovefang.stadyService");//?設(shè)置廣播的Action
????????????????sendBroadcast(intent);
????????????}
????????};
????????
????}

?

4.2 處理廣播消息

package?lovefang.stadyService;
/***/
????import?android.content.BroadcastReceiver;
????import?android.content.Context;
????import?android.content.Intent;
????import?android.util.Log;
/**這是一個接收廣播的類*/
????public?class?UseBroadcastReceiver?extends?BroadcastReceiver{
????????public?void?onReceive(Context?context,?Intent?intent){
????????????Log.v("UseBroadcastReceiver",?"I?get?a?message");
????????}
????}


5.Notification

這個稱之為通知,顯示在手機(jī)的通知欄,用戶可以清除,可以點(diǎn)擊

實(shí)現(xiàn)的代碼如下:

?

package?lovefang.stadyService;

????import?android.content.Intent;
????import?android.os.Bundle;
????import?android.app.Activity;
????import?android.app.Notification;
????import?android.app.NotificationManager;
????import?android.app.PendingIntent;
????import?android.net.Uri;
????import?android.media.RingtoneManager;
????import?android.widget.Button;
????import?android.view.View;

/**使用notification*/
????public?class?UseNotification?extends?Activity?{
????????????/**創(chuàng)建組件*/
????????private?Button?textButton;
????????private?Button?soundButton;//?聲音通知
????????private?Button?vibrateButton;//?震動通知
????????private?Button?ledButton;//?led通知
????????private?Button?offButton;//?關(guān)閉通知
????????NotificationManager?notificationManager;
????????????/**創(chuàng)建Activity*/
????????public?void?onCreate(Bundle?savedInstanceState){
????????????super.onCreate(savedInstanceState);
????????????setContentView(R.layout.notification);
????????????getComment();
????????????registerComment();
????????}
????????????/**獲取對象*/
????????public?void?getComment(){
????????????????/**獲取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);
????????}
????????????/**注冊對象*/
????????public?void?registerComment(){
????????????textButton.setOnClickListener(notificationMessage);
????????????soundButton.setOnClickListener(notificationSound);
????????????vibrateButton.setOnClickListener(notificationVibrate);
????????????ledButton.setOnClickListener(notificationLed);
????????????offButton.setOnClickListener(notificationOff);
????????}
????????public?Button.OnClickListener?notificationMessage?=?new?Button.OnClickListener(){
????????????public?void?onClick(View?view){
????????????????Notification?notification?=?new?Notification();//?創(chuàng)建Notification對象
????????????????notification.icon?=?R.drawable.icon;
????????????????notification.tickerText?=?"This?is?text?notication";//?設(shè)置通知消息
????????????????????/**單擊通知后的Intent,此例子單擊后還是在當(dāng)前頁面*/
????????????????PendingIntent?intent?=?PendingIntent
????????????????????.getActivity(UseNotification.this,
????????????????????????????0,?new?Intent(UseNotification.this,UseNotification.class)
????????????????????????????,?0);
????????????????????/**設(shè)置通知消息*/
????????????????notification.setLatestEventInfo(UseNotification.this
????????????????????????,"Notification","Content?of?Notification?Demo",intent);
????????????????????/**執(zhí)行通知*/
????????????????notificationManager.notify(0,?notification);
????????????}
????????};
????????public?Button.OnClickListener?notificationSound?=?new?Button.OnClickListener(){
????????????public?void?onClick(View?view){
????????????????????/**創(chuàng)建通知對象*/
????????????????Notification?notification?=?new?Notification();
????????????????????/**獲取系統(tǒng)當(dāng)前聲音*/
????????????????String?ringName?=?RingtoneManager.getActualDefaultRingtoneUri(
????????????????????????UseNotification.this,?RingtoneManager.TYPE_RINGTONE)
????????????????????????.toString();
????????????????????/**設(shè)置系統(tǒng)當(dāng)前鈴聲為此通知的鈴聲*/
????????????????notification.sound?=?Uri.parse(ringName);
????????????????????/**執(zhí)行通知*/
????????????????notificationManager.notify(0,notification);
????????????}
????????};
????????????/**震動通知*/
????????public?Button.OnClickListener?notificationVibrate?=?new?Button.OnClickListener(){
????????????public?void?onClick(View?view){
????????????????Notification?notification?=?new?Notification();//?創(chuàng)建Notification對象
????????????????notification.vibrate?=?new?long[]?{0,?100,?200,?300};//?設(shè)置通知震動模式
????????????????notificationManager.notify(0,notification);//?執(zhí)行通知
????????????}
????????};
????????????/**LED通知*/
????????public?Button.OnClickListener?notificationLed?=?new?Button.OnClickListener(){
????????????public?void?onClick(View?view){
????????????????Notification?notification?=?new?Notification();//?創(chuàng)建Notification對象
????????????????notification.ledOnMS?=?300;//?設(shè)置led開始閃光的時間
????????????????notification.ledOffMS?=?1000;//?設(shè)置關(guān)閉時的閃光時間
????????????????notificationManager.notify(0,notification);//?執(zhí)行通知
????????????}
????????};
????????????/**關(guān)閉通知*/
????????public?Button.OnClickListener?notificationOff?=?new?Button.OnClickListener(){
????????????public?void?onClick(View?view){
????????????????notificationManager.cancel(0);//?關(guān)閉通知
????????????}
????????};
????}

?


6.Alarm

?

鬧鐘服務(wù)

?

package?lovefang.stadyService;

import?android.app.Activity;
import?android.os.Bundle;
import?android.widget.Button;
import?android.view.View;
import?android.app.AlarmManager;

import?java.util.Calendar;

public?class?UseAlarmManager?extends?Activity?{
????????/**創(chuàng)建參數(shù)*/
????private?Button?startAlarm;
????private?Button?shutdownAlarm;
????private?AlarmManager?alarm;
????
????????/**創(chuàng)建Activity*/
????public?void?onCreate(Bundle?savedInstanceState){
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.usealarmmanager);
????????getWidget();
????}
????public?void?getWidget(){
????????startAlarm?=?(Button)findViewById(R.id.startAlarm);
????????shutdownAlarm?=?(Button)findViewById(R.id.shutDowntAlarm);
????????alarm?=?(AlarmManager)getSystemService(ALARM_SERVICE);//?獲取AlarmManager
????}
????public?void?registerWidget(){
????????startAlarm.setOnClickListener(startAlarms);
????????shutdownAlarm.setOnClickListener(shutdownAlarms);
????}
????????/**啟動鬧鐘*/
????public?Button.OnClickListener?startAlarms?=?new?Button.OnClickListener(){
????????public?void?onClick(View?view){
????????????????//?設(shè)置10秒后出發(fā)鬧鐘
????????????Calendar?calendar?=?Calendar.getInstance();
????????????calendar.setTimeInMillis(System.currentTimeMillis());//?設(shè)置calendar的時間
????????????calendar.add(Calendar.SECOND,?10);
????????????
????????????alarm.set(AlarmManager.RTC_WAKEUP,?calendar.getTimeInMillis(),?null);
????????}
????};
????public?Button.OnClickListener?shutdownAlarms?=?new?Button.OnClickListener(){
????????public?void?onClick(View?view){
????????????alarm.cancel(null);
????????}
????};
}


7.獲取手機(jī)的狀態(tài)

這個功能實(shí)現(xiàn)的是獲取用戶手機(jī)的一些定義的信息

package?lovefang.stadyService;
/**引入包*/
????import?android.os.Bundle;
????import?android.app.Activity;
????import?android.app.Service;
????import?android.view.View;
????import?android.widget.Button;
????import?android.widget.TextView;
????import?android.content.ContentResolver;//This?class?provides?applications?access?to?the?content?model.
????import?android.telephony.TelephonyManager;
????import?android.util.Log;
/**獲取手機(jī)的狀態(tài)*/
????public?class?UsePhoneState?extends?Activity{
????????????/**創(chuàng)建參數(shù)*/
????????private?ContentResolver?cr;
????????private?Button?getStateButton;//?用來獲取用戶的手機(jī)狀態(tài)
????????????/**創(chuàng)建Activity*/
????????public?void?onCreate(Bundle?savedInstanceState){
????????????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");
????????}
????????private?Button.OnClickListener?getState?=?new?Button.OnClickListener(){
????????????public?void?onClick(View?view){
????????????????????/**獲得TelephonyManager對象*/
????????????????TelephonyManager?telephonyManager?=?(TelephonyManager)?getSystemService(Service.TELEPHONY_SERVICE);
????????????????????/**獲取電信網(wǎng)絡(luò)級別*/
????????????????String?teleCode?=?telephonyManager.getNetworkCountryIso();
????????????????????/**獲取電信網(wǎng)絡(luò)公司代碼*/
????????????????String?teleComCode?=?telephonyManager.getNetworkOperator();
????????????????????/**獲取電信網(wǎng)絡(luò)公司名稱*/
????????????????String?teleComName?=?telephonyManager.getNetworkOperatorName();
????????????????????/**獲取行動通信類型*/
????????????????int?TypeCode?=?telephonyManager.getPhoneType();
????????????????
????????????????String?type?=?"";
????????????????
????????????????switch(TypeCode){
????????????????????case?TelephonyManager.PHONE_TYPE_NONE:
????????????????????????type?=?"PHONE_TYPE_NONE";
????????????????????????break;
????????????????????case?TelephonyManager.PHONE_TYPE_GSM:
????????????????????????type?=?"PHONE_TYPE_GSM";
????????????????????????break;
????????????????????case?TelephonyManager.PHONE_TYPE_CDMA:
????????????????????????type?=?"PHONE_TYPE_CDMA";
????????????????????????break;
????????????????}
????????????????????/**獲取網(wǎng)絡(luò)類型*/
????????????????int?netTypeCode?=?telephonyManager.getNetworkType();
????????????????String?netType?=?"NETWORK_TYPE_UNKNOW";
????????????????switch(netTypeCode){
????????????????????case?TelephonyManager.NETWORK_TYPE_1xRTT:
????????????????????????netType?=?"NETWORK_TYPE_1xRTT";
????????????????????????break;
????????????????????case?TelephonyManager.NETWORK_TYPE_CDMA:
????????????????????????netType?=?"NETWORK_TYPE_CDMA";
????????????????????????break;
????????????????????case?TelephonyManager.NETWORK_TYPE_EDGE:
????????????????????????netType?=?"NETWORK_TYPE_EDGE";
????????????????????????break;
????????????????????case?TelephonyManager.NETWORK_TYPE_EVDO_0:
????????????????????????netType?=?"NETWORK_TYPE_EVDO_0";
????????????????????????break;
????????????????????case?TelephonyManager.NETWORK_TYPE_EVDO_A:
????????????????????????netType?=?"NETWORK_TYPE_EVDO_A";
????????????????????????break;
????????????????????case?TelephonyManager.NETWORK_TYPE_GPRS:
????????????????????????netType?=?"NETWORK_TYPE_GPRS";
????????????????????????break;
????????????????????case?TelephonyManager.NETWORK_TYPE_HSDPA:
????????????????????????netType?=?"NETWORK_TYPE_HSDPA";
????????????????????????break;
????????????????????case?TelephonyManager.NETWORK_TYPE_HSPA:
????????????????????????netType?=?"NETWORK_TYPE_HSPA";
????????????????????????break;
????????????????????case?TelephonyManager.NETWORK_TYPE_HSUPA:
????????????????????????netType?=?"NETWORK_TYPE_HSUPA";
????????????????????????break;
????????????????????case?TelephonyManager.NETWORK_TYPE_IDEN:
????????????????????????netType?=?"NETWORK_TYPE_IDEN";
????????????????????????break;
????????????????????case?TelephonyManager.NETWORK_TYPE_UMTS:
????????????????????????netType?=?"NETWORK_TYPE_UMTS";
????????????????????????break;
????????????????????default:
????????????????????????break;
????????????????}
????????????????
????????????????????/**獲取漫游狀態(tài)*/
????????????????boolean?roamStatusCode?=?telephonyManager.isNetworkRoaming();
????????????????String?roamStatus?=?"NOT?ROAMINF";
????????????????if(roamStatusCode){
????????????????????roamStatus?=?"ROAMING";
????????????????}
????????????????
????????????????????/**獲取手機(jī)唯一標(biāo)識*/
????????????????String?imei?=?telephonyManager.getDeviceId();
????????????????????/**獲取手機(jī)IMEI?SV*/
????????????????String?imeiSV?=?telephonyManager.getDeviceSoftwareVersion();
????????????????????/**獲取手機(jī)IMSI*/
????????????????String?imsi?=?telephonyManager.getSubscriberId();
????????????????
????????????????????/**藍(lán)牙服務(wù)*/
????????????????String?statusCode?=?android.provider.Settings.System.getString(cr,
????????????????????????android.provider.Settings.System.BLUETOOTH_ON);
????????????????String?bulettothStatus?=?"";
????????????????if(statusCode.equals("1")){
????????????????????bulettothStatus?=?"ENABLE";
????????????????}else{
????????????????????bulettothStatus?=?"DISABLE";
????????????????}
????????????????
????????????????????/**飛行模式是否打開*/
????????????????statusCode?=?android.provider.Settings.System.getString(cr,
????????????????????????android.provider.Settings.System.AIRPLANE_MODE_ON);
????????????????
????????????????String?AirplaneStatus?=?"";
????????????????if(statusCode.equals("1")){
????????????????????AirplaneStatus?=?"ENABLE";
????????????????}else{
????????????????????AirplaneStatus?=?"DISABLE";
????????????????}
????????????????
????????????????????/**數(shù)據(jù)漫游模式是否打開*/
????????????????statusCode?=?android.provider.Settings.System.getString(cr,
????????????????????????android.provider.Settings.System.DATA_ROAMING);
????????????????String?dataRoamStatus?=?"";
????????????????if(statusCode.equals("1")){
????????????????????dataRoamStatus?=?"ENABLE";
????????????????}else{
????????????????????dataRoamStatus?=?"DISABLE";
????????????????}
????????????????TextView?txt?=?(TextView)?findViewById(R.id.text_showphonestate);
????????????????StringBuilder?sb?=?new?StringBuilder();
????????????????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

?

震動功能,實(shí)現(xiàn)對手機(jī)震動的管理

?

package?lovefang.stadyService;
/***/
????import?android.os.Bundle;
????import?android.os.Vibrator;
????import?android.app.Activity;
????import?android.view.View;
????import?android.content.Context;
????import?android.widget.Button;
/**如何實(shí)現(xiàn)手機(jī)的震動提示Vibrator*/
????public?class?UseVibrator?extends?Activity{
????????????/***/
????????private?Button?vibrator_1_Button;
????????private?Button?vibrator_2_Button;
????????private?Button?vibrator_3_Button;
????????private?Vibrator?vibrator;
????????????/***/
????????public?void?onCreate(Bundle?savedInstanceState){
????????????super.onCreate(savedInstanceState);
????????????setContentView(R.layout.use_vibrator);
????????????vibrator?=?(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
????????????getWidget();
????????????registerWidget();
????????}
????????
????????public?void?getWidget(){
????????????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);
????????}
????????
????????public?void?registerWidget(){
????????????vibrator_1_Button.setOnClickListener(vibrator_1);
????????????vibrator_2_Button.setOnClickListener(vibrator_2);
????????????vibrator_3_Button.setOnClickListener(vibrator_3);
????????}
????????????/**震動一次*/
????????public?Button.OnClickListener?vibrator_1?=?new?Button.OnClickListener(){
????????????public?void?onClick(View?view){
????????????????????/**long參數(shù)數(shù)組里大參數(shù)的含義*/
????????????????????/**第一個參數(shù)表示等待100毫秒后開始震動*/
????????????????????/**第二個參數(shù)表示震動100毫秒后停止震動*/
????????????????vibrator.vibrate(new?long[]{100,100},?0);
????????????}
????????};
????????????/**震動兩次*/
????????public?Button.OnClickListener?vibrator_2?=?new?Button.OnClickListener(){
????????????public?void?onClick(View?view){
????????????????vibrator.vibrate(new?long[]{1000,3000,1000,3000},?0);
????????????}
????????};
????????????/**震動三次*/
????????public?Button.OnClickListener?vibrator_3?=?new?Button.OnClickListener(){
????????????public?void?onClick(View?view){
????????????????vibrator.vibrate(new?long[]{1000,1000,1000,2000,1000,300},?0);
????????????}
????????};
????}


本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險,如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運(yùn)營商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學(xué)會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團(tuán))股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉