简体   繁体   English

为什么 Android BroadcastReceiver 不工作?

[英]Why Android BroadcastReceiver not WORKING?

BroadcastReceiver is not working....广播接收器不工作....

Can someone help please I think I tried everything!有人可以帮忙吗我想我已经尝试了一切!

I can register it from the activity and its works fine but I want to register it from the manifest我可以从活动中注册它并且它工作正常,但我想从清单中注册它

public class MyRecever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("RECIVER STATE","on at START");
   
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
    if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
      
        Toast.makeText(context, incomingNumber.toString(), Toast.LENGTH_LONG).show();
        Log.d("RECIVER STATE","on while RINGING");
    }

    }

this is my manifest:这是我的清单:

<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=".MyRecever" android:exported="true">
            <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
        </receiver>

    </application>

Try this:尝试这个:

Inside your AndroidManifest.xml change your receiver code to:在您的AndroidManifest.xml将您的接收器代码更改为:

<receiver android:name=".MyRecever"
  android:enabled="true"
  android:exported="true">
    <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED" />
      <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
      <action android:name="android.intent.action.PHONE_STATE"/>
    </intent-filter>
</receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM