Abhishek Suri
Abhishek Suri

Reputation: 33

How to use broadcastreceiver class in service class

In this example firstly i am starting the activity class in that i am calling startservice() and i am getting toast of "in on create" from oncreate method of service class And in the service class i did this coding

public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(getApplicationContext(), "in on create", Toast.LENGTH_SHORT).show();

    }

public void onStart(Intent arg0, int startId) {
        // TODO Auto-generated method stub
        super.onStart(arg0, startId);

    }

    public void broadcastIntent(View view)
       {
          Intent intent = new Intent();
          intent.setAction("android.intent.action.PHONE_STATE");
          sendBroadcast(intent);
       }

in the broadcast receiver class i did this

public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            Toast.makeText(context,"Phone is Ringing", Toast.LENGTH_LONG).show();
            Intent i=new Intent(context,MainActivity.class);
            i.putExtra("state", state);
            context.startActivity(i);

        }


    }

and in the manifest file i did...

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>


<receiver 
            android:name="Start">

            </receiver> 
            <service 
                android:name="Run"></service>

Upvotes: 0

Views: 262

Answers (1)

Gopal Gopi
Gopal Gopi

Reputation: 11131

Declare the receiver in manifest like this...

    <receiver
        android:name=".Start"
        android:priority="999" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

here android:name=".Start" represents the class name of BroadcastReceiver. Here Start is class name of BroadcastReceiver whose package is equal to the package name of Application (package name of your app) and you are not allowed to send android.intent.action.PHONE_STATE broadcast manually

and from comments, This may help you.

    toggleButton
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    buttonView
                            .getContext()
                            .getSharedPreferences("app_preference",
                                    Context.MODE_PRIVATE).edit()
                            .putBoolean("IS_RECEIVER_ENABLED", isChecked)
                            .commit();

                }
            });

and in onReceive()

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isEnabled = context.getSharedPreferences("app_preference",
                Context.MODE_PRIVATE).getBoolean("IS_RECEIVER_ENABLED",
                false);
        if (isEnabled) {
            // Show Toast here.
        }
    }

Upvotes: 1

Related Questions