简体   繁体   中英

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:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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