常用系统服务

来源:互联网 发布:java ee看源代码 编辑:程序博客网 时间:2024/06/03 05:08

行动是治愈恐惧的良药,而犹豫、拖延将不断滋养恐惧。


本讲内容:常用系统服务


一、常用系统服务

通过调用getSystemService(参数)    Aitivity的一个方法(继承父类的)获取系统某个服务

譬如:

传入的Name
返回的对象
说明
ALARM_SERVICE
AlarmManager
闹钟服务
NOTIFICATION_SERVICE
NotificationManager
状态栏服务


示例一:

   

下面是res/layout/activity_main.xml 布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/id_b1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="判断网络是否连接" />    <Button        android:id="@+id/id_b2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="开关WIFI" />    <Button        android:id="@+id/id_b3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="获取系统的音量" />    <Button        android:id="@+id/id_b4"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="获取当前进程包名" /></LinearLayout>


下面是MainActivity.java主界面文件:

public class MainActivity extends Activity implements OnClickListener{private Button b1;private Button b2;private Button b3;private Button b4;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);View view=inflater.inflate(R.layout.activity_main, null);setContentView(view);initViews();}private void initViews() {b1=(Button) findViewById(R.id.id_b1);b2=(Button) findViewById(R.id.id_b2);b3=(Button) findViewById(R.id.id_b3);b4=(Button) findViewById(R.id.id_b4);b1.setOnClickListener(this);b2.setOnClickListener(this);b3.setOnClickListener(this);b4.setOnClickListener(this);}public void onClick(View v) {switch (v.getId()) {case R.id.id_b1:if(isNetWorkConnected(MainActivity.this)){Toast.makeText(MainActivity.this, "网络已经打开", Toast.LENGTH_LONG).show();}else{Toast.makeText(MainActivity.this, "网络未连接", Toast.LENGTH_LONG).show();}break;case R.id.id_b2:WifiManager mWifiManamer = (WifiManager) MainActivity.this.getSystemService(WIFI_SERVICE);if(mWifiManamer.isWifiEnabled()){mWifiManamer.setWifiEnabled(false);Toast.makeText(MainActivity.this, "WIFI已经关闭", Toast.LENGTH_LONG).show();}else{mWifiManamer.setWifiEnabled(true);Toast.makeText(MainActivity.this, "WIFI已经打开", Toast.LENGTH_LONG).show();}break;case R.id.id_b3:AudioManager mAudioManager=(AudioManager) MainActivity.this.getSystemService(AUDIO_SERVICE);int max=mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);int current=mAudioManager.getStreamVolume(AudioManager.STREAM_RING);Toast.makeText(MainActivity.this, "系统的最大音量为:"+max+",当前音量是:"+current, Toast.LENGTH_SHORT).show();break;case R.id.id_b4:ActivityManager mActivityManager=(ActivityManager) MainActivity.this.getSystemService(ACTIVITY_SERVICE);String packageName=mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();Toast.makeText(MainActivity.this, "当前运行的Activity包名:"+packageName, Toast.LENGTH_SHORT).show();break;}}/** * 判断网络是否连接 */public boolean isNetWorkConnected(Context context){if(context!=null){ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);NetworkInfo mNetworkInfo=mConnectivityManager.getActiveNetworkInfo();if(mNetworkInfo!=null){return mNetworkInfo.isAvailable();}}return false;}}


权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/><uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/><uses-permission android:name="android.permission.GET_TASKS"/>



Take your time and enjoy it 



1 0