1

I'm trying to add a Broadcast receiver for PhoneStateListener. I've declared receiver in the manifest,

<receiver
        android:name="my.pckname.receiver.IncomingCallReceiver"
        android:enabled="false" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

And I'm trying to enable and disable the receiver through preference screen using CheckBoxPreference.

<PreferenceCategory android:title="Title here" >
    <CheckBoxPreference
        android:key="allow_calls_from_circle"
        android:summary="Summary here"
        android:title="This a title" />

    <EditTextPreference
        android:dialogTitle="Title here"
        android:key="default_sms_body"
        android:summary="Summary here"
        android:title="Title here"
        android:dependency="allow_calls_from_circle" 
       />
</PreferenceCategory>

However, I'm able to enable it, it works fine! And if I try to disable it, it is still receiving the broadcast messages. Here is my code.

public class Settings extends SherlockPreferenceActivity implements
 OnPreferenceClickListener {

private static final String TAG = Settings.class.getSimpleName();

CheckBoxPreference allowCallsFromCircle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // deprecated no replacement API level below 13
    addPreferencesFromResource(R.xml.settings);

    allowCallsFromCircle = (CheckBoxPreference) findPreference("allow_calls_from_circle");
    allowCallsFromCircle.setOnPreferenceClickListener(this);
}


@Override
public boolean onPreferenceClick(Preference preference) {
    Log.d(TAG, "onPreferenceClick");

    ComponentName component = new ComponentName(getBaseContext(),
            IncomingCallReceiver.class);
    int status = getPackageManager().getComponentEnabledSetting(component);
    Log.d(TAG, "status" + status);

    if ((status == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT && allowCallsFromCircle
            .isChecked())
            || (status == PackageManager.COMPONENT_ENABLED_STATE_DISABLED && allowCallsFromCircle
                    .isChecked())) {
        getPackageManager().setComponentEnabledSetting(component,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
        Log.d(TAG, "enabled");

    } else if (status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
            && !allowCallsFromCircle.isChecked()) {
        getPackageManager().setComponentEnabledSetting(component,
                PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
                PackageManager.DONT_KILL_APP);
        Log.d(TAG, "disabled");
    }
    return true;
}}

Note : Before I posting this question, I tried similar solution as this one here , but it didn't work for me.

EDIT : If I use 0 instead of PackageManager.DONT_KILL_APP it is working, but it closes the app.



Thanks in advance.

Community
  • 1
  • 1

2 Answers2

0

I think your mistake is in receiver creation - class name should be "The name of the class inside of pkg that implements the component.". So, in your case it must be simply ".IncomingCallReceiver"

I usually use another form on this method, passing a class to it - http://developer.android.com/reference/android/content/ComponentName.html#ComponentName%28android.content.Context,%20java.lang.Class%3C?%3E%29

Andrey Kopeyko
  • 1,556
  • 15
  • 14
  • I think ComponentName is not a problem since I'm able to enable the receiver, disabling the receiver is my problem. And also I tried passing context and cls name as well but still the same problem. –  Mar 13 '15 at 10:15
0

My solution is register and unregister the listener IncomingCallReceiver in java code. Receiver can be registered via the Android manifest file. You can also register and unregister a receiver at runtime via the Context.registerReceiver() and Context.unregisterReceiver() methods. You can see the example in here

ThaiPD
  • 3,503
  • 3
  • 30
  • 48