2

I am beginner android developer.
I am trying to call BroadcastReceiver from Activity because I need to update entry if somebody call me.

This is the activity where I am calling BroadcastReceiver.

Source code:

public class CalendarCall extends Activity {

    private static final String TAG = "CalendarCall";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent i = new Intent(this, MyBroadcastReceiver.class);
        startActivity(i);
    }  
    // ......
}

MyBroadcastReceiver.class:

package org.example.calendarcall;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
         Bundle extras = intent.getExtras();
         if (extras != null) {
                String state = extras.getString(TelephonyManager.EXTRA_STATE);
                Log.w("DEBUG", state);
                if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                    String phoneNumber = extras
                            .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    Log.w("DEBUG", phoneNumber);
                }
         }
     }
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.example.calendarcall"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CalendarCall"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="MyBroadcastReceiver">
        </receiver>
    </application>
    <uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
</manifest> 

Error:

04-21 12:37:40.821: ERROR/AndroidRuntime(793): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.calendarcall/org.example.calendarcall.CalendarCall}: android.content.ActivityNotFoundException: Unable to find explicit activity class {org.example.calendarcall/org.example.calendarcall.MyBroadcastReceiver}; 

I think the problem is declaraction in Manifest and i also dont know if i can call BroadcastReceiver from Activity using:

Intent i = new Intent(this, MyBroadcastReceiver.class);
startActivity(i);
wtsang02
  • 18,603
  • 10
  • 49
  • 67
Husky
  • 1,451
  • 4
  • 19
  • 35

2 Answers2

2

A BroadCastReceiver receives intents from sender's who's calling the sendBroadcast() method. If you would like your MyBroadcastReceiver to be invoked when someone calls you, you could add the filter android.intent.action.PHONE_STATE like:

  <receiver android:name=".MyBroadcastReceiver">

    <intent-filter>

      <action android:name="android.intent.action.PHONE_STATE" />

    </intent-filter>

</receiver>

You don't really need your CalendarCall activity.

If you want to transfer information from your broadcast receiver to your activity, see this stackoverflow question.

Community
  • 1
  • 1
pakerfeldt
  • 1,491
  • 1
  • 14
  • 23
  • I added intent-filter but there is same error: 04-21 13:10:40.181: ERROR/AndroidRuntime(970): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {org.example.calendarcall/org.example.calendarcall.MyBroadcastReceiver}; And i need CalendarCall activity because there is a lot of buttons like fill calendar,... – Husky Apr 21 '11 at 11:13
  • 1
    If you only want to call your MyBroadcastReceiver from your CalendarCall activity, then you wouldn't really want a BroadcastReceiver but instead a standard Activity. Your error is caused because you are trying to start MyBroadcastReceiver as though it were an ordinary Activity (which it isn't). – pakerfeldt Apr 21 '11 at 11:21
  • By the looks of your MyBroadcastReceiver you are really interesting in the phone state. Your onReceive method will be called whenever the phone state changes. You do not trigger that manually from your other activity. MyBroadcastReceiver isn't an Activity, so you cannot start it with startActivity as you do in CalendarCall. And you shouldn't need to. When, exactly, do you want the code in onReceive to execute? – pakerfeldt Apr 21 '11 at 11:38
  • I want only take PhoneNumber and write it to the calendar when somebody call – Husky Apr 21 '11 at 11:47
  • You have the phone number in onReceive. If you want to transfer that information to your activity you might want to have a look at [this stackoverflow question](http://stackoverflow.com/questions/2513832/how-to-send-data-from-broadcastreceiver-to-an-activity-in-android) – pakerfeldt Apr 21 '11 at 11:53
0

you can call a broadcaste reciever from activity. you need to register and unregister the reciever and you need to call sendbroadcastereciver() also in your code

Durga
  • 1,191
  • 1
  • 12
  • 18