一: 如何实时监听数据开关状态
- TelephonyManager管理类
- listen方法,第一个参数PhoneStateListener,第二个参数需要监听的状态
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
- 实现PhoneStateListener中相应的监听函数
- 取消监听状态
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框架