Android基礎知識(四)-----如何實時監聽數據流量開關狀態

一: 如何實時監聽數據開關狀態
  1. TelephonyManager管理類
  2. listen方法,第一個參數PhoneStateListener,第二個參數需要監聽的狀態telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
  3. 實現PhoneStateListener中相應的監聽函數
  4. 取消監聽狀態telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);

主要示例代碼如下:

package com.example.myapplication;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TelephonyManager telephonyManager;
    private Button btnOpen, btnClose;

    private PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onDataConnectionStateChanged(int state) {
            super.onDataConnectionStateChanged(state);
            Log.d("jasun", "===========onDataConnectionStateChanged====state==========" + state);
            Toast.makeText(MainActivity.this, "state = " + state, Toast.LENGTH_LONG).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        initView();
    }

    private void initView() {
        btnOpen = (Button) findViewById(R.id.btn_open);
        btnOpen.setOnClickListener(this);

        btnClose = (Button) findViewById(R.id.btn_close);
        btnClose.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_open:
                telephonyManager.setDataEnabled(true);
                break;
            case R.id.btn_close:
                telephonyManager.setDataEnabled(false);
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
    }
}

權限聲明如下,需要系統級權限,app無法使用。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.myapplication">


    <uses-permission
        android:name="android.permission.MODIFY_PHONE_STATE"
        tools:ignore="ProtectedPermissions" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

佈局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_open"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="OPEN_DATA" />

    <Button
        android:id="@+id/btn_close"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CLOSE_DATA" />

</LinearLayout>


您可能感興趣的文章:
Android源碼分析(一)-----如何快速掌握Android編譯文件
Android源碼分析(二)-----如何編譯修改後的framework資源文件
Android源碼分析(三)-----系統框架設計思想
Android源碼分析(四)-----Android源碼編譯及刷機步驟
Android源碼分析(五)-----如何從架構師的角度去設計Framework框架

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章