標籤:android style blog http color io os 使用 ar
轉:http://blog.csdn.net/zhandoushi1982/article/details/8501773
Context字面意思上下文,位於framework 的android.content.Context中。其實該類為LONG型,類似Win32中的Handle控制代碼,很多方法需要通過Context才能 得到調用者的執行個體,比如說Toast的第一個參數就是Context,一般在Activity中我們直接用this代替;而到了一個button的 onClick(View view)等方法時,我們用this時就會報錯,我們可能使用ActivityName.this來解決(或者 getBaseContext()/getApplicationContext())。
實現Context的類主要有Android特有的幾個模型,Activity、Service以及BroadcastReceiver。 Context提供了關於應用環境全域資訊的介面,同時啟動應用級的操作,如啟動Activity,broadcasting和接收intents。應用 程式App共有的Context數目公式為: 總Context執行個體個數 = Service個數 + Activity個數 + 1(Application對應的Context執行個體)。
BroadcastReceiver使用Context是通過在public void onReceive(Context context, Intent intent)這個形參來使用的。
================================================================================================================================
利用android.app.Activity.getSystemService這個方法,可以獲得系統常用的服務及一些列方法。舉例如下:
(1) 獲得當前正在啟動並執行應用程式套件名
建立一個activity,首先在XML中要添加擷取系統運行任務的許可權:
<uses-permission android:name="android.permission.GET_TASKS" />
否則運行時會報異常:java.lang.SecurityException: Permission Denial:。。。。。。requires android.permission.GET_TASKS
響應核心源碼如下:
import android.app.ActivityManager;public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { Log.i("zhangcheng","run here"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView strText = (TextView)findViewById(R.id.callinfo); ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); //獲得頁面管理服務 String currentPackageName = ""; currentPackageName = activityManager.getRunningTasks(1) .get(0).topActivity.getPackageName();//獲得當前運行任務頂層頁面的包名 strText.setText(currentPackageName); }}
我的java src路徑是:CallPhone\src\com\example\callphone\,所以最終顯示的包名是:com.example.callphone。
=========================================================================================
有時需要判斷當前app是否連網,是用的WIFI還是其他網路連接方式等。核心代碼如下:
(1)當前連網網路類型,是否連網了
ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); if(manager == null)return; NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo(); if(activeNetworkInfo == null) return; TextView display = (TextView)findViewById(R.id.testview1); if((activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) && activeNetworkInfo.isConnected()){ display.setText("WIFI NET"); }else if(activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE && activeNetworkInfo.isConnected()){ display.setText("MOBILE NET"); }
需要的許可權
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
(2)開關WIFI:
用到的許可權
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
開關按鈕
WifiManager wifiManager;wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);public void onClick(View v){ if(wifiManager.isWifiEnabled()){ wifiManager.setWifiEnabled(false); }else{ wifiManager.setWifiEnabled(true); }}
(3)開關移動網路。這個比較複雜,由於ConnectivityManager的setMobileDataEnabled函數是隱藏的,所以必 須採用JAVA函數反射來調用到(JAVA反射相關內容可以參見他人的博文http://write.blog.csdn.net/postedit /8501773,目前會用但是理解不透)。
定義全域變數
Context context = this;ConnectivityManager manager;TextView display;
頁面的onCreate函數初始化
manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); if(manager == null)return;display = (TextView)findViewById(R.id.testview1); //提示當前移動網路狀態
反射函數
public boolean invokeMethod(String methodName,Object[] arg) throws Exception {Class ownerClass = manager.getClass(); Class[] argsClass = null; if (arg != null) { argsClass = new Class[1]; argsClass[0] = arg.getClass(); } Method method = ownerClass.getMethod(methodName, argsClass); Boolean isOpen = (Boolean) method.invoke(manager, arg); return isOpen;}public Object invokeBooleanArgMethod(String methodName,boolean value) throws Exception { Class ownerClass = manager.getClass(); Class[] argsClass = new Class[1]; argsClass[0] = boolean.class; Method method = ownerClass.getMethod(methodName,argsClass); return method.invoke(manager, value);}
反覆單擊一個按鈕,可以看到移動網路在開關切換,從狀態列表徵圖可以看出,從頁面的textview文本也可以看出是對應的。
public void onClick(View v){//Log.i("zhangcheng","click");Object[] arg = null; try { boolean isMobileDataEnable = invokeMethod("getMobileDataEnabled", arg); if(!isMobileDataEnable){ invokeBooleanArgMethod("setMobileDataEnabled", true); display.setText("MOBILE NET ON"); } else{ invokeBooleanArgMethod("setMobileDataEnabled", false); display.setText("MOBILE NET OFF"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }}
需要的許可權
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
============================================================================================
通過程式擷取android系統手機的鈴聲和音量。同樣,設定鈴聲和音量的方法也很簡單!
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
(1)通話音量
int max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL ); int current = mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL ); Log.d(”VIOCE_CALL”, “max : ” + max + ” current : ” + current);
(2)系統音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_SYSTEM ); current = mAudioManager.getStreamVolume( AudioManager.STREAM_SYSTEM ); Log.d(”SYSTEM”, “max : ” + max + ” current : ” + current);
(3)鈴聲音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_RING ); current = mAudioManager.getStreamVolume( AudioManager.STREAM_RING ); Log.d(”RING”, “max : ” + max + ” current : ” + current);
(4)音樂音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC ); current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC ); Log.d(”MUSIC”, “max : ” + max + ” current : ” + current);
(5)提示聲音音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_ALARM ); current = mAudioManager.getStreamVolume( AudioManager.STREAM_ALARM ); Log.d(”ALARM”, “max : ” + max + ” current : ” + current);
設定音量的方法也很簡單:public void setStreamVolume(int streamType, int index, int flags) ,其中streamType為鈴聲類型,例如:AudioManager.STREAM_VOICE_CALL、 AudioManager.STREAM_SYSTEM等,index為音量大小,falgs為標誌位。
以下是修改音頻設定:
(6)通話時設定靜音
System.out.println("isMicrophoneMute =" + audioManager.isMicrophoneMute());audioManager.setMicrophoneMute(!audioManager.isMicrophoneMute());
(7)通話時設定免提
System.out.println("isSpeakerphoneOn =" + audioManager.isSpeakerphoneOn());audioManager.setSpeakerphoneOn(!audioManager.isSpeakerphoneOn());
別忘了修改的許可權 <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
===========================================================================================
設定震動:
mVibrator = (Vibrator) mContext.getSystemService(Service.VIBRATOR_SERVICE); long[] pattern = {150, 100}; // OFF/ON/OFF/ON... mVibrator.vibrate(pattern, -1);
參考原文:http://www.cnblogs.com/wgw8299/articles/2128202.html
參考原文:http://blog.csdn.net/comkingfly/article/details/7359950
參考原文:http://blog.csdn.net/ponderforever/article/details/7167591
參考原文:http://blog.csdn.net/gf771115/article/details/6996577
Android的Context && 安卓常用系統服務(當前運行包名/當前網路狀態和開關網路/音頻服務/馬達服務) (轉)