为什么我的广播接收器不响应任何 phone 状态?
Why My broadcast receiver is not responding to any of the phone state?
实际上,我想要一个可以读取 phone 状态的广播接收器,例如 calling
、OFFHOOK
、idle
。
这是我的代码。
我在清单文件中添加了 <uses-permission>
用于读取 Phone 状态
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
这是我静态注册的接收器
<receiver android:name=".CallStateBroadcast">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
这是MainActivity.Java
package com.example.callstateapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
CallStateBroadcast myReceiver = new CallStateBroadcast();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(TelephonyManager.EXTRA_STATE);
registerReceiver(myReceiver,intentFilter);
}
}
这是CallStateBroadcast.Class
package com.example.callstateapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class CallStateBroadcast extends BroadcastReceiver {
Context mContext;
@Override
public void onReceive(final Context context, Intent intent)
{
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
Toast.makeText(context, "Ringing...", Toast.LENGTH_SHORT).show();
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
Toast.makeText(context, "Busy...", Toast.LENGTH_SHORT).show();
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
Toast.makeText(context, "Free...", Toast.LENGTH_SHORT).show();
}
}
要使用 READ_PHONE_STATE,您需要:
Note: If both your minSdkVersion and targetSdkVersion values are set to 3 or lower, the system implicitly grants your app this permission. If you don't need this permission, be sure your targetSdkVersion is 4 or higher.
那么,您需要指定:
<uses-sdk
android:minSdkVersion="20"
android:targetSdkVersion="whatyouwant" />
您需要在应用程序启动时至少注册一次接收器 class。您可以将它添加到您的 MainActivity.java
文件中,它将起作用。
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CallStateBroadcast myReceiver = new CallStateBroadcast();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(TelephonyManager.EXTRA_STATE);
registerReceiver(myReceiver,intentFilter);
}
CallStateBroadcast.java
public class CallStateBroadcast extends BroadcastReceiver {
Context mContext;
@Override
public void onReceive(final Context context, Intent intent)
{
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Log.e("Zed", "ringing");
Toast.makeText(context, "Ringing...", Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Log.e("Zed", "offhook");
Toast.makeText(context, "Busy...", Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Log.e("Zed", "idle");
Toast.makeText(context, "Free...", Toast.LENGTH_SHORT).show();
}
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<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>
<receiver android:name=".CallStateBroadcast">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
编辑
我上传了我的示例项目代码供您检查。 MinSdk 版本 21,TargetSdk 30.
这是工作的记录。 https://i.stack.imgur.com/wqqqO.gif
实际上,我想要一个可以读取 phone 状态的广播接收器,例如 calling
、OFFHOOK
、idle
。
这是我的代码。
我在清单文件中添加了 <uses-permission>
用于读取 Phone 状态
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
这是我静态注册的接收器
<receiver android:name=".CallStateBroadcast">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
这是MainActivity.Java
package com.example.callstateapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
CallStateBroadcast myReceiver = new CallStateBroadcast();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(TelephonyManager.EXTRA_STATE);
registerReceiver(myReceiver,intentFilter);
}
}
这是CallStateBroadcast.Class
package com.example.callstateapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class CallStateBroadcast extends BroadcastReceiver {
Context mContext;
@Override
public void onReceive(final Context context, Intent intent)
{
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
Toast.makeText(context, "Ringing...", Toast.LENGTH_SHORT).show();
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
Toast.makeText(context, "Busy...", Toast.LENGTH_SHORT).show();
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
Toast.makeText(context, "Free...", Toast.LENGTH_SHORT).show();
}
}
要使用 READ_PHONE_STATE,您需要:
Note: If both your minSdkVersion and targetSdkVersion values are set to 3 or lower, the system implicitly grants your app this permission. If you don't need this permission, be sure your targetSdkVersion is 4 or higher.
那么,您需要指定:
<uses-sdk
android:minSdkVersion="20"
android:targetSdkVersion="whatyouwant" />
您需要在应用程序启动时至少注册一次接收器 class。您可以将它添加到您的 MainActivity.java
文件中,它将起作用。
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CallStateBroadcast myReceiver = new CallStateBroadcast();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(TelephonyManager.EXTRA_STATE);
registerReceiver(myReceiver,intentFilter);
}
CallStateBroadcast.java
public class CallStateBroadcast extends BroadcastReceiver {
Context mContext;
@Override
public void onReceive(final Context context, Intent intent)
{
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Log.e("Zed", "ringing");
Toast.makeText(context, "Ringing...", Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Log.e("Zed", "offhook");
Toast.makeText(context, "Busy...", Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Log.e("Zed", "idle");
Toast.makeText(context, "Free...", Toast.LENGTH_SHORT).show();
}
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<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>
<receiver android:name=".CallStateBroadcast">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
编辑
我上传了我的示例项目代码供您检查。 MinSdk 版本 21,TargetSdk 30.
这是工作的记录。 https://i.stack.imgur.com/wqqqO.gif