为什么我的广播接收器不对任何电话状态做出响应?

huangapple go评论62阅读模式
英文:

Why My broadcast receiver is not responding to any of the phone state?

问题

我需要翻译的部分如下:

"Actually, I want a broadcast receiver that can read the phone state like calling, OFFHOOK, idle.
here is my code.

I have added the <uses-permission> for reading Phone State in Manifest file

Here is my Statically registered Receiver





This is the 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);
}

}

This is 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();
}

}"

请注意,以上内容已经翻译成中文。

英文:

Actually, I want a broadcast receiver that can read the phone state like calling, OFFHOOK, idle.
here is my code.

I have added the &lt;uses-permission&gt; for reading Phone State in Manifest file

&lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot;/&gt;

Here is my Statically registered Receiver

&lt;receiver android:name=&quot;.CallStateBroadcast&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.PHONE_STATE&quot;/&gt;
        &lt;/intent-filter&gt;
&lt;/receiver&gt;

This is the 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);
    }
}

This is 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, &quot;Ringing...&quot;, Toast.LENGTH_SHORT).show();
        if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
            Toast.makeText(context, &quot;Busy...&quot;, Toast.LENGTH_SHORT).show();
        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
            Toast.makeText(context, &quot;Free...&quot;, Toast.LENGTH_SHORT).show();
    }
}

答案1

得分: 1

您需要在应用程序启动时至少注册一次接收器类您可以在您的 `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>

</manifest>

Edit

我已经为您检查了示例项目的代码。MinSdk 版本为 21,TargetSdk 版本为 30。

以下是工作录像:https://i.stack.imgur.com/wqqqO.gif


<details>
<summary>英文:</summary>
You need to register your receiver class at least once when your application starts. You can add this in your `MainActivity.java` file and it will work.
**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(&quot;Zed&quot;, &quot;ringing&quot;);
Toast.makeText(context, &quot;Ringing...&quot;, Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Log.e(&quot;Zed&quot;, &quot;offhook&quot;);
Toast.makeText(context, &quot;Busy...&quot;, Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Log.e(&quot;Zed&quot;, &quot;idle&quot;);
Toast.makeText(context, &quot;Free...&quot;, Toast.LENGTH_SHORT).show();
}
}
}
**AndroidManifest.xml**
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
package=&quot;com.example.myapplication&quot;&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot;/&gt;
&lt;application
android:allowBackup=&quot;true&quot;
android:icon=&quot;@mipmap/ic_launcher&quot;
android:label=&quot;@string/app_name&quot;
android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
android:supportsRtl=&quot;true&quot;
android:theme=&quot;@style/AppTheme&quot;&gt;
&lt;activity android:name=&quot;.MainActivity&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&lt;receiver android:name=&quot;.CallStateBroadcast&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.PHONE_STATE&quot;/&gt;
&lt;/intent-filter&gt;
&lt;/receiver&gt;
&lt;/application&gt;
&lt;/manifest&gt;
**Edit** 
I uploaded my sample project code for you to check. MinSdk version 21, TargetSdk 30.
Here is the recording of working. https://i.stack.imgur.com/wqqqO.gif
</details>
# 答案2
**得分**: 0
使用READ_PHONE_STATE权限,您需要:
注意:如果您的minSdkVersion和targetSdkVersion值都设置为3或更低,系统会隐式授予您的应用此权限。如果您不需要此权限,请确保您的targetSdkVersion为4或更高。
然后,您需要指定:
```xml
<uses-sdk
android:minSdkVersion="20"
android:targetSdkVersion="您想要的版本" />
英文:

To use READ_PHONE_STATE you need:

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&#39;t need this permission, be sure your targetSdkVersion is 4 or higher.

Then, you need to specify:

&lt;uses-sdk
android:minSdkVersion=&quot;20&quot;
android:targetSdkVersion=&quot;whatyouwant&quot; /&gt;

huangapple
  • 本文由 发表于 2020年10月6日 17:49:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64223362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定